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