001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.nio.util;
029
030import java.io.IOException;
031
032import org.apache.http.nio.ContentDecoder;
033
034/**
035 * Generic content input buffer.
036 *
037 * @since 4.0
038 */
039public interface ContentInputBuffer {
040
041    /**
042     * Reads content from the given {@link ContentDecoder} and stores it in
043     * this buffer.
044     *
045     * @param decoder the content decoder.
046     * @return number of bytes read.
047     * @throws IOException in case of an I/O error.
048     *
049     * @deprecated (4.3) use implementation specific methods.
050     */
051    @Deprecated
052    int consumeContent(ContentDecoder decoder) throws IOException;
053
054    /**
055     * Resets the buffer by clearing its state and stored content.
056     */
057    void reset();
058
059    /**
060     * Reads up to {@code len} bytes of data from this buffer into
061     * an array of bytes. The exact number of bytes read depends how many bytes
062     * are stored in the buffer.
063     *
064     * <p> If {@code off} is negative, or {@code len} is negative, or
065     * {@code off+len} is greater than the length of the array
066     * {@code b}, this method can throw a runtime exception. The exact type
067     * of runtime exception thrown by this method depends on implementation.
068     * This method returns {@code -1} if the end of content stream has been
069     * reached.
070     *
071     * @param      b     the buffer into which the data is read.
072     * @param      off   the start offset in array {@code b}
073     *                   at which the data is written.
074     * @param      len   the maximum number of bytes to read.
075     * @return     the total number of bytes read into the buffer, or
076     *             {@code -1} if there is no more data because the end of
077     *             the stream has been reached.
078     * @throws  IOException  if an I/O error occurs.
079     */
080    int read(byte[] b, int off, int len) throws IOException;
081
082    /**
083     * Reads one byte from this buffer. If the buffer is empty this method can
084     * throw a runtime exception. The exact type of runtime exception thrown
085     * by this method depends on implementation. This method returns
086     * {@code -1} if the end of content stream has been reached.
087     *
088     * @return one byte
089     */
090    int read() throws IOException;
091
092}