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.nio; 29 30 import java.io.IOException; 31 import java.nio.ByteBuffer; 32 import java.nio.channels.ReadableByteChannel; 33 import java.nio.channels.WritableByteChannel; 34 import java.nio.charset.CharacterCodingException; 35 36 import org.apache.hc.core5.util.CharArrayBuffer; 37 38 /** 39 * Session output buffer for non-blocking HTTP/1.1 connections. 40 * <p> 41 * This interface facilitates intermediate buffering of output data streamed out 42 * to a destination channel and provides provides methods for writing lines of text. 43 * </p> 44 * 45 * @since 4.0 46 */ 47 public interface SessionOutputBuffer { 48 49 /** 50 * Determines if the buffer contains data. 51 * 52 * @return {@code true} if there is data in the buffer, 53 * {@code false} otherwise. 54 */ 55 boolean hasData(); 56 57 /** 58 * Returns available space in the buffer. 59 * 60 * @return available space. 61 */ 62 int capacity(); 63 64 /** 65 * Returns the length of this buffer. 66 * 67 * @return buffer length. 68 */ 69 int length(); 70 71 /** 72 * Makes an attempt to flush the content of this buffer to the given 73 * destination {@link WritableByteChannel}. 74 * 75 * @param channel the destination channel. 76 * @return The number of bytes written, possibly zero. 77 * @throws IOException in case of an I/O error. 78 */ 79 int flush(WritableByteChannel channel) 80 throws IOException; 81 82 /** 83 * Copies content of the source buffer into this buffer. The capacity of 84 * the destination will be expanded in order to accommodate the entire 85 * content of the source buffer. 86 * 87 * @param src the source buffer. 88 */ 89 void write(ByteBuffer src); 90 91 /** 92 * Reads a sequence of bytes from the source channel into this buffer. 93 * 94 * @param src the source channel. 95 */ 96 void write(ReadableByteChannel src) 97 throws IOException; 98 99 /** 100 * Copies content of the source buffer into this buffer as one line of text 101 * including a line delimiter. The capacity of the destination will be 102 * expanded in order to accommodate the entire content of the source buffer. 103 * <p> 104 * The choice of a char encoding and line delimiter sequence is up to the 105 * specific implementations of this interface. 106 * 107 * @param src the source buffer. 108 */ 109 void writeLine(CharArrayBuffer src) 110 throws CharacterCodingException; 111 112 }