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