/** * @param maxBucketSizeInBytes maximum byte size of each bucket * @param maxBucketCount maximum number of buckets */ public DiscardingRollingOutputStream(int maxBucketSizeInBytes, int maxBucketCount) { if (maxBucketCount < 2) { throw new IllegalArgumentException("'maxBucketCount' must be >1"); } this.maxBucketSizeInBytes = maxBucketSizeInBytes; this.filledBuckets = new LinkedBlockingDeque<ByteArrayOutputStream>(maxBucketCount); this.recycledBucketPool = new ConcurrentLinkedQueue<ByteArrayOutputStream>(); this.currentBucket = newBucket(); }
/** * Moves the current active bucket to the list of filled buckets and defines a new one. * * The new active bucket is reused from the {@link #recycledBucketPool} pool if one is available or recreated. */ public void rollCurrentBucket() { currentBucketLock.lock(); try { boolean offered = filledBuckets.offer(currentBucket); if (offered) { onBucketRoll(currentBucket); } else { onBucketDiscard(currentBucket); discardedBucketCount.incrementAndGet(); } currentBucket = newBucket(); } finally { currentBucketLock.unlock(); } }