001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.layout;
018
019import java.nio.ByteBuffer;
020
021import org.apache.logging.log4j.core.appender.OutputStreamManager;
022
023/**
024 * ByteBufferDestination is the destination that {@link Encoder}s write binary data to. It encapsulates a
025 * {@code ByteBuffer} and a {@code drain()} method the producer can call when the {@code ByteBuffer} is full.
026 * <p>
027 * This interface allows a producer to write arbitrary amounts of data to a destination.
028 * </p>
029 * @since 2.6
030 */
031public interface ByteBufferDestination {
032    /**
033     * Returns the buffer to write to.
034     *
035     * @return the buffer to write to
036     */
037    ByteBuffer getByteBuffer();
038
039    /**
040     * Consumes the buffer content and returns a buffer with more {@linkplain ByteBuffer#remaining() available} space
041     * (which may or may not be the same instance).
042     * <p>
043     * Called by the producer when buffer becomes too full to write to.
044     *
045     * @param buf the buffer to drain
046     * @return a buffer with more available space (which may or may not be the same instance)
047     */
048    ByteBuffer drain(ByteBuffer buf);
049
050    /**
051     * Writes the given data to this ByteBufferDestination entirely. Call of this method should *not* be protected
052     * with synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should
053     * synchronize themselves inside this method, if needed.
054     *
055     * @since 2.9 (see LOG4J2-1874)
056     */
057    void writeBytes(ByteBuffer data);
058
059    /**
060     * Writes the given data to this ByteBufferDestination. Call of this method should *not* be protected with
061     * synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should
062     * synchronize themselves inside this method, if needed.
063     * <p>
064     * This method should behave identically to {@code writeBytes(ByteBuffer.wrap(data, offset, length)}.
065     * It is provided to allow callers not to generate extra garbage.
066     * <p>
067     * This method is called writeBytes() to avoid clashing with {@link OutputStreamManager#write(byte[], int, int)},
068     * which might be overridden in user-defined subclasses as protected, hence adding it to interface and requiring
069     * the method to be public breaks source compatibility.
070     *
071     * @since 2.9 (see LOG4J2-1874)
072     */
073    void writeBytes(byte[] data, int offset, int length);
074}