1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
44
45
46
47
48
49
50
51
52
53
54 @NotThreadSafe
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 public void consumeContent(
73 final ContentDecoder decoder,
74 final IOControl ioctrl) throws IOException {
75 this.buffer.consumeContent(decoder);
76 if (decoder.isCompleted()) {
77 this.finished = true;
78 }
79 }
80
81 public void finish() {
82 this.finished = true;
83 }
84
85 @Override
86 public void consumeContent() throws IOException {
87 }
88
89
90
91
92
93
94
95 @Override
96 public InputStream getContent() throws IOException {
97 if (!this.finished) {
98 throw new IllegalStateException("Entity content has not been fully received");
99 }
100 if (this.consumed) {
101 throw new IllegalStateException("Entity content has been consumed");
102 }
103 this.consumed = true;
104 return new ContentInputStream(this.buffer);
105 }
106
107 @Override
108 public boolean isRepeatable() {
109 return false;
110 }
111
112 @Override
113 public boolean isStreaming() {
114 return true;
115 }
116
117 @Override
118 public void writeTo(final OutputStream outstream) throws IOException {
119 if (outstream == null) {
120 throw new IllegalArgumentException("Output stream may not be null");
121 }
122 InputStream instream = getContent();
123 byte[] buffer = new byte[BUFFER_SIZE];
124 int l;
125
126 while ((l = instream.read(buffer)) != -1) {
127 outstream.write(buffer, 0, l);
128 }
129 }
130
131 }