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.client.fluent;
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.entity.AbstractHttpEntity;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.util.Args;
38
39 class InternalByteArrayEntity extends AbstractHttpEntity implements Cloneable {
40
41 private final byte[] b;
42 private final int off, len;
43
44 public InternalByteArrayEntity(final byte[] b, final ContentType contentType) {
45 super();
46 Args.notNull(b, "Source byte array");
47 this.b = b;
48 this.off = 0;
49 this.len = this.b.length;
50 if (contentType != null) {
51 setContentType(contentType.toString());
52 }
53 }
54
55 public InternalByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType) {
56 super();
57 Args.notNull(b, "Source byte array");
58 if ((off < 0) || (off > b.length) || (len < 0) ||
59 ((off + len) < 0) || ((off + len) > b.length)) {
60 throw new IndexOutOfBoundsException("off: " + off + " len: " + len + " b.length: " + b.length);
61 }
62 this.b = b;
63 this.off = off;
64 this.len = len;
65 if (contentType != null) {
66 setContentType(contentType.toString());
67 }
68 }
69
70 public InternalByteArrayEntity(final byte[] b) {
71 this(b, null);
72 }
73
74 public InternalByteArrayEntity(final byte[] b, final int off, final int len) {
75 this(b, off, len, null);
76 }
77
78 @Override
79 public boolean isRepeatable() {
80 return true;
81 }
82
83 @Override
84 public long getContentLength() {
85 return this.len;
86 }
87
88 @Override
89 public InputStream getContent() {
90 return new ByteArrayInputStream(this.b, this.off, this.len);
91 }
92
93 @Override
94 public void writeTo(final OutputStream outStream) throws IOException {
95 Args.notNull(outStream, "Output stream");
96 outStream.write(this.b, this.off, this.len);
97 outStream.flush();
98 }
99
100 @Override
101 public boolean isStreaming() {
102 return false;
103 }
104
105 }