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.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.http.util.Args;
36
37
38
39
40
41
42 public class ByteArrayEntity extends AbstractHttpEntity implements Cloneable {
43
44
45
46
47 @Deprecated
48 protected final byte[] content;
49 private final byte[] b;
50 private final int off, len;
51
52
53
54
55 public ByteArrayEntity(final byte[] b, final ContentType contentType) {
56 super();
57 Args.notNull(b, "Source byte array");
58 this.content = b;
59 this.b = b;
60 this.off = 0;
61 this.len = this.b.length;
62 if (contentType != null) {
63 setContentType(contentType.toString());
64 }
65 }
66
67
68
69
70 public ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
71 super();
72 Args.notNull(b, "Source byte array");
73 if ((off < 0) || (off > b.length) || (len < 0) ||
74 ((off + len) < 0) || ((off + len) > b.length)) {
75 throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
76 }
77 this.content = b;
78 this.b = b;
79 this.off = off;
80 this.len = len;
81 if (contentType != null) {
82 setContentType(contentType.toString());
83 }
84 }
85
86 public ByteArrayEntity(final byte[] b) {
87 this(b, null);
88 }
89
90 public ByteArrayEntity(final byte[] b, final int off, final int len) {
91 this(b, off, len, null);
92 }
93
94 @Override
95 public boolean isRepeatable() {
96 return true;
97 }
98
99 @Override
100 public long getContentLength() {
101 return this.len;
102 }
103
104 @Override
105 public InputStream getContent() {
106 return new ByteArrayInputStream(this.b, this.off, this.len);
107 }
108
109 @Override
110 public void writeTo(final OutputStream outStream) throws IOException {
111 Args.notNull(outStream, "Output stream");
112 outStream.write(this.b, this.off, this.len);
113 outStream.flush();
114 }
115
116
117
118
119
120
121
122 @Override
123 public boolean isStreaming() {
124 return false;
125 }
126
127 @Override
128 public Object clone() throws CloneNotSupportedException {
129 return super.clone();
130 }
131
132 }