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 Args.notNegative(off, "offset");
60 Args.notNegative(len, "length");
61 Args.notNegative(off + len, "off + len");
62 Args.check(off <= b.length, "off %s cannot be greater then b.length %s ", off, b.length);
63 Args.check(off + len <= b.length, "off + len %s cannot be less then b.length %s ", off + len, b.length);
64 this.b = b;
65 this.off = off;
66 this.len = len;
67 }
68
69
70
71
72 public ByteArrayEntity(
73 final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding) {
74 this(b, off, len, contentType, contentEncoding, false);
75 }
76
77
78
79
80 public ByteArrayEntity(
81 final byte[] b, final ContentType contentType, final String contentEncoding, final boolean chunked) {
82 super(contentType, contentEncoding, chunked);
83 Args.notNull(b, "Source byte array");
84 this.b = b;
85 this.off = 0;
86 this.len = this.b.length;
87 }
88
89
90
91
92 public ByteArrayEntity(final byte[] b, final ContentType contentType, final String contentEncoding) {
93 this(b, contentType, contentEncoding, false);
94 }
95
96 public ByteArrayEntity(final byte[] b, final ContentType contentType, final boolean chunked) {
97 this(b, contentType, null, chunked);
98 }
99
100 public ByteArrayEntity(final byte[] b, final ContentType contentType) {
101 this(b, contentType, null, false);
102 }
103
104 public ByteArrayEntity(
105 final byte[] b, final int off, final int len, final ContentType contentType, final boolean chunked) {
106 this(b, off, len, contentType, null, chunked);
107 }
108
109 public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
110 this(b, off, len, contentType, null, false);
111 }
112
113 @Override
114 public final boolean isRepeatable() {
115 return true;
116 }
117
118 @Override
119 public final long getContentLength() {
120 return this.len;
121 }
122
123 @Override
124 public final InputStream getContent() {
125 return new ByteArrayInputStream(this.b, this.off, this.len);
126 }
127
128 @Override
129 public final void writeTo(final OutputStream outStream) throws IOException {
130 Args.notNull(outStream, "Output stream");
131 outStream.write(this.b, this.off, this.len);
132 outStream.flush();
133 }
134
135 @Override
136 public final boolean isStreaming() {
137 return false;
138 }
139
140 @Override
141 public final void close() throws IOException {
142 }
143
144 }