View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.layout;
18  
19  import java.nio.ByteBuffer;
20  
21  /**
22   * Helper class for ByteBufferDestination implementors.
23   *
24   * @since 2.9 (see LOG4J2-1874)
25   */
26  public final class ByteBufferDestinationHelper {
27  
28      private ByteBufferDestinationHelper() {
29      }
30  
31      /**
32       * Writes the specified data to the specified destination. Doesn't synchronize on the destination object. The helper
33       * method for {@link ByteBufferDestination#writeBytes(ByteBuffer)} implementations.
34       *
35       * @param source        the data to write
36       * @param destination the {@code ByteBufferDestination} to write to
37       */
38      public static void writeToUnsynchronized(final ByteBuffer source, final ByteBufferDestination destination) {
39          ByteBuffer destBuff = destination.getByteBuffer();
40          while (source.remaining() > destBuff.remaining()) {
41              final int originalLimit = source.limit();
42              source.limit(Math.min(source.limit(), source.position() + destBuff.remaining()));
43              destBuff.put(source);
44              source.limit(originalLimit);
45              destBuff = destination.drain(destBuff);
46          }
47          destBuff.put(source);
48          // No drain in the end.
49      }
50  
51      /**
52       * Writes the specified data to the specified destination. Doesn't synchronize on the destination object. The helper
53       * method for {@link ByteBufferDestination#writeBytes(byte[], int, int)} implementations.
54       *
55       * @param data        the data to write
56       * @param offset      where to start in the specified data array
57       * @param length      the number of bytes to write
58       * @param destination the {@code ByteBufferDestination} to write to
59       */
60      public static void writeToUnsynchronized(final byte[] data, int offset, int length,
61              final ByteBufferDestination destination) {
62          ByteBuffer buffer = destination.getByteBuffer();
63          while (length > buffer.remaining()) {
64              final int chunk = buffer.remaining();
65              buffer.put(data, offset, chunk);
66              offset += chunk;
67              length -= chunk;
68              buffer = destination.drain(buffer);
69          }
70          buffer.put(data, offset, length);
71          // No drain in the end.
72      }
73  }