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