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.nio.reactor;
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.http.util.CharArrayBuffer;
37  
38  /**
39   * Session output buffer for non-blocking connections. This interface
40   * facilitates intermediate buffering of output data streamed out to
41   * a destination channel and writing data to the buffer from a source, usually
42   * {@link ByteBuffer} or {@link ReadableByteChannel}. This interface also
43   * provides methods for writing lines of text.
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 the length of this buffer.
59       *
60       * @return buffer length.
61       */
62      int length();
63  
64      /**
65       * Makes an attempt to flush the content of this buffer to the given
66       * destination {@link WritableByteChannel}.
67       *
68       * @param channel the destination channel.
69       * @return The number of bytes written, possibly zero.
70       * @throws IOException in case of an I/O error.
71       */
72      int flush(WritableByteChannel channel)
73          throws IOException;
74  
75      /**
76       * Copies content of the source buffer into this buffer. The capacity of
77       * the destination will be expanded in order to accommodate the entire
78       * content of the source buffer.
79       *
80       * @param src the source buffer.
81       */
82      void write(ByteBuffer src);
83  
84      /**
85       * Reads a sequence of bytes from the source channel into this buffer.
86       *
87       * @param src the source channel.
88       */
89      void write(ReadableByteChannel src)
90          throws IOException;
91  
92      /**
93       * Copies content of the source buffer into this buffer as one line of text
94       * including a line delimiter. The capacity of the destination will be
95       * expanded in order to accommodate the entire content of the source buffer.
96       * <p>
97       * The choice of a char encoding and line delimiter sequence is up to the
98       * specific implementations of this interface.
99       *
100      * @param src the source buffer.
101      */
102     void writeLine(CharArrayBuffer src)
103         throws CharacterCodingException;
104 
105     /**
106      * Copies content of the given string into this buffer as one line of text
107      * including a line delimiter.
108      * The capacity of the destination will be expanded in order to accommodate
109      * the entire string.
110      * <p>
111      * The choice of a char encoding and line delimiter sequence is up to the
112      * specific implementations of this interface.
113      *
114      * @param s the string.
115      */
116     void writeLine(String s)
117         throws IOException;
118 
119 }