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/04 17:30:06 UTC
[2/2] logging-log4j2 git commit: LOG4J2-1343,
LOG4J2-1344 ByteBufferDestinationOutputStream replaced
OutputStreamByteBufferDestinationAdapter
LOG4J2-1343, LOG4J2-1344 ByteBufferDestinationOutputStream replaced OutputStreamByteBufferDestinationAdapter
- this is essentially a BufferedOutputStream
- except that for its buffer it has a ByteBuffer so this class can also be used as a ByteBufferDestination
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8616f8ff
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8616f8ff
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8616f8ff
Branch: refs/heads/master
Commit: 8616f8ff19f813e460edfe46a595a82f5463a4e2
Parents: b7d7833
Author: rpopma <rp...@apache.org>
Authored: Tue Apr 5 00:30:18 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Tue Apr 5 00:30:18 2016 +0900
----------------------------------------------------------------------
.../util/ByteBufferDestinationOutputStream.java | 141 +++++++++++++++++++
...utputStreamByteBufferDestinationAdapter.java | 80 -----------
2 files changed, 141 insertions(+), 80 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8616f8ff/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
new file mode 100644
index 0000000..b6a7ed4
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ByteBufferDestinationOutputStream.java
@@ -0,0 +1,141 @@
+/*
+ * 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.util;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Objects;
+
+import org.apache.logging.log4j.core.layout.ByteBufferDestination;
+
+/**
+ * Buffered OutputStream that implements the ByteBufferDestination interface.
+ * <p>
+ * This class is not thread-safe and it cannot be made thread-safe on its own since it exposes its buffer for direct
+ * modification. Client code needs to make sure multiple threads don't modify the buffer or write to this OutputStream
+ * concurrently.
+ * </p>
+ *
+ * @see ByteBufferDestination
+ */
+public class ByteBufferDestinationOutputStream extends FilterOutputStream implements ByteBufferDestination {
+ private static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
+ private final ByteBuffer byteBuffer;
+
+ public ByteBufferDestinationOutputStream(final OutputStream out) {
+ this(out, DEFAULT_BUFFER_SIZE);
+ }
+
+ public ByteBufferDestinationOutputStream(final OutputStream out, final int bufferSize) {
+ super(Objects.requireNonNull(out));
+ this.byteBuffer = ByteBuffer.wrap(new byte[Assert.valueIsAtLeast(bufferSize, 16)]);
+ }
+
+ @Override
+ public ByteBuffer getByteBuffer() {
+ return byteBuffer;
+ }
+
+ /**
+ * Writes any data in the specified {@code ByteBuffer} to the wrapped {@code OutputStream}, returning the specified
+ * {@code ByteBuffer}.
+ *
+ * @param buf the buffer to drain
+ * @return the specified ByteBuffer
+ */
+ @Override
+ public ByteBuffer drain(final ByteBuffer buf) {
+ buf.flip();
+ try {
+ out.write(buf.array(), 0, buf.limit());
+ } catch (final IOException ex) {
+ throw new IllegalStateException("Could not write " + buf.limit() + " bytes to " + out, ex);
+ }
+ buf.clear();
+ return buf;
+ }
+
+ /**
+ * Writes any data remaining in the {@code ByteBuffer} to the {@code OutputStream}.
+ */
+ /**
+ * Flushes this buffered output stream. This forces any buffered
+ * output bytes to be written out to the underlying output stream.
+ *
+ * @exception IOException if an I/O error occurs.
+ * @see java.io.FilterOutputStream#out
+ */
+ public void flush() {
+ flushBuffer();
+ try {
+ out.flush();
+ } catch (final IOException ex) {
+ throw new IllegalStateException("Could not flush " + out, ex);
+ }
+ }
+
+ private void flushBuffer() {
+ drain(byteBuffer);
+ }
+
+ /**
+ * Writes <code>len</code> bytes from the specified byte array
+ * starting at offset <code>off</code> to this buffered output stream.
+ *
+ * <p> Ordinarily this method stores bytes from the given array into this
+ * stream's buffer, flushing the buffer to the underlying output stream as
+ * needed. If the requested length is at least as large as this stream's
+ * buffer, however, then this method will flush the buffer and write the
+ * bytes directly to the underlying output stream. Thus redundant
+ * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
+ *
+ * @param src the data.
+ * @param offset the start offset in the data.
+ * @param length the number of bytes to write.
+ * @exception IOException if an I/O error occurs.
+ */
+ public void write(final byte src[], final int offset, final int length) throws IOException {
+ if (length >= byteBuffer.capacity()) {
+ /* If the request length exceeds the size of the output buffer,
+ flush the output buffer and then write the data directly.
+ In this way buffered streams will cascade harmlessly. */
+ flushBuffer();
+ out.write(src, offset, length);
+ return;
+ }
+ if (length > byteBuffer.remaining()) {
+ flushBuffer();
+ }
+ byteBuffer.put(src, offset, length);
+ }
+
+
+ /**
+ * Writes the specified byte to this buffered output stream.
+ *
+ * @param b the byte to be written.
+ * @exception IOException if an I/O error occurs.
+ */
+ public void write(final int b) throws IOException {
+ if (byteBuffer.remaining() <= 0) {
+ flushBuffer();
+ }
+ byteBuffer.put((byte) b);
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8616f8ff/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OutputStreamByteBufferDestinationAdapter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OutputStreamByteBufferDestinationAdapter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OutputStreamByteBufferDestinationAdapter.java
deleted file mode 100644
index c48582a..0000000
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OutputStreamByteBufferDestinationAdapter.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.util;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.nio.ByteBuffer;
-import java.util.Objects;
-
-import org.apache.logging.log4j.core.layout.ByteBufferDestination;
-import org.apache.logging.log4j.core.util.Assert;
-
-/**
- * Adapts an OutputStream to the ByteBufferDestination interface.
- *
- * @see ByteBufferDestination
- */
-public class OutputStreamByteBufferDestinationAdapter implements ByteBufferDestination {
- private static final int DEFAULT_BUFFER_SIZE = 4 * 1024;
- private final OutputStream out;
- private final int bufferSize;
- private ByteBuffer byteBuffer;
-
- public OutputStreamByteBufferDestinationAdapter(final OutputStream out) {
- this(out, DEFAULT_BUFFER_SIZE);
- }
-
- public OutputStreamByteBufferDestinationAdapter(final OutputStream out, final int bufferSize) {
- this.out = Objects.requireNonNull(out);
- this.bufferSize = Assert.valueIsAtLeast(bufferSize, 16);
- }
-
- @Override
- public ByteBuffer getByteBuffer() {
- if (byteBuffer == null) {
- byteBuffer = ByteBuffer.wrap(new byte[bufferSize]);
- }
- return byteBuffer;
- }
-
- /**
- * Writes any data in the specified {@code ByteBuffer} to the wrapped {@code OutputStream}, returning the specified
- * {@code ByteBuffer}.
- *
- * @param buf the buffer to drain
- * @return the specified ByteBuffer
- */
- @Override
- public ByteBuffer drain(final ByteBuffer buf) {
- buf.flip();
- try {
- out.write(buf.array(), 0, buf.limit());
- } catch (final IOException ex) {
- throw new IllegalStateException("Could not write " + buf.limit() + " bytes to " + out, ex);
- }
- buf.clear();
- return buf;
- }
-
- /**
- * Writes any data remaining in the {@code ByteBuffer} to the {@code OutputStream}.
- */
- public void drain() {
- drain(getByteBuffer());
- }
-}