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  package org.apache.http.nio.util;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.annotation.NotThreadSafe;
32  import org.apache.http.nio.ContentDecoder;
33  
34  /**
35   * Basic implementation of the {@link ContentInputBuffer} interface.
36   * <p>
37   * This class is not thread safe.
38   *
39   * @since 4.0
40   */
41  @NotThreadSafe
42  public class SimpleInputBuffer extends ExpandableBuffer implements ContentInputBuffer {
43  
44      private boolean endOfStream = false;
45  
46      public SimpleInputBuffer(int buffersize, final ByteBufferAllocator allocator) {
47          super(buffersize, allocator);
48      }
49  
50      public void reset() {
51          this.endOfStream = false;
52          super.clear();
53      }
54  
55      public int consumeContent(final ContentDecoder decoder) throws IOException {
56          setInputMode();
57          int totalRead = 0;
58          int bytesRead;
59          while ((bytesRead = decoder.read(this.buffer)) != -1) {
60              if (bytesRead == 0) {
61                  if (!this.buffer.hasRemaining()) {
62                      expand();
63                  } else {
64                      break;
65                  }
66              } else {
67                  totalRead += bytesRead;
68              }
69          }
70          if (bytesRead == -1 || decoder.isCompleted()) {
71              this.endOfStream = true;
72          }
73          return totalRead;
74      }
75  
76      public boolean isEndOfStream() {
77          return !hasData() && this.endOfStream;
78      }
79  
80      public int read() throws IOException {
81          if (isEndOfStream()) {
82              return -1;
83          }
84          setOutputMode();
85          return this.buffer.get() & 0xff;
86      }
87  
88      public int read(final byte[] b, int off, int len) throws IOException {
89          if (isEndOfStream()) {
90              return -1;
91          }
92          if (b == null) {
93              return 0;
94          }
95          setOutputMode();
96          int chunk = len;
97          if (chunk > this.buffer.remaining()) {
98              chunk = this.buffer.remaining();
99          }
100         this.buffer.get(b, off, chunk);
101         return chunk;
102     }
103 
104     public int read(final byte[] b) throws IOException {
105         if (isEndOfStream()) {
106             return -1;
107         }
108         if (b == null) {
109             return 0;
110         }
111         return read(b, 0, b.length);
112     }
113 
114     public void shutdown() {
115         this.endOfStream = true;
116     }
117 
118 }