You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2016/04/11 19:53:34 UTC

[07/11] logging-log4j2 git commit: LOG4J2-1343 RandomRollingFileManager: simplified now that superclass owns the ByteBuffer

LOG4J2-1343 RandomRollingFileManager: simplified now that superclass owns the ByteBuffer


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d3c029b1
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d3c029b1
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d3c029b1

Branch: refs/heads/LOG4J2-1343-no-gc-os-appenders-bytebuffered
Commit: d3c029b1353e43569acdf50c59f478f14723848d
Parents: 5c5209b
Author: rpopma <rp...@apache.org>
Authored: Tue Apr 12 02:39:35 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Apr 12 02:39:35 2016 +0900

----------------------------------------------------------------------
 .../core/appender/RandomAccessFileManager.java  | 63 +++-----------------
 1 file changed, 7 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d3c029b1/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 cbd5eb1..6a2bee5 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
@@ -39,22 +39,17 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
 
     private static final RandomAccessFileManagerFactory FACTORY = new RandomAccessFileManagerFactory();
 
-    private final boolean isImmediateFlush;
     private final String advertiseURI;
     private final RandomAccessFile randomAccessFile;
-    private final ByteBuffer buffer;
     private final ThreadLocal<Boolean> isEndOfBatch = new ThreadLocal<>();
 
     protected RandomAccessFileManager(final RandomAccessFile file,
-            final String fileName, final OutputStream os,
-            final boolean immediateFlush, final int bufferSize,
+            final String fileName, final OutputStream os, final int bufferSize,
             final String advertiseURI, final Layout<? extends Serializable> layout, final boolean writeHeader) {
-        super(os, fileName, layout, writeHeader);
-        this.isImmediateFlush = immediateFlush;
+        super(os, fileName, layout, writeHeader, ByteBuffer.wrap(new byte[bufferSize]));
         this.randomAccessFile = file;
         this.advertiseURI = advertiseURI;
         this.isEndOfBatch.set(Boolean.FALSE);
-        this.buffer = ByteBuffer.wrap(new byte[bufferSize]);
     }
 
     /**
@@ -86,26 +81,7 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
     }
 
     @Override
-    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
-
-        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();
-        }
-    }
-
-    private void writeToRandomAccessFile(final byte[] bytes, final int offset, final int length) {
+    protected void writeToDestination(final byte[] bytes, final int offset, final int length) {
         try {
             randomAccessFile.write(bytes, offset, length);
         } catch (final IOException ex) {
@@ -116,11 +92,7 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
 
     @Override
     public synchronized void flush() {
-        buffer.flip();
-        if (buffer.limit() > 0) {
-            writeToRandomAccessFile(buffer.array(), 0, buffer.limit());
-        }
-        buffer.clear();
+        flushBuffer(byteBuffer);
     }
 
     @Override
@@ -147,7 +119,7 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
      * @return the buffer size
      */
     public int getBufferSize() {
-        return buffer.capacity();
+        return byteBuffer.capacity();
     }
 
     /**
@@ -167,27 +139,6 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
     }
 
     /**
-     * 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;
-    }
-
-    @Override
-    public ByteBuffer drain(final ByteBuffer buf) {
-        flush();
-        return buffer;
-    }
-
-    /**
      * Factory Data.
      */
     private static class FactoryData {
@@ -201,7 +152,7 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
          * Constructor.
          *
          * @param append Append status.
-         * @param bufferSize TODO
+         * @param bufferSize size of the buffer
          */
         public FactoryData(final boolean append, final boolean immediateFlush,
                 final int bufferSize, final String advertiseURI, final Layout<? extends Serializable> layout) {
@@ -247,7 +198,7 @@ public class RandomAccessFileManager extends OutputStreamManager implements Byte
                 } else {
                     raf.setLength(0);
                 }
-                return new RandomAccessFileManager(raf, name, os, data.immediateFlush,
+                return new RandomAccessFileManager(raf, name, os,
                         data.bufferSize, data.advertiseURI, data.layout, writeHeader);
             } catch (final Exception ex) {
                 LOGGER.error("RandomAccessFileManager (" + name + ") " + ex, ex);