LOG4J2-1343 refactored RandomAccessFileManager to reuse the garbage-free Layout mechanism defined in the superclass, simplified logic when bytes to write exceed buffer size
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/32e557bf Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/32e557bf Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/32e557bf Branch: refs/heads/LOG4J2-1356 Commit: 32e557bfffc9330242f7acacfc7a12c4553695ba Parents: 920f8aa Author: rpopma <[email protected]> Authored: Thu Apr 7 03:04:27 2016 +0900 Committer: rpopma <[email protected]> Committed: Thu Apr 7 03:04:27 2016 +0900 ---------------------------------------------------------------------- .../core/appender/RandomAccessFileManager.java | 44 +++++++++++++------- 1 file changed, 30 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/32e557bf/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java index ec616c4..9b7fc91 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/RandomAccessFileManager.java @@ -89,31 +89,37 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte protected synchronized void write(final byte[] bytes, int offset, int length, final boolean immediateFlush) { super.write(bytes, offset, length, immediateFlush); // writes to dummy output stream - int chunk = 0; - do { - if (length > buffer.remaining()) { - flush(); - } - chunk = Math.min(length, buffer.remaining()); - buffer.put(bytes, offset, chunk); - offset += chunk; - length -= chunk; - } while (length > 0); + if (length >= buffer.capacity()) { + // if request length exceeds buffer capacity, flush the buffer and write the data directly + flush(); + writeToRandomAccessFile(bytes, offset, length); + return; + } + if (length > buffer.remaining()) { + flush(); + } + buffer.put(bytes, offset, length); if (immediateFlush || isImmediateFlush || isEndOfBatch.get() == Boolean.TRUE) { flush(); } } - @Override - public synchronized void flush() { - buffer.flip(); + private void writeToRandomAccessFile(final byte[] bytes, final int offset, final int length) { try { - randomAccessFile.write(buffer.array(), 0, buffer.limit()); + randomAccessFile.write(bytes, offset, length); } catch (final IOException ex) { final String msg = "Error writing to RandomAccessFile " + getName(); throw new AppenderLoggingException(msg, ex); } + } + + @Override + public synchronized void flush() { + buffer.flip(); + if (buffer.limit() > 0) { + writeToRandomAccessFile(buffer.array(), 0, buffer.limit()); + } buffer.clear(); } @@ -160,6 +166,16 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte return result; } + /** + * Returns this {@code RandomAccessFileManager}. + * @param immediateFlush ignored + * @return this {@code RandomAccessFileManager} + */ + @Override + protected ByteBufferDestination createByteBufferDestination(final boolean immediateFlush) { + return this; + } + @Override public ByteBuffer getByteBuffer() { return buffer;
