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.impl.nio.reactor;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.CharBuffer;
33  import java.nio.channels.ReadableByteChannel;
34  import java.nio.channels.WritableByteChannel;
35  import java.nio.charset.CharacterCodingException;
36  import java.nio.charset.Charset;
37  import java.nio.charset.CharsetEncoder;
38  import java.nio.charset.CoderResult;
39  import java.nio.charset.CodingErrorAction;
40  
41  import org.apache.http.nio.reactor.SessionOutputBuffer;
42  import org.apache.http.nio.util.ByteBufferAllocator;
43  import org.apache.http.nio.util.ExpandableBuffer;
44  import org.apache.http.nio.util.HeapByteBufferAllocator;
45  import org.apache.http.params.CoreProtocolPNames;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.protocol.HTTP;
48  import org.apache.http.util.Args;
49  import org.apache.http.util.CharArrayBuffer;
50  import org.apache.http.util.CharsetUtils;
51  
52  /**
53   * Default implementation of {@link SessionOutputBuffer} based on
54   * the {@link ExpandableBuffer} class.
55   *
56   * @since 4.0
57   */
58  @SuppressWarnings("deprecation")
59  public class SessionOutputBufferImpl extends ExpandableBuffer implements SessionOutputBuffer {
60  
61      private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF};
62  
63      private final CharsetEncoder charencoder;
64      private final int lineBuffersize;
65  
66      private CharBuffer charbuffer;
67  
68      /**
69       *  Creates SessionOutputBufferImpl instance.
70       *
71       * @param buffersize input buffer size
72       * @param lineBuffersize buffer size for line operations. Has effect only if
73       *   {@code charencoder} is not {@code null}.
74       * @param charencoder charencoder to be used for encoding HTTP protocol elements.
75       *   If {@code null} simple type cast will be used for char to byte conversion.
76       * @param allocator memory allocator.
77       *   If {@code null} {@link HeapByteBufferAllocator#INSTANCE} will be used.
78       *
79       * @since 4.3
80       */
81      public SessionOutputBufferImpl(
82              final int buffersize,
83              final int lineBuffersize,
84              final CharsetEncoder charencoder,
85              final ByteBufferAllocator allocator) {
86          super(buffersize, allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE);
87          this.lineBuffersize = Args.positive(lineBuffersize, "Line buffer size");
88          this.charencoder = charencoder;
89      }
90  
91      /**
92       * @deprecated (4.3) use
93       *   {@link SessionOutputBufferImpl#SessionOutputBufferImpl(int, int, CharsetEncoder,
94       *     ByteBufferAllocator)}
95       */
96      @Deprecated
97      public SessionOutputBufferImpl(
98              final int buffersize,
99              final int lineBuffersize,
100             final ByteBufferAllocator allocator,
101             final HttpParams params) {
102         super(buffersize, allocator);
103         this.lineBuffersize = Args.positive(lineBuffersize, "Line buffer size");
104         final String charsetName = (String) params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
105         final Charset charset = CharsetUtils.lookup(charsetName);
106         if (charset != null) {
107             this.charencoder = charset.newEncoder();
108             final CodingErrorAction a1 = (CodingErrorAction) params.getParameter(
109                     CoreProtocolPNames.HTTP_MALFORMED_INPUT_ACTION);
110             this.charencoder.onMalformedInput(a1 != null ? a1 : CodingErrorAction.REPORT);
111             final CodingErrorAction a2 = (CodingErrorAction) params.getParameter(
112                     CoreProtocolPNames.HTTP_UNMAPPABLE_INPUT_ACTION);
113             this.charencoder.onUnmappableCharacter(a2 != null? a2 : CodingErrorAction.REPORT);
114         } else {
115             this.charencoder = null;
116         }
117     }
118 
119     /**
120      * @deprecated (4.3) use
121      *   {@link SessionOutputBufferImpl#SessionOutputBufferImpl(int, int, Charset)}
122      */
123     @Deprecated
124     public SessionOutputBufferImpl(
125             final int buffersize,
126             final int linebuffersize,
127             final HttpParams params) {
128         this(buffersize, linebuffersize, HeapByteBufferAllocator.INSTANCE, params);
129     }
130 
131     /**
132      * @since 4.3
133      */
134     public SessionOutputBufferImpl(final int buffersize) {
135         this(buffersize, 256, null, HeapByteBufferAllocator.INSTANCE);
136     }
137 
138     /**
139      * @since 4.3
140      */
141     public SessionOutputBufferImpl(
142             final int buffersize,
143             final int linebuffersize,
144             final Charset charset) {
145         this(buffersize, linebuffersize,
146                 charset != null ? charset.newEncoder() : null, HeapByteBufferAllocator.INSTANCE);
147     }
148 
149     /**
150      * @since 4.3
151      */
152     public SessionOutputBufferImpl(
153             final int buffersize,
154             final int linebuffersize) {
155         this(buffersize, linebuffersize, null, HeapByteBufferAllocator.INSTANCE);
156     }
157 
158     public void reset(final HttpParams params) {
159         clear();
160     }
161 
162     @Override
163     public int flush(final WritableByteChannel channel) throws IOException {
164         Args.notNull(channel, "Channel");
165         setOutputMode();
166         return channel.write(this.buffer);
167     }
168 
169     @Override
170     public void write(final ByteBuffer src) {
171         if (src == null) {
172             return;
173         }
174         setInputMode();
175         final int requiredCapacity = this.buffer.position() + src.remaining();
176         ensureCapacity(requiredCapacity);
177         this.buffer.put(src);
178     }
179 
180     @Override
181     public void write(final ReadableByteChannel src) throws IOException {
182         if (src == null) {
183             return;
184         }
185         setInputMode();
186         src.read(this.buffer);
187     }
188 
189     private void write(final byte[] b) {
190         if (b == null) {
191             return;
192         }
193         setInputMode();
194         final int off = 0;
195         final int len = b.length;
196         final int requiredCapacity = this.buffer.position() + len;
197         ensureCapacity(requiredCapacity);
198         this.buffer.put(b, off, len);
199     }
200 
201     private void writeCRLF() {
202         write(CRLF);
203     }
204 
205     @Override
206     public void writeLine(final CharArrayBuffer linebuffer) throws CharacterCodingException {
207         if (linebuffer == null) {
208             return;
209         }
210         setInputMode();
211         // Do not bother if the buffer is empty
212         if (linebuffer.length() > 0 ) {
213             if (this.charencoder == null) {
214                 final int requiredCapacity = this.buffer.position() + linebuffer.length();
215                 ensureCapacity(requiredCapacity);
216                 if (this.buffer.hasArray()) {
217                     final byte[] b = this.buffer.array();
218                     final int len = linebuffer.length();
219                     final int off = this.buffer.position();
220                     for (int i = 0; i < len; i++) {
221                         b[off + i]  = (byte) linebuffer.charAt(i);
222                     }
223                     this.buffer.position(off + len);
224                 } else {
225                     for (int i = 0; i < linebuffer.length(); i++) {
226                         this.buffer.put((byte) linebuffer.charAt(i));
227                     }
228                 }
229             } else {
230                 if (this.charbuffer == null) {
231                     this.charbuffer = CharBuffer.allocate(this.lineBuffersize);
232                 }
233                 this.charencoder.reset();
234                 // transfer the string in small chunks
235                 int remaining = linebuffer.length();
236                 int offset = 0;
237                 while (remaining > 0) {
238                     int l = this.charbuffer.remaining();
239                     boolean eol = false;
240                     if (remaining <= l) {
241                         l = remaining;
242                         // terminate the encoding process
243                         eol = true;
244                     }
245                     this.charbuffer.put(linebuffer.buffer(), offset, l);
246                     this.charbuffer.flip();
247 
248                     boolean retry = true;
249                     while (retry) {
250                         final CoderResult result = this.charencoder.encode(this.charbuffer, this.buffer, eol);
251                         if (result.isError()) {
252                             result.throwException();
253                         }
254                         if (result.isOverflow()) {
255                             expand();
256                         }
257                         retry = !result.isUnderflow();
258                     }
259                     this.charbuffer.compact();
260                     offset += l;
261                     remaining -= l;
262                 }
263                 // flush the encoder
264                 boolean retry = true;
265                 while (retry) {
266                     final CoderResult result = this.charencoder.flush(this.buffer);
267                     if (result.isError()) {
268                         result.throwException();
269                     }
270                     if (result.isOverflow()) {
271                         expand();
272                     }
273                     retry = !result.isUnderflow();
274                 }
275             }
276         }
277         writeCRLF();
278     }
279 
280     @Override
281     public void writeLine(final String s) throws IOException {
282         if (s == null) {
283             return;
284         }
285         if (s.length() > 0) {
286             final CharArrayBuffer tmp = new CharArrayBuffer(s.length());
287             tmp.append(s);
288             writeLine(tmp);
289         } else {
290             write(CRLF);
291         }
292     }
293 
294 }