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      public void reset() {
58          this.endOfStream = false;
59          super.clear();
60      }
61  
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      public int read() throws IOException {
88          if (isEndOfStream()) {
89              return -1;
90          }
91          setOutputMode();
92          return this.buffer.get() & 0xff;
93      }
94  
95      public int read(final byte[] b, final int off, final int len) throws IOException {
96          if (isEndOfStream()) {
97              return -1;
98          }
99          if (b == null) {
100             return 0;
101         }
102         setOutputMode();
103         int chunk = len;
104         if (chunk > this.buffer.remaining()) {
105             chunk = this.buffer.remaining();
106         }
107         this.buffer.get(b, off, chunk);
108         return chunk;
109     }
110 
111     public int read(final byte[] b) throws IOException {
112         if (isEndOfStream()) {
113             return -1;
114         }
115         if (b == null) {
116             return 0;
117         }
118         return read(b, 0, b.length);
119     }
120 
121     public void shutdown() {
122         this.endOfStream = true;
123     }
124 
125 }