View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.nio.util;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.nio.ContentDecoder;
33  
34  /**
35   * Buffer for storing content streamed out from a {@link ContentDecoder}.
36   *
37   * @since 4.0
38   */
39  public interface ContentInputBuffer {
40  
41      /**
42       * Reads content from the given {@link ContentDecoder} and stores it in
43       * this buffer.
44       *
45       * @param decoder the content decoder.
46       * @return number of bytes read.
47       * @throws IOException in case of an I/O error.
48       */
49      int consumeContent(ContentDecoder decoder) throws IOException;
50  
51      /**
52       * Resets the buffer by clearing its state and stored content.
53       */
54      void reset();
55  
56      /**
57       * Reads up to <code>len</code> bytes of data from this buffer into
58       * an array of bytes. The exact number of bytes read depends how many bytes
59       * are stored in the buffer.
60       *
61       * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
62       * <code>off+len</code> is greater than the length of the array
63       * <code>b</code>, this method can throw a runtime exception. The exact type
64       * of runtime exception thrown by this method depends on implementation.
65       * This method returns <code>-1</code> if the end of content stream has been
66       * reached.
67       *
68       * @param      b     the buffer into which the data is read.
69       * @param      off   the start offset in array <code>b</code>
70       *                   at which the data is written.
71       * @param      len   the maximum number of bytes to read.
72       * @return     the total number of bytes read into the buffer, or
73       *             <code>-1</code> if there is no more data because the end of
74       *             the stream has been reached.
75       * @exception  IOException  if an I/O error occurs.
76       */
77      int read(byte[] b, int off, int len) throws IOException;
78  
79      /**
80       * Reads one byte from this buffer. If the buffer is empty this method can
81       * throw a runtime exception. The exact type of runtime exception thrown
82       * by this method depends on implementation. This method returns
83       * <code>-1</code> if the end of content stream has been reached.
84       *
85       * @return one byte
86       */
87      int read() throws IOException;
88  
89  }