LOG4J2-1343 enabled MemoryMappedFileManager to reuse the garbage-free Layout mechanism defined in the superclass
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ef0023a4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ef0023a4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ef0023a4 Branch: refs/heads/LOG4J2-1356 Commit: ef0023a4087df35bc89ffc3f84a668995f2ca352 Parents: 875cec8 Author: rpopma <[email protected]> Authored: Thu Apr 7 03:07:31 2016 +0900 Committer: rpopma <[email protected]> Committed: Thu Apr 7 03:07:31 2016 +0900 ---------------------------------------------------------------------- .../core/appender/MemoryMappedFileManager.java | 57 +++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ef0023a4/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java index 9335ddb..dc9fc88 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/MemoryMappedFileManager.java @@ -22,6 +22,7 @@ import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.Serializable; import java.lang.reflect.Method; +import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; @@ -33,6 +34,7 @@ import java.util.Map; import java.util.Objects; import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.layout.ByteBufferDestination; import org.apache.logging.log4j.core.util.Closer; import org.apache.logging.log4j.core.util.NullOutputStream; @@ -42,7 +44,7 @@ import org.apache.logging.log4j.core.util.NullOutputStream; * Extends OutputStreamManager but instead of using a buffered output stream, this class maps a region of a file into * memory and writes to this memory region. * <p> - * + * * @see <a href="http://www.codeproject.com/Tips/683614/Things-to-Know-about-Memory-Mapped-File-in-Java"> * http://www.codeproject.com/Tips/683614/Things-to-Know-about-Memory-Mapped-File-in-Java</a> * @see <a href="http://bugs.java.com/view_bug.do?bug_id=6893654">http://bugs.java.com/view_bug.do?bug_id=6893654</a> @@ -50,11 +52,11 @@ import org.apache.logging.log4j.core.util.NullOutputStream; * @see <a * href="http://stackoverflow.com/questions/9261316/memory-mapped-mappedbytebuffer-or-direct-bytebuffer-for-db-implementation"> * http://stackoverflow.com/questions/9261316/memory-mapped-mappedbytebuffer-or-direct-bytebuffer-for-db-implementation</a> - * + * * @since 2.1 */ //CHECKSTYLE:ON -public class MemoryMappedFileManager extends OutputStreamManager { +public class MemoryMappedFileManager extends OutputStreamManager implements ByteBufferDestination { /** * Default length of region to map. */ @@ -111,6 +113,21 @@ public class MemoryMappedFileManager extends OutputStreamManager { } @Override + protected void write(final byte[] bytes) { + write(bytes, 0, bytes.length, false); + } + + @Override + protected void write(final byte[] bytes, final boolean immediateFlush) { + write(bytes, 0, bytes.length, immediateFlush); + } + + @Override + protected void write(final byte[] bytes, final int offset, final int length) { + write(bytes, 0, bytes.length, false); + } + + @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 @@ -141,7 +158,7 @@ public class MemoryMappedFileManager extends OutputStreamManager { final float millis = (float) ((System.nanoTime() - startNanos) / NANOS_PER_MILLISEC); LOGGER.debug("{} {} extended {} OK in {} millis", getClass().getSimpleName(), getName(), getFileName(), millis); - + mappedBuffer = mmap(randomAccessFile.getChannel(), getFileName(), offset, length); mappingOffset = offset; } catch (final Exception ex) { @@ -235,7 +252,7 @@ public class MemoryMappedFileManager extends OutputStreamManager { /** * Returns the length of the memory mapped region. - * + * * @return the length of the mapped region */ public int getRegionLength() { @@ -245,7 +262,7 @@ public class MemoryMappedFileManager extends OutputStreamManager { /** * Returns {@code true} if the content of the buffer should be forced to the storage device on every write, * {@code false} otherwise. - * + * * @return whether each write should be force-sync'ed */ public boolean isImmediateFlush() { @@ -257,7 +274,7 @@ public class MemoryMappedFileManager extends OutputStreamManager { * <p> * Key: "fileURI" Value: provided "advertiseURI" param. * </p> - * + * * @return Map of content format keys supporting FileManager */ @Override @@ -268,6 +285,32 @@ public class MemoryMappedFileManager extends OutputStreamManager { } /** + * Returns this {@code MemoryMappedFileManager}. + * @param immediateFlush ignored + * @return this {@code MemoryMappedFileManager} + */ + @Override + protected ByteBufferDestination createByteBufferDestination(final boolean immediateFlush) { + return this; + } + + @Override + protected void flushBuffer() { + // do nothing (avoid spurious calls to remap()) + } + + @Override + public ByteBuffer getByteBuffer() { + return mappedBuffer; + } + + @Override + public ByteBuffer drain(final ByteBuffer buf) { + remap(); + return mappedBuffer; + } + + /** * Factory Data. */ private static class FactoryData {
