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