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.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36 import org.apache.hc.core5.annotation.Contract;
37 import org.apache.hc.core5.annotation.ThreadingBehavior;
38 import org.apache.hc.core5.http.ContentType;
39 import org.apache.hc.core5.io.IOCallback;
40 import org.apache.hc.core5.util.Args;
41
42
43
44
45
46
47
48 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
49 public final class EntityTemplate extends AbstractHttpEntity {
50
51 private final long contentLength;
52 private final IOCallback<OutputStream> callback;
53
54 public EntityTemplate(
55 final long contentLength, final ContentType contentType, final String contentEncoding,
56 final IOCallback<OutputStream> callback) {
57 super(contentType, contentEncoding);
58 this.contentLength = contentLength;
59 this.callback = Args.notNull(callback, "I/O callback");
60 }
61
62 @Override
63 public long getContentLength() {
64 return contentLength;
65 }
66
67 @Override
68 public InputStream getContent() throws IOException {
69 final ByteArrayOutputStream buf = new ByteArrayOutputStream();
70 writeTo(buf);
71 return new ByteArrayInputStream(buf.toByteArray());
72 }
73
74 @Override
75 public boolean isRepeatable() {
76 return true;
77 }
78
79 @Override
80 public void writeTo(final OutputStream outStream) throws IOException {
81 Args.notNull(outStream, "Output stream");
82 this.callback.execute(outStream);
83 }
84
85 @Override
86 public boolean isStreaming() {
87 return false;
88 }
89
90 @Override
91 public void close() throws IOException {
92 }
93
94 }