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</code> 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</code> 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</code> is negative, or <code>len</code> is negative, or
55       * <code>off+len</code> is greater than the length of the array
56       * <code>b</code>, then an <code>IndexOutOfBoundsException</code> 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</code>
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</code> if there is no more data because the end of
65       *             the stream has been reached.
66       * @exception  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</code>. 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</code> is there is no more data because the end of
79       *             the stream has been reached.
80       * @exception  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</code> in the range <code>0</code> to
87       * <code>255</code>. If no byte is available because the end of the stream
88       * has been reached, the value <code>-1</code> 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</code> if the end of the
93       *             stream is reached.
94       * @exception  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</code> 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      * @exception  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</code> 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      * @exception  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</code> this method blocks indefinitely.
133      *
134      * @param timeout in milliseconds.
135      * @return <code>true</code> if some data is available in the session
136      *   buffer or <code>false</code> otherwise.
137      * @exception  IOException  if an I/O error occurs.
138      */
139     boolean isDataAvailable(int timeout) throws IOException;
140 
141     /**
142      * Returns {@link HttpTransportMetrics} for this session buffer.
143      *
144      * @return transport metrics.
145      */
146     HttpTransportMetrics getMetrics();
147 
148 }