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.CharsetDecoder;
38  import java.nio.charset.CoderResult;
39  
40  import org.apache.http.annotation.NotThreadSafe;
41  import org.apache.http.nio.reactor.SessionInputBuffer;
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.HttpParams;
46  import org.apache.http.params.HttpProtocolParams;
47  import org.apache.http.protocol.HTTP;
48  import org.apache.http.util.CharArrayBuffer;
49  
50  /**
51   * Default implementation of {@link SessionInputBuffer} based on
52   * the {@link ExpandableBuffer} class.
53   * <p>
54   * The following parameters can be used to customize the behavior of this
55   * class:
56   * <ul>
57   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
58   * </ul>
59   *
60   * @since 4.0
61   */
62  @NotThreadSafe
63  public class SessionInputBufferImpl extends ExpandableBuffer implements SessionInputBuffer {
64  
65      private CharBuffer charbuffer = null;
66      private Charset charset = null;
67      private CharsetDecoder chardecoder = null;
68  
69      public SessionInputBufferImpl(
70              int buffersize,
71              int linebuffersize,
72              final ByteBufferAllocator allocator,
73              final HttpParams params) {
74          super(buffersize, allocator);
75          this.charbuffer = CharBuffer.allocate(linebuffersize);
76          this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params));
77          this.chardecoder = this.charset.newDecoder();
78          this.chardecoder.onMalformedInput(HttpProtocolParams.getMalformedInputAction(params));
79          this.chardecoder.onUnmappableCharacter(HttpProtocolParams.getUnmappableInputAction(params));
80      }
81  
82      public SessionInputBufferImpl(
83              int buffersize,
84              int linebuffersize,
85              final HttpParams params) {
86          this(buffersize, linebuffersize, new HeapByteBufferAllocator(), params);
87      }
88  
89      public int fill(final ReadableByteChannel channel) throws IOException {
90          if (channel == null) {
91              throw new IllegalArgumentException("Channel may not be null");
92          }
93          setInputMode();
94          if (!this.buffer.hasRemaining()) {
95              expand();
96          }
97          int readNo = channel.read(this.buffer);
98          return readNo;
99      }
100 
101     public int read() {
102         setOutputMode();
103         return this.buffer.get() & 0xff;
104     }
105 
106     public int read(final ByteBuffer dst, int maxLen) {
107         if (dst == null) {
108             return 0;
109         }
110         setOutputMode();
111         int len = Math.min(dst.remaining(), maxLen);
112         int chunk = Math.min(this.buffer.remaining(), len);
113         if (this.buffer.remaining() > chunk) {
114             int oldLimit = this.buffer.limit();
115             int newLimit = this.buffer.position() + chunk;
116             this.buffer.limit(newLimit);
117             dst.put(this.buffer);
118             this.buffer.limit(oldLimit);
119             return len;
120         } else {
121             dst.put(this.buffer);
122         }
123         return chunk;
124     }
125 
126     public int read(final ByteBuffer dst) {
127         if (dst == null) {
128             return 0;
129         }
130         return read(dst, dst.remaining());
131     }
132 
133     public int read(final WritableByteChannel dst, int maxLen) throws IOException {
134         if (dst == null) {
135             return 0;
136         }
137         setOutputMode();
138         int bytesRead;
139         if (this.buffer.remaining() > maxLen) {
140             int oldLimit = this.buffer.limit();
141             int newLimit = oldLimit - (this.buffer.remaining() - maxLen);
142             this.buffer.limit(newLimit);
143             bytesRead = dst.write(this.buffer);
144             this.buffer.limit(oldLimit);
145         } else {
146             bytesRead = dst.write(this.buffer);
147         }
148         return bytesRead;
149     }
150 
151     public int read(final WritableByteChannel dst) throws IOException {
152         if (dst == null) {
153             return 0;
154         }
155         setOutputMode();
156         return dst.write(this.buffer);
157     }
158 
159     public boolean readLine(
160             final CharArrayBuffer linebuffer,
161             boolean endOfStream) throws CharacterCodingException {
162 
163         setOutputMode();
164         // See if there is LF char present in the buffer
165         int pos = -1;
166         boolean hasLine = false;
167         for (int i = this.buffer.position(); i < this.buffer.limit(); i++) {
168             int b = this.buffer.get(i);
169             if (b == HTTP.LF) {
170                 hasLine = true;
171                 pos = i + 1;
172                 break;
173             }
174         }
175         if (!hasLine) {
176             if (endOfStream && this.buffer.hasRemaining()) {
177                 // No more data. Get the rest
178                 pos = this.buffer.limit();
179             } else {
180                 // Either no complete line present in the buffer
181                 // or no more data is expected
182                 return false;
183             }
184         }
185         int origLimit = this.buffer.limit();
186         this.buffer.limit(pos);
187 
188         int len = this.buffer.limit() - this.buffer.position();
189         // Ensure capacity of len assuming ASCII as the most likely charset
190         linebuffer.ensureCapacity(len);
191 
192         this.chardecoder.reset();
193 
194         for (;;) {
195             CoderResult result = this.chardecoder.decode(
196                     this.buffer,
197                     this.charbuffer,
198                     true);
199             if (result.isError()) {
200                 result.throwException();
201             }
202             if (result.isOverflow()) {
203                 this.charbuffer.flip();
204                 linebuffer.append(
205                         this.charbuffer.array(),
206                         this.charbuffer.position(),
207                         this.charbuffer.remaining());
208                 this.charbuffer.clear();
209             }
210             if (result.isUnderflow()) {
211                 break;
212             }
213         }
214         this.buffer.limit(origLimit);
215 
216         // flush the decoder
217         this.chardecoder.flush(this.charbuffer);
218         this.charbuffer.flip();
219         // append the decoded content to the line buffer
220         if (this.charbuffer.hasRemaining()) {
221             linebuffer.append(
222                     this.charbuffer.array(),
223                     this.charbuffer.position(),
224                     this.charbuffer.remaining());
225         }
226 
227         // discard LF if found
228         int l = linebuffer.length();
229         if (l > 0) {
230             if (linebuffer.charAt(l - 1) == HTTP.LF) {
231                 l--;
232                 linebuffer.setLength(l);
233             }
234             // discard CR if found
235             if (l > 0) {
236                 if (linebuffer.charAt(l - 1) == HTTP.CR) {
237                     l--;
238                     linebuffer.setLength(l);
239                 }
240             }
241         }
242         return true;
243     }
244 
245     public String readLine(boolean endOfStream) throws CharacterCodingException {
246         CharArrayBuffer charbuffer = new CharArrayBuffer(64);
247         boolean found = readLine(charbuffer, endOfStream);
248         if (found) {
249             return charbuffer.toString();
250         } else {
251             return null;
252         }
253     }
254 
255 }