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 import java.nio.ByteBuffer;
34 import java.nio.channels.Channels;
35 import java.nio.channels.ReadableByteChannel;
36
37 import org.apache.http.HttpEntity;
38 import org.apache.http.annotation.NotThreadSafe;
39 import org.apache.http.entity.HttpEntityWrapper;
40 import org.apache.http.nio.ContentEncoder;
41 import org.apache.http.nio.IOControl;
42
43
44
45
46
47
48
49
50
51 @NotThreadSafe
52 @Deprecated
53 public class NHttpEntityWrapper
54 extends HttpEntityWrapper implements ProducingNHttpEntity {
55
56 private final ReadableByteChannel channel;
57 private final ByteBuffer buffer;
58
59 public NHttpEntityWrapper(final HttpEntity httpEntity) throws IOException {
60 super(httpEntity);
61 this.channel = Channels.newChannel(httpEntity.getContent());
62 this.buffer = ByteBuffer.allocate(4096);
63 }
64
65
66
67
68 @Override
69 public InputStream getContent() throws IOException, UnsupportedOperationException {
70 throw new UnsupportedOperationException("Does not support blocking methods");
71 }
72
73 @Override
74 public boolean isStreaming() {
75 return true;
76 }
77
78
79
80
81 @Override
82 public void writeTo(final OutputStream out) throws IOException, UnsupportedOperationException {
83 throw new UnsupportedOperationException("Does not support blocking methods");
84 }
85
86
87
88
89
90
91
92 @Override
93 public void consumeContent() throws IOException {
94 finish();
95 }
96
97 public void produceContent(
98 final ContentEncoder encoder,
99 final IOControl ioctrl) throws IOException {
100 final int i = this.channel.read(this.buffer);
101 this.buffer.flip();
102 encoder.write(this.buffer);
103 final boolean buffering = this.buffer.hasRemaining();
104 this.buffer.compact();
105 if (i == -1 && !buffering) {
106 encoder.complete();
107 this.channel.close();
108 }
109 }
110
111 public void finish() {
112 try {
113 this.channel.close();
114 } catch (final IOException ignore) {
115 }
116 }
117
118 }