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.OutputStream; 32 33 import org.apache.hc.core5.util.CharArrayBuffer; 34 35 /** 36 * Session output buffer for blocking HTTP/1.1 connections. 37 * <p> 38 * This interface facilitates intermediate buffering of output data streamed out 39 * to an output stream and provides provides methods for writing lines of text. 40 * 41 * @since 4.0 42 */ 43 public interface SessionOutputBuffer { 44 45 /** 46 * Return 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 * Writes {@code len} bytes from the specified byte array 68 * starting at offset {@code off} to this session buffer. 69 * <p> 70 * If {@code off} is negative, or {@code len} is negative, or 71 * {@code off+len} is greater than the length of the array 72 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown. 73 * 74 * @param b the data. 75 * @param off the start offset in the data. 76 * @param len the number of bytes to write. 77 * @param outputStream the target OutputStream. 78 * @throws IOException if an I/O error occurs. 79 */ 80 void write(byte[] b, int off, int len, OutputStream outputStream) throws IOException; 81 82 /** 83 * Writes {@code b.length} bytes from the specified byte array 84 * to this session buffer. 85 * 86 * @param b the data. 87 * @param outputStream the target OutputStream. 88 * @throws IOException if an I/O error occurs. 89 */ 90 void write(byte[] b, OutputStream outputStream) throws IOException; 91 92 /** 93 * Writes the specified byte to this session buffer. 94 * 95 * @param b the {@code byte}. 96 * @param outputStream the target OutputStream. 97 * @throws IOException if an I/O error occurs. 98 */ 99 void write(int b, OutputStream outputStream) throws IOException; 100 101 /** 102 * Writes characters from the specified char array followed by a line 103 * delimiter to this session buffer. 104 * <p> 105 * The choice of a char encoding and line delimiter sequence is up to the 106 * specific implementations of this interface. 107 * 108 * @param buffer the buffer containing chars of the line. 109 * @param outputStream the target OutputStream. 110 * @throws IOException if an I/O error occurs. 111 */ 112 void writeLine(CharArrayBuffer buffer, OutputStream outputStream) throws IOException; 113 114 /** 115 * Flushes this session buffer and forces any buffered output bytes 116 * to be written out. The general contract of {@code flush} is 117 * that calling it is an indication that, if any bytes previously 118 * written have been buffered by the implementation of the output 119 * stream, such bytes should immediately be written to their 120 * intended destination. 121 * 122 * @param outputStream the target OutputStream. 123 * @throws IOException if an I/O error occurs. 124 */ 125 void flush(OutputStream outputStream) throws IOException; 126 127 /** 128 * Returns {@link HttpTransportMetrics} for this session buffer. 129 * 130 * @return transport metrics. 131 */ 132 HttpTransportMetrics getMetrics(); 133 134 }