@NotThreadSafe public class SplittableOutputStream extends OutputStream implements SplittableOutputStreamBuilder, SplittableOutputStreamBuilder.SplitOutputStreamBuilder
Deals with structured (e.g. XML) or unstructured data.
User code must invoke beginRecord()
before writing byte
s, and subsequently delimit safe
splitting points by invoking endRecord()
. The number of byte
s written between the
call to beginRecord()
and call to endRecord()
must not exceed
maxSize(long)
- (header
.length
+ footer
.length
)
A record is defined as any byte
s written between calls to beginRecord()
and
endRecord()
. Should the maximum file size be specified and the length of a single record (plus
header and footer) exceed the maximum then a BufferOverflowException
is throw.
Common usage would be splitting files, in this case the Supplier
is expected to keep
track of output file names.
Please note limitation; due to the use of Long
internally, the maximum
size per-file is limited to 9223372036854775807L bytes (which is
9,223PB or 9,223,000,000GB) per split OutputStream
.
SplittableOutputStreamBuilder.SplitOutputStreamBuilder
Modifier and Type | Method and Description |
---|---|
void |
beginRecord()
Called to mark the beginning of a record (where a "record" is any block
of bytes that can only be treated atomically; in that it's valid to split content
at the record's boundaries.
|
void |
close() |
void |
endRecord()
Called to mark the end of a record
|
SplittableOutputStreamBuilder.SplitOutputStreamBuilder |
maxCount(long numberOfRecords)
Split criteria for maximum number of records.
|
SplittableOutputStreamBuilder.SplitOutputStreamBuilder |
maxSize(long bytes)
Split criteria for maximum file size.
|
SplittableOutputStream |
outputStream()
The builder's build method.
|
static SplittableOutputStreamBuilder |
splittable(Supplier<OutputStream> next,
byte[] head,
byte[] footer)
Begin building a
SplittableOutputStream |
void |
write(byte[] bytes) |
void |
write(int b) |
flush, write
public static SplittableOutputStreamBuilder splittable(Supplier<OutputStream> next, byte[] head, byte[] footer) throws IOException
SplittableOutputStream
next
- a Supplier
of the underlying OutputStream
sheader
- written at the start of each OutputStream
(e.g. file header)footer
- written at the end of each OutputStream
(e.g. file footer)IOException
- rethrows in the unlikely event the underlying ByteArrayOutputStream buffer doespublic SplittableOutputStreamBuilder.SplitOutputStreamBuilder maxSize(long bytes)
SplittableOutputStreamBuilder
Split criteria for maximum file size.
May also be composed with maximum record count criteria.
maxSize
in interface SplittableOutputStreamBuilder
bytes
- the maximum file size permitted.SplittableOutputStreamBuilder.maxCount(long)
public SplittableOutputStreamBuilder.SplitOutputStreamBuilder maxCount(long numberOfRecords)
SplittableOutputStreamBuilder
Split criteria for maximum number of records. Where the definition of a record
is any bytes
written between calls to SplittableOutputStream#start()
and
SplittableOutputStream#finish()
May also be composed with maximum file size criteria.
maxCount
in interface SplittableOutputStreamBuilder
numberOfRecords
- the maximum permitted number of records per file.SplittableOutputStreamBuilder.maxSize(long)
public SplittableOutputStream outputStream() throws IOException
SplittableOutputStreamBuilder.SplitOutputStreamBuilder
The builder's build method.
Note: side effect; also writes the head byte
array.
outputStream
in interface SplittableOutputStreamBuilder.SplitOutputStreamBuilder
OutputStream
for regular useIOException
- if the attempt to write the head failspublic void write(int b) throws IOException
write
in class OutputStream
IOException
public void write(byte[] bytes) throws IOException
write
in class OutputStream
IOException
public void beginRecord()
endRecord()
public void endRecord() throws IOException
IOException
- rethrows anything from the underlying OutputStream
beginRecord()
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
IOException
Copyright © 2017–2018 earcam. All rights reserved.