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 import org.apache.logging.log4j.core.appender.OutputStreamManager;
22
23 /**
24 * ByteBufferDestination is the destination that {@link Encoder}s write binary data to. It encapsulates a
25 * {@code ByteBuffer} and a {@code drain()} method the producer can call when the {@code ByteBuffer} is full.
26 * <p>
27 * This interface allows a producer to write arbitrary amounts of data to a destination.
28 * </p>
29 * @since 2.6
30 */
31 public interface ByteBufferDestination {
32 /**
33 * Returns the buffer to write to.
34 *
35 * @return the buffer to write to
36 */
37 ByteBuffer getByteBuffer();
38
39 /**
40 * Consumes the buffer content and returns a buffer with more {@linkplain ByteBuffer#remaining() available} space
41 * (which may or may not be the same instance).
42 * <p>
43 * Called by the producer when buffer becomes too full to write to.
44 *
45 * @param buf the buffer to drain
46 * @return a buffer with more available space (which may or may not be the same instance)
47 */
48 ByteBuffer drain(ByteBuffer buf);
49
50 /**
51 * Writes the given data to this ByteBufferDestination entirely. Call of this method should *not* be protected
52 * with synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should
53 * synchronize themselves inside this method, if needed.
54 *
55 * @since 2.9 (see LOG4J2-1874)
56 */
57 void writeBytes(ByteBuffer data);
58
59 /**
60 * Writes the given data to this ByteBufferDestination. Call of this method should *not* be protected with
61 * synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should
62 * synchronize themselves inside this method, if needed.
63 * <p>
64 * This method should behave identically to {@code writeBytes(ByteBuffer.wrap(data, offset, length)}.
65 * It is provided to allow callers not to generate extra garbage.
66 * <p>
67 * This method is called writeBytes() to avoid clashing with {@link OutputStreamManager#write(byte[], int, int)},
68 * which might be overridden in user-defined subclasses as protected, hence adding it to interface and requiring
69 * the method to be public breaks source compatibility.
70 *
71 * @since 2.9 (see LOG4J2-1874)
72 */
73 void writeBytes(byte[] data, int offset, int length);
74 }