From d46e4112c8824eb9007337d41ef389b27139cd4f Mon Sep 17 00:00:00 2001 From: Bertrand Renuart Date: Mon, 5 Jul 2021 23:40:40 +0200 Subject: [PATCH] Rename ReusableByteBuffers into ReusableByteBufferPool --- .../logback/encoder/CompositeJsonEncoder.java | 6 ++-- .../logback/layout/CompositeJsonLayout.java | 6 ++-- .../logback/util/ReusableByteBuffer.java | 29 ++++++++++--------- ...ffers.java => ReusableByteBufferPool.java} | 8 ++--- 4 files changed, 25 insertions(+), 24 deletions(-) rename src/main/java/net/logstash/logback/util/{ReusableByteBuffers.java => ReusableByteBufferPool.java} (93%) diff --git a/src/main/java/net/logstash/logback/encoder/CompositeJsonEncoder.java b/src/main/java/net/logstash/logback/encoder/CompositeJsonEncoder.java index 7bfe3be0..44968855 100644 --- a/src/main/java/net/logstash/logback/encoder/CompositeJsonEncoder.java +++ b/src/main/java/net/logstash/logback/encoder/CompositeJsonEncoder.java @@ -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; @@ -48,7 +48,7 @@ public abstract class CompositeJsonEncoder prefix; private Encoder suffix; @@ -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()); diff --git a/src/main/java/net/logstash/logback/layout/CompositeJsonLayout.java b/src/main/java/net/logstash/logback/layout/CompositeJsonLayout.java index e56a1e5c..29b1b49c 100644 --- a/src/main/java/net/logstash/logback/layout/CompositeJsonLayout.java +++ b/src/main/java/net/logstash/logback/layout/CompositeJsonLayout.java @@ -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; @@ -62,7 +62,7 @@ public abstract class CompositeJsonLayout /** * Provides reusable byte buffers (initialized when layout is started) */ - private ReusableByteBuffers bufferPool; + private ReusableByteBufferPool bufferPool; private final CompositeJsonFormatter formatter; @@ -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); diff --git a/src/main/java/net/logstash/logback/util/ReusableByteBuffer.java b/src/main/java/net/logstash/logback/util/ReusableByteBuffer.java index 6ca2ff43..5e18884f 100644 --- a/src/main/java/net/logstash/logback/util/ReusableByteBuffer.java +++ b/src/main/java/net/logstash/logback/util/ReusableByteBuffer.java @@ -34,6 +34,8 @@ *

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. * + *

This class is *not* thread-safe! + * * @author brenuart * */ @@ -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? @@ -97,7 +99,7 @@ public void write(int datum) throws IOException { } growIfNeeded(); - getTailBuffer()[this.index++] = (byte) datum; + getTailBuffer()[this.tailWriteIndex++] = (byte) datum; } @@ -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; } @@ -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; } @@ -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; } @@ -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); } } } @@ -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); } } @@ -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; } } diff --git a/src/main/java/net/logstash/logback/util/ReusableByteBuffers.java b/src/main/java/net/logstash/logback/util/ReusableByteBufferPool.java similarity index 93% rename from src/main/java/net/logstash/logback/util/ReusableByteBuffers.java rename to src/main/java/net/logstash/logback/util/ReusableByteBufferPool.java index e898f3b9..6635bbef 100644 --- a/src/main/java/net/logstash/logback/util/ReusableByteBuffers.java +++ b/src/main/java/net/logstash/logback/util/ReusableByteBufferPool.java @@ -22,12 +22,12 @@ * A pool of {@link ReusableByteBuffer}. * *

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. @@ -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"); } @@ -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); }