public class DiscardingRollingOutputStream extends OutputStream
Capped in-memory OutputStream composed of a chain of ByteArrayOutputStream called 'buckets'.
Each 'bucket' is limited in size (see maxBucketSizeInBytes) and the total size of the OutputStream
is bounded thanks to a discarding policy. An external component is expected to consume the filled buckets thanks to
getFilledBuckets().
Implementation decisions:
FileBackedOutputStream but had the drawback to introduce a dependency. Loggly batch appender use case
should be OK with a pure in-memory approach.| Modifier and Type | Field and Description |
|---|---|
static String |
LINE_SEPARATOR |
| Constructor and Description |
|---|
DiscardingRollingOutputStream(int maxBucketSizeInBytes,
int maxBucketCount) |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close all the underlying buckets (current bucket, filled buckets and buckets from the recycled buckets pool).
|
void |
flush() |
long |
getCurrentOutputStreamSize() |
int |
getDiscardedBucketCount()
Number of discarded buckets.
|
BlockingDeque<ByteArrayOutputStream> |
getFilledBuckets()
Return the filled buckets
|
protected ByteArrayOutputStream |
newBucket()
Get a new bucket from the
recycledBucketPool or instantiate a new one if none available
in the free bucket pool. |
protected void |
onBucketDiscard(ByteArrayOutputStream discardedBucket)
Designed for extension.
|
protected void |
onBucketRoll(ByteArrayOutputStream rolledBucket)
The rolled bucket.
|
void |
recycleBucket(ByteArrayOutputStream bucket)
Returns the given bucket to the pool of free buckets.
|
void |
rollCurrentBucket()
Moves the current active bucket to the list of filled buckets and defines a new one.
|
void |
rollCurrentBucketIfNotEmpty()
Roll current bucket if size threshold has been reached.
|
String |
toString() |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public static final String LINE_SEPARATOR
public DiscardingRollingOutputStream(int maxBucketSizeInBytes,
int maxBucketCount)
maxBucketSizeInBytes - maxBucketCount - public void write(int b)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b,
int off,
int len)
throws IOException
write in class OutputStreamIOExceptionpublic void flush()
throws IOException
flush in interface Flushableflush in class OutputStreamIOExceptionpublic void close()
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreampublic void rollCurrentBucketIfNotEmpty()
public void rollCurrentBucket()
recycledBucketPool pool if one is available or recreated.protected void onBucketDiscard(ByteArrayOutputStream discardedBucket)
discardedBucket - the discarded bucketprotected void onBucketRoll(ByteArrayOutputStream rolledBucket)
rolledBucket - the discarded bucketprotected ByteArrayOutputStream newBucket()
recycledBucketPool or instantiate a new one if none available
in the free bucket pool.public void recycleBucket(ByteArrayOutputStream bucket)
bucket - the bucket to recyclepublic BlockingDeque<ByteArrayOutputStream> getFilledBuckets()
public int getDiscardedBucketCount()
public long getCurrentOutputStreamSize()
Copyright © 2014. All Rights Reserved.