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