LOG4J2-1343 Added OutputStreamManagerDestination that adapts an OutputStreamManager to the ByteBufferDestination interface
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/49878b43 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/49878b43 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/49878b43 Branch: refs/heads/LOG4J2-1356 Commit: 49878b435b97f6f771a0f02e2621aabfb3b25d60 Parents: a95eeb8 Author: rpopma <[email protected]> Authored: Thu Apr 7 02:53:44 2016 +0900 Committer: rpopma <[email protected]> Committed: Thu Apr 7 02:53:44 2016 +0900 ---------------------------------------------------------------------- .../OutputStreamManagerDestination.java | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/49878b43/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManagerDestination.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManagerDestination.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManagerDestination.java new file mode 100644 index 0000000..18689a1 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManagerDestination.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.appender; + +import java.nio.ByteBuffer; +import java.util.Objects; + +import org.apache.logging.log4j.core.layout.ByteBufferDestination; + +/** + * Decorates an {@code OutputStreamManager} to adapt it to the {@code ByteBufferDestination} interface. + */ +public class OutputStreamManagerDestination implements ByteBufferDestination { + private static final int DEFAULT_BUFFER_SIZE = 8 * 1024; + private final ByteBuffer byteBuffer; + private final boolean immediateFlush; + private final OutputStreamManager outputStreamManager; + + public OutputStreamManagerDestination(final boolean immediateFlush, final OutputStreamManager outputStreamManager) { + this(DEFAULT_BUFFER_SIZE, immediateFlush, outputStreamManager); + } + + public OutputStreamManagerDestination(final int bufferSize, final boolean immediateFlush, + final OutputStreamManager outputStreamManager) { + + this.byteBuffer = ByteBuffer.wrap(new byte[bufferSize]); + this.immediateFlush = immediateFlush; + this.outputStreamManager = Objects.requireNonNull(outputStreamManager, "outputStreamManager"); + } + + @Override + public ByteBuffer getByteBuffer() { + return byteBuffer; + } + + @Override + public ByteBuffer drain(final ByteBuffer buf) { + buf.flip(); + if (buf.limit() > 0) { + outputStreamManager.write(buf.array(), 0, buf.limit(), immediateFlush); + } + buf.clear(); + return buf; + } +}
