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.nio.entity;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  
34  import org.apache.http.HttpEntity;
35  import org.apache.http.entity.HttpEntityWrapper;
36  import org.apache.http.nio.ContentDecoder;
37  import org.apache.http.nio.IOControl;
38  import org.apache.http.nio.util.ByteBufferAllocator;
39  import org.apache.http.nio.util.SimpleInputBuffer;
40  import org.apache.http.util.Args;
41  import org.apache.http.util.Asserts;
42  
43  /**
44   * A {@link ConsumingNHttpEntity} that consumes content into a buffer. The
45   * content can be retrieved as an InputStream via
46   * {@link HttpEntity#getContent()}, or written to an output stream via
47   * {@link HttpEntity#writeTo(OutputStream)}.
48   *
49   * @since 4.0
50   *
51   * @deprecated use (4.2)
52   *  {@link org.apache.http.nio.protocol.BasicAsyncRequestProducer}
53   *  or {@link org.apache.http.nio.protocol.BasicAsyncResponseProducer}
54   */
55  @Deprecated
56  public class BufferingNHttpEntity extends HttpEntityWrapper implements
57          ConsumingNHttpEntity {
58  
59      private final static int BUFFER_SIZE = 2048;
60  
61      private final SimpleInputBuffer buffer;
62      private boolean finished;
63      private boolean consumed;
64  
65      public BufferingNHttpEntity(
66              final HttpEntity httpEntity,
67              final ByteBufferAllocator allocator) {
68          super(httpEntity);
69          this.buffer = new SimpleInputBuffer(BUFFER_SIZE, allocator);
70      }
71  
72      @Override
73      public void consumeContent(
74              final ContentDecoder decoder,
75              final IOControl ioctrl) throws IOException {
76          this.buffer.consumeContent(decoder);
77          if (decoder.isCompleted()) {
78              this.finished = true;
79          }
80      }
81  
82      @Override
83      public void finish() {
84          this.finished = true;
85      }
86  
87      /**
88       * Obtains entity's content as {@link InputStream}.
89       *
90       *  @throws IllegalStateException if content of the entity has not been
91       *    fully received or has already been consumed.
92       */
93      @Override
94      public InputStream getContent() throws IOException {
95          Asserts.check(this.finished, "Entity content has not been fully received");
96          Asserts.check(!this.consumed, "Entity content has been consumed");
97          this.consumed = true;
98          return new ContentInputStream(this.buffer);
99      }
100 
101     @Override
102     public boolean isRepeatable() {
103         return false;
104     }
105 
106     @Override
107     public boolean isStreaming() {
108         return true;
109     }
110 
111     @Override
112     public void writeTo(final OutputStream outstream) throws IOException {
113         Args.notNull(outstream, "Output stream");
114         final InputStream instream = getContent();
115         final byte[] buff = new byte[BUFFER_SIZE];
116         int l;
117         // consume until EOF
118         while ((l = instream.read(buff)) != -1) {
119             outstream.write(buff, 0, l);
120         }
121     }
122 
123 }