public class DiscardingRollingOutputStream
extends java.io.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 java.lang.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() |
java.util.concurrent.BlockingDeque<java.io.ByteArrayOutputStream> |
getFilledBuckets()
Return the filled buckets
|
protected java.io.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(java.io.ByteArrayOutputStream discardedBucket)
Designed for extension.
|
protected void |
onBucketRoll(java.io.ByteArrayOutputStream rolledBucket)
The rolled bucket.
|
void |
recycleBucket(java.io.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.
|
java.lang.String |
toString() |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public DiscardingRollingOutputStream(int maxBucketSizeInBytes,
int maxBucketCount)
maxBucketSizeInBytes - maximum byte size of each bucketmaxBucketCount - maximum number of bucketspublic void write(int b)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void write(byte[] b)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void write(byte[] b,
int off,
int len)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void flush()
throws java.io.IOException
flush in interface java.io.Flushableflush in class java.io.OutputStreamjava.io.IOExceptionpublic void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.OutputStreampublic void rollCurrentBucketIfNotEmpty()
public void rollCurrentBucket()
recycledBucketPool pool if one is available or recreated.protected void onBucketDiscard(java.io.ByteArrayOutputStream discardedBucket)
discardedBucket - the discarded bucketprotected void onBucketRoll(java.io.ByteArrayOutputStream rolledBucket)
rolledBucket - the discarded bucketprotected java.io.ByteArrayOutputStream newBucket()
recycledBucketPool or instantiate a new one if none available
in the free bucket pool.public void recycleBucket(java.io.ByteArrayOutputStream bucket)
bucket - the bucket to recyclepublic java.util.concurrent.BlockingDeque<java.io.ByteArrayOutputStream> getFilledBuckets()
public int getDiscardedBucketCount()
public long getCurrentOutputStreamSize()
public java.lang.String toString()
toString in class java.lang.Object