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