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 * <p> If {@code len} is zero, then no bytes are read and 0 is returned. 80 * 81 * @param b the buffer into which the data is read. 82 * @param off the start offset in array {@code b} 83 * at which the data is written. 84 * @param len the maximum number of bytes to read. 85 * @param inputStream Input stream 86 * @return the total number of bytes read into the buffer, or 87 * {@code -1} if there is no more data because the end of 88 * the stream has been reached. 89 * @throws IOException if an I/O error occurs. 90 */ 91 int read(byte[] b, int off, int len, InputStream inputStream) throws IOException; 92 93 /** 94 * Reads some number of bytes from the session buffer and stores them into 95 * the buffer array {@code b}. The number of bytes actually read is 96 * returned as an integer. This method blocks until input data is 97 * available, end of file is detected, or an exception is thrown. 98 * 99 * @param b the buffer into which the data is read. 100 * @param inputStream Input stream 101 * @return the total number of bytes read into the buffer, or 102 * {@code -1} is there is no more data because the end of 103 * the stream has been reached. 104 * @throws IOException if an I/O error occurs. 105 */ 106 int read(byte[] b, InputStream inputStream) throws IOException; 107 108 /** 109 * Reads the next byte of data from this session buffer. The value byte is 110 * returned as an {@code int} in the range {@code 0} to 111 * {@code 255}. If no byte is available because the end of the stream 112 * has been reached, the value {@code -1} is returned. This method 113 * blocks until input data is available, the end of the stream is detected, 114 * or an exception is thrown. 115 * 116 * @param inputStream Input stream 117 * @return the next byte of data, or {@code -1} if the end of the 118 * stream is reached. 119 * @throws IOException if an I/O error occurs. 120 */ 121 int read(InputStream inputStream) throws IOException; 122 123 /** 124 * Reads a complete line of characters up to a line delimiter from this 125 * session buffer into the given line buffer. The number of chars actually 126 * read is returned as an integer. The line delimiter itself is discarded. 127 * If no char is available because the end of the stream has been reached, 128 * the value {@code -1} is returned. This method blocks until input 129 * data is available, end of file is detected, or an exception is thrown. 130 * <p> 131 * The choice of a char encoding and line delimiter sequence is up to the 132 * specific implementations of this interface. 133 * 134 * @param buffer the line buffer, one line of characters upon return 135 * @param inputStream Input stream 136 * @return the total number of bytes read into the buffer, or 137 * {@code -1} is there is no more data because the end of 138 * the stream has been reached. 139 * @throws IOException if an I/O error occurs. 140 */ 141 int readLine(CharArrayBuffer buffer, InputStream inputStream) throws IOException; 142 143 /** 144 * Returns {@link HttpTransportMetrics} for this session buffer. 145 * 146 * @return transport metrics. 147 */ 148 HttpTransportMetrics getMetrics(); 149 150 }