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.hc.core5.http.io.entity;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 import org.apache.hc.core5.annotation.Contract;
36 import org.apache.hc.core5.annotation.ThreadingBehavior;
37 import org.apache.hc.core5.http.ContentType;
38 import org.apache.hc.core5.util.Args;
39
40
41
42
43
44
45 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
46 public class ByteArrayEntity extends AbstractHttpEntity {
47
48 private final byte[] b;
49 private final int off, len;
50
51
52
53
54 public ByteArrayEntity(
55 final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding,
56 final boolean chunked) {
57 super(contentType, contentEncoding, chunked);
58 Args.notNull(b, "Source byte array");
59 if ((off < 0) || (off > b.length) || (len < 0) ||
60 ((off + len) < 0) || ((off + len) > b.length)) {
61 throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
62 }
63 this.b = b;
64 this.off = off;
65 this.len = len;
66 }
67
68
69
70
71 public ByteArrayEntity(
72 final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding) {
73 this(b, off, len, contentType, contentEncoding, false);
74 }
75
76
77
78
79 public ByteArrayEntity(
80 final byte[] b, final ContentType contentType, final String contentEncoding, final boolean chunked) {
81 super(contentType, contentEncoding, chunked);
82 Args.notNull(b, "Source byte array");
83 this.b = b;
84 this.off = 0;
85 this.len = this.b.length;
86 }
87
88
89
90
91 public ByteArrayEntity(final byte[] b, final ContentType contentType, final String contentEncoding) {
92 this(b, contentType, contentEncoding, false);
93 }
94
95 public ByteArrayEntity(final byte[] b, final ContentType contentType, final boolean chunked) {
96 this(b, contentType, null, chunked);
97 }
98
99 public ByteArrayEntity(final byte[] b, final ContentType contentType) {
100 this(b, contentType, null, false);
101 }
102
103 public ByteArrayEntity(
104 final byte[] b, final int off, final int len, final ContentType contentType, final boolean chunked) {
105 this(b, off, len, contentType, null, chunked);
106 }
107
108 public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
109 this(b, off, len, contentType, null, false);
110 }
111
112 @Override
113 public final boolean isRepeatable() {
114 return true;
115 }
116
117 @Override
118 public final long getContentLength() {
119 return this.len;
120 }
121
122 @Override
123 public final InputStream getContent() {
124 return new ByteArrayInputStream(this.b, this.off, this.len);
125 }
126
127 @Override
128 public final void writeTo(final OutputStream outStream) throws IOException {
129 Args.notNull(outStream, "Output stream");
130 outStream.write(this.b, this.off, this.len);
131 outStream.flush();
132 }
133
134 @Override
135 public final boolean isStreaming() {
136 return false;
137 }
138
139 @Override
140 public final void close() throws IOException {
141 }
142
143 }