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