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(final int buffersize, final ByteBufferAllocator allocator) {
47          super(buffersize, allocator);
48      }
49  
50      /**
51       * @since 4.3
52       */
53      public SimpleInputBuffer(final int buffersize) {
54          this(buffersize, HeapByteBufferAllocator.INSTANCE);
55      }
56  
57      @Override
58      public void reset() {
59          this.endOfStream = false;
60          super.clear();
61      }
62  
63      @Override
64      public int consumeContent(final ContentDecoder decoder) throws IOException {
65          setInputMode();
66          int totalRead = 0;
67          int bytesRead;
68          while ((bytesRead = decoder.read(this.buffer)) != -1) {
69              if (bytesRead == 0) {
70                  if (!this.buffer.hasRemaining()) {
71                      expand();
72                  } else {
73                      break;
74                  }
75              } else {
76                  totalRead += bytesRead;
77              }
78          }
79          if (bytesRead == -1 || decoder.isCompleted()) {
80              this.endOfStream = true;
81          }
82          return totalRead;
83      }
84  
85      public boolean isEndOfStream() {
86          return !hasData() && this.endOfStream;
87      }
88  
89      @Override
90      public int read() throws IOException {
91          if (isEndOfStream()) {
92              return -1;
93          }
94          setOutputMode();
95          return this.buffer.get() & 0xff;
96      }
97  
98      @Override
99      public int read(final byte[] b, final int off, final int len) throws IOException {
100         if (isEndOfStream()) {
101             return -1;
102         }
103         if (b == null) {
104             return 0;
105         }
106         setOutputMode();
107         int chunk = len;
108         if (chunk > this.buffer.remaining()) {
109             chunk = this.buffer.remaining();
110         }
111         this.buffer.get(b, off, chunk);
112         return chunk;
113     }
114 
115     public int read(final byte[] b) throws IOException {
116         if (isEndOfStream()) {
117             return -1;
118         }
119         if (b == null) {
120             return 0;
121         }
122         return read(b, 0, b.length);
123     }
124 
125     public void shutdown() {
126         this.endOfStream = true;
127     }
128 
129 }