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.io;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.util.CharArrayBuffer;
33  
34  /**
35   * Session input buffer for blocking connections. This interface is similar to
36   * InputStream class, but it also provides methods for reading lines of text.
37   * <p>
38   * Implementing classes are also expected to manage intermediate data buffering
39   * for optimal input performance.
40   *
41   * @since 4.0
42   */
43  public interface SessionInputBuffer {
44  
45      /**
46       * Reads up to {@code len} bytes of data from the session buffer into
47       * an array of bytes.  An attempt is made to read as many as
48       * {@code len} bytes, but a smaller number may be read, possibly
49       * zero. The number of bytes actually read is returned as an integer.
50       *
51       * <p> This method blocks until input data is available, end of file is
52       * detected, or an exception is thrown.
53       *
54       * <p> If {@code off} is negative, or {@code len} is negative, or
55       * {@code off+len} is greater than the length of the array
56       * {@code b}, then an {@code IndexOutOfBoundsException} is
57       * thrown.
58       *
59       * @param      b     the buffer into which the data is read.
60       * @param      off   the start offset in array {@code b}
61       *                   at which the data is written.
62       * @param      len   the maximum number of bytes to read.
63       * @return     the total number of bytes read into the buffer, or
64       *             {@code -1} if there is no more data because the end of
65       *             the stream has been reached.
66       * @throws  IOException  if an I/O error occurs.
67       */
68      int read(byte[] b, int off, int len) throws IOException;
69  
70      /**
71       * Reads some number of bytes from the session buffer and stores them into
72       * the buffer array {@code b}. The number of bytes actually read is
73       * returned as an integer.  This method blocks until input data is
74       * available, end of file is detected, or an exception is thrown.
75       *
76       * @param      b   the buffer into which the data is read.
77       * @return     the total number of bytes read into the buffer, or
78       *             {@code -1} is there is no more data because the end of
79       *             the stream has been reached.
80       * @throws  IOException  if an I/O error occurs.
81       */
82      int read(byte[] b) throws IOException;
83  
84      /**
85       * Reads the next byte of data from this session buffer. The value byte is
86       * returned as an {@code int} in the range {@code 0} to
87       * {@code 255}. If no byte is available because the end of the stream
88       * has been reached, the value {@code -1} is returned. This method
89       * blocks until input data is available, the end of the stream is detected,
90       * or an exception is thrown.
91       *
92       * @return     the next byte of data, or {@code -1} if the end of the
93       *             stream is reached.
94       * @throws  IOException  if an I/O error occurs.
95       */
96      int read() throws IOException;
97  
98      /**
99       * Reads a complete line of characters up to a line delimiter from this
100      * session buffer into the given line buffer. The number of chars actually
101      * read is returned as an integer. The line delimiter itself is discarded.
102      * If no char is available because the end of the stream has been reached,
103      * the value {@code -1} is returned. This method blocks until input
104      * data is available, end of file is detected, or an exception is thrown.
105      * <p>
106      * The choice of a char encoding and line delimiter sequence is up to the
107      * specific implementations of this interface.
108      *
109      * @param      buffer   the line buffer.
110      * @return     one line of characters
111      * @throws  IOException  if an I/O error occurs.
112      */
113     int readLine(CharArrayBuffer buffer) throws IOException;
114 
115     /**
116      * Reads a complete line of characters up to a line delimiter from this
117      * session buffer. The line delimiter itself is discarded. If no char is
118      * available because the end of the stream has been reached,
119      * {@code null} is returned. This method blocks until input data is
120      * available, end of file is detected, or an exception is thrown.
121      * <p>
122      * The choice of a char encoding and line delimiter sequence is up to the
123      * specific implementations of this interface.
124      *
125      * @return HTTP line as a string
126      * @throws  IOException  if an I/O error occurs.
127      */
128     String readLine() throws IOException;
129 
130     /** Blocks until some data becomes available in the session buffer or the
131      * given timeout period in milliseconds elapses. If the timeout value is
132      * {@code 0} this method blocks indefinitely.
133      *
134      * @param timeout in milliseconds.
135      * @return {@code true} if some data is available in the session
136      *   buffer or {@code false} otherwise.
137      * @throws  IOException  if an I/O error occurs.
138      *
139      * @deprecated (4.3) do not use. This function should be provided at the
140      *   connection level
141      */
142     @Deprecated
143     boolean isDataAvailable(int timeout) throws IOException;
144 
145     /**
146      * Returns {@link HttpTransportMetrics} for this session buffer.
147      *
148      * @return transport metrics.
149      */
150     HttpTransportMetrics getMetrics();
151 
152 }