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.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
45
46
47
48
49
50
51
52
53
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 ioControl) 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
89
90
91
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
118 while ((l = inStream.read(buff)) != -1) {
119 outStream.write(buff, 0, l);
120 }
121 }
122
123 }