ch.qos.logback.ext.loggly.io
Class DiscardingRollingOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by ch.qos.logback.ext.loggly.io.DiscardingRollingOutputStream
All Implemented Interfaces:
Closeable, Flushable

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:

Author:
Cyrille Le Clerc

Field Summary
static String LINE_SEPARATOR
           
 
Constructor Summary
DiscardingRollingOutputStream(int maxBucketSizeInBytes, int maxBucketCount)
           
 
Method Summary
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final String LINE_SEPARATOR
Constructor Detail

DiscardingRollingOutputStream

public DiscardingRollingOutputStream(int maxBucketSizeInBytes,
                                     int maxBucketCount)
Parameters:
maxBucketSizeInBytes -
maxBucketCount -
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

close

public void close()
Close all the underlying buckets (current bucket, filled buckets and buckets from the recycled buckets pool).

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream

rollCurrentBucketIfNotEmpty

public void rollCurrentBucketIfNotEmpty()
Roll current bucket if size threshold has been reached.


rollCurrentBucket

public void rollCurrentBucket()
Moves the current active bucket to the list of filled buckets and defines a new one.

The new active bucket is reused from the recycledBucketPool pool if one is available or recreated.


onBucketDiscard

protected void onBucketDiscard(ByteArrayOutputStream discardedBucket)
Designed for extension.

Parameters:
discardedBucket - the discarded bucket

onBucketRoll

protected void onBucketRoll(ByteArrayOutputStream rolledBucket)
The rolled bucket. Designed for extension.

Parameters:
rolledBucket - the discarded bucket

newBucket

protected ByteArrayOutputStream newBucket()
Get a new bucket from the recycledBucketPool or instantiate a new one if none available in the free bucket pool.

Returns:
the bucket ready to use

recycleBucket

public void recycleBucket(ByteArrayOutputStream bucket)
Returns the given bucket to the pool of free buckets.

Parameters:
bucket - the bucket to recycle

getFilledBuckets

public BlockingDeque<ByteArrayOutputStream> getFilledBuckets()
Return the filled buckets


getDiscardedBucketCount

public int getDiscardedBucketCount()
Number of discarded buckets. Monitoring oriented metric.


getCurrentOutputStreamSize

public long getCurrentOutputStreamSize()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2014-2015. All Rights Reserved.