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.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.nio.ByteBuffer;
35
36 import org.apache.http.entity.AbstractHttpEntity;
37 import org.apache.http.entity.ContentType;
38 import org.apache.http.nio.ContentEncoder;
39 import org.apache.http.nio.IOControl;
40 import org.apache.http.util.Args;
41
42
43
44
45
46
47
48 @SuppressWarnings("deprecation")
49 public class NByteArrayEntity extends AbstractHttpEntity
50 implements HttpAsyncContentProducer, ProducingNHttpEntity {
51
52 private final byte[] b;
53 private final int off, len;
54 private final ByteBuffer buf;
55
56
57
58 @Deprecated
59 protected final byte[] content;
60
61
62
63 @Deprecated
64 protected final ByteBuffer buffer;
65
66
67
68
69 public NByteArrayEntity(final byte[] b, final ContentType contentType) {
70 super();
71 Args.notNull(b, "Source byte array");
72 this.b = b;
73 this.off = 0;
74 this.len = b.length;
75 this.buf = ByteBuffer.wrap(b);
76 this.content = b;
77 this.buffer = this.buf;
78 if (contentType != null) {
79 setContentType(contentType.toString());
80 }
81 }
82
83
84
85
86 public NByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
87 super();
88 Args.notNull(b, "Source byte array");
89 if ((off < 0) || (off > b.length) || (len < 0) ||
90 ((off + len) < 0) || ((off + len) > b.length)) {
91 throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
92 }
93 this.b = b;
94 this.off = off;
95 this.len = len;
96 this.buf = ByteBuffer.wrap(b, off, len);
97 this.content = b;
98 this.buffer = this.buf;
99 if (contentType != null) {
100 setContentType(contentType.toString());
101 }
102 }
103
104 public NByteArrayEntity(final byte[] b) {
105 this(b, null);
106 }
107
108 public NByteArrayEntity(final byte[] b, final int off, final int len) {
109 this(b, off, len, null);
110 }
111
112
113
114
115
116
117 @Override
118 public void close() {
119 this.buf.rewind();
120 }
121
122
123
124
125
126
127 @Override
128 @Deprecated
129 public void finish() {
130 close();
131 }
132
133 @Override
134 public void produceContent(final ContentEncoder encoder, final IOControl ioControl)
135 throws IOException {
136 encoder.write(this.buf);
137 if(!this.buf.hasRemaining()) {
138 encoder.complete();
139 }
140 }
141
142 @Override
143 public long getContentLength() {
144 return this.len;
145 }
146
147 @Override
148 public boolean isRepeatable() {
149 return true;
150 }
151
152 @Override
153 public boolean isStreaming() {
154 return false;
155 }
156
157 @Override
158 public InputStream getContent() {
159 return new ByteArrayInputStream(this.b, this.off, this.len);
160 }
161
162 @Override
163 public void writeTo(final OutputStream outStream) throws IOException {
164 Args.notNull(outStream, "Output stream");
165 outStream.write(this.b, this.off, this.len);
166 outStream.flush();
167 }
168
169 }