Skip to content

Commit

Permalink
Rename ReusableByteBuffers into ReusableByteBufferPool
Browse files Browse the repository at this point in the history
  • Loading branch information
brenuart committed Jul 5, 2021
1 parent 0d8ad26 commit d46e411
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import net.logstash.logback.decorate.JsonFactoryDecorator;
import net.logstash.logback.decorate.JsonGeneratorDecorator;
import net.logstash.logback.util.ReusableByteBuffer;
import net.logstash.logback.util.ReusableByteBuffers;
import net.logstash.logback.util.ReusableByteBufferPool;

import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.EncoderBase;
Expand All @@ -48,7 +48,7 @@ public abstract class CompositeJsonEncoder<Event extends DeferredProcessingAware
/**
* Provides reusable byte buffers (initialized when the encoder is started).
*/
private ReusableByteBuffers bufferPool;
private ReusableByteBufferPool bufferPool;

private Encoder<Event> prefix;
private Encoder<Event> suffix;
Expand Down Expand Up @@ -115,7 +115,7 @@ public void start() {
}

super.start();
this.bufferPool = new ReusableByteBuffers(this.minBufferSize);
this.bufferPool = new ReusableByteBufferPool(this.minBufferSize);
formatter.setContext(getContext());
formatter.start();
charset = Charset.forName(formatter.getEncoding());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import net.logstash.logback.encoder.CompositeJsonEncoder;
import net.logstash.logback.encoder.SeparatorParser;
import net.logstash.logback.util.ReusableByteBuffer;
import net.logstash.logback.util.ReusableByteBuffers;
import net.logstash.logback.util.ReusableByteBufferPool;

import ch.qos.logback.core.Layout;
import ch.qos.logback.core.LayoutBase;
Expand Down Expand Up @@ -62,7 +62,7 @@ public abstract class CompositeJsonLayout<Event extends DeferredProcessingAware>
/**
* Provides reusable byte buffers (initialized when layout is started)
*/
private ReusableByteBuffers bufferPool;
private ReusableByteBufferPool bufferPool;


private final CompositeJsonFormatter<Event> formatter;
Expand Down Expand Up @@ -120,7 +120,7 @@ private void writeFormatter(Writer writer, Event event) throws IOException {
@Override
public void start() {
super.start();
this.bufferPool = new ReusableByteBuffers(this.minBufferSize);
this.bufferPool = new ReusableByteBufferPool(this.minBufferSize);
formatter.setContext(getContext());
formatter.start();
startWrapped(prefix);
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/net/logstash/logback/util/ReusableByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* <p>The {@link #reset()} method clears the content and resets the buffer to its initial state.
* Buffers are disposed except the initial buffer which is reused by subsequent usage.
*
* <p>This class is *not* thread-safe!
*
* @author brenuart
*
*/
Expand Down Expand Up @@ -62,7 +64,7 @@ public class ReusableByteBuffer extends OutputStream {
/**
* The write index in the tail buffer
*/
private int index = 0;
private int tailWriteIndex = 0;

/**
* Is the stream closed?
Expand Down Expand Up @@ -97,7 +99,7 @@ public void write(int datum) throws IOException {
}

growIfNeeded();
getTailBuffer()[this.index++] = (byte) datum;
getTailBuffer()[this.tailWriteIndex++] = (byte) datum;
}


Expand All @@ -114,13 +116,13 @@ public void write(byte[] data, int offset, int length) throws IOException {

while (length > 0) {
byte[] buffer = getTailBuffer();
int freeSpace = buffer.length - this.index;
int freeSpace = buffer.length - this.tailWriteIndex;

if (freeSpace > 0) {
int toCopy = Math.min(freeSpace, length);
System.arraycopy(data, offset, buffer, this.index, toCopy);
System.arraycopy(data, offset, buffer, this.tailWriteIndex, toCopy);
offset += toCopy;
this.index += toCopy;
this.tailWriteIndex += toCopy;
length -= toCopy;
}

Expand All @@ -143,7 +145,7 @@ public void close() {
* @return the current size of the buffer.
*/
public int size() {
return this.alreadyBufferedSize + this.index;
return this.alreadyBufferedSize + this.tailWriteIndex;
}


Expand All @@ -158,9 +160,8 @@ public void reset() {
this.buffers.clear();
this.buffers.add(initialBuffer);

//this.nextBlockSize = this.initialBlockSize;
this.closed = false;
this.index = 0;
this.tailWriteIndex = 0;
this.alreadyBufferedSize = 0;
}

Expand All @@ -177,7 +178,7 @@ public void writeTo(OutputStream out) throws IOException {
if (it.hasNext()) {
out.write(buffer, 0, buffer.length);
} else {
out.write(buffer, 0, this.index);
out.write(buffer, 0, this.tailWriteIndex);
}
}
}
Expand Down Expand Up @@ -209,7 +210,7 @@ public byte[] toByteArray() {
System.arraycopy(buffer, 0, result, offset, buffer.length);
offset += buffer.length;
} else {
System.arraycopy(buffer, 0, result, offset, this.index);
System.arraycopy(buffer, 0, result, offset, this.tailWriteIndex);
}
}

Expand All @@ -221,10 +222,10 @@ public byte[] toByteArray() {
* Allocate a new chunk if needed
*/
private void growIfNeeded() {
if (getTailBuffer().length == this.index) {
this.alreadyBufferedSize += this.index;
this.buffers.add(new byte[this.index * 2]); // block size doubles each time
this.index = 0;
if (getTailBuffer().length == this.tailWriteIndex) {
this.alreadyBufferedSize += this.tailWriteIndex;
this.buffers.add(new byte[this.tailWriteIndex * 2]); // block size doubles each time
this.tailWriteIndex = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
* A pool of {@link ReusableByteBuffer}.
*
* <p>The pool is technically unbounded but will never hold more buffers than the number of concurrent
* threads accessing it. Buffers are kept in the pool using weak references so they can be garbage
* threads accessing it. Buffers are kept in the pool using soft references so they can be garbage
* collected by the JVM when running low in memory.
*
* @author brenuart
*/
public class ReusableByteBuffers {
public class ReusableByteBufferPool {

/**
* Pool of reusable buffers.
Expand All @@ -44,7 +44,7 @@ public class ReusableByteBuffers {
*
* @param intialCapacity the initial capacity of buffers created by this pool.
*/
public ReusableByteBuffers(int intialCapacity) {
public ReusableByteBufferPool(int intialCapacity) {
if (intialCapacity <= 0) {
throw new IllegalArgumentException("initialCapacity must be greater than 0");
}
Expand All @@ -54,7 +54,7 @@ public ReusableByteBuffers(int intialCapacity) {
/**
* Create a new buffer pool holding buffers with a default initial capacity.
*/
public ReusableByteBuffers() {
public ReusableByteBufferPool() {
this(ReusableByteBuffer.DEFAULT_INITIAL_CAPACITY);
}

Expand Down

0 comments on commit d46e411

Please sign in to comment.