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.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Set;
36
37 import org.apache.hc.core5.function.Supplier;
38 import org.apache.hc.core5.http.ContentType;
39 import org.apache.hc.core5.http.Header;
40 import org.apache.hc.core5.http.HttpEntity;
41 import org.apache.hc.core5.util.Args;
42
43
44
45
46
47
48
49 public abstract class AbstractHttpEntity implements HttpEntity {
50
51 static final int OUTPUT_BUFFER_SIZE = 4096;
52
53 private final String contentType;
54 private final String contentEncoding;
55 private final boolean chunked;
56
57 protected AbstractHttpEntity(final String contentType, final String contentEncoding, final boolean chunked) {
58 this.contentType = contentType;
59 this.contentEncoding = contentEncoding;
60 this.chunked = chunked;
61 }
62
63 protected AbstractHttpEntity(final ContentType contentType, final String contentEncoding, final boolean chunked) {
64 this.contentType = contentType != null ? contentType.toString() : null;
65 this.contentEncoding = contentEncoding;
66 this.chunked = chunked;
67 }
68
69 protected AbstractHttpEntity(final String contentType, final String contentEncoding) {
70 this(contentType, contentEncoding, false);
71 }
72
73 protected AbstractHttpEntity(final ContentType contentType, final String contentEncoding) {
74 this(contentType, contentEncoding, false);
75 }
76
77 public static void writeTo(final HttpEntity entity, final OutputStream outStream) throws IOException {
78 Args.notNull(entity, "Entity");
79 Args.notNull(outStream, "Output stream");
80 try (final InputStream inStream = entity.getContent()) {
81 if (inStream != null) {
82 int count;
83 final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
84 while ((count = inStream.read(tmp)) != -1) {
85 outStream.write(tmp, 0, count);
86 }
87 }
88 }
89 }
90
91 @Override
92 public void writeTo(final OutputStream outStream) throws IOException {
93 writeTo(this, outStream);
94 }
95
96 @Override
97 public final String getContentType() {
98 return contentType;
99 }
100
101 @Override
102 public final String getContentEncoding() {
103 return contentEncoding;
104 }
105
106 @Override
107 public final boolean isChunked() {
108 return chunked;
109 }
110
111 @Override
112 public boolean isRepeatable() {
113 return false;
114 }
115
116 @Override
117 public Supplier<List<? extends Header>> getTrailers() {
118 return null;
119 }
120
121 @Override
122 public Set<String> getTrailerNames() {
123 return Collections.emptySet();
124 }
125
126 @Override
127 public String toString() {
128 final StringBuilder sb = new StringBuilder();
129 sb.append("[Entity-Class: ");
130 sb.append(getClass().getSimpleName());
131 sb.append(", Content-Type: ");
132 sb.append(contentType);
133 sb.append(", Content-Encoding: ");
134 sb.append(contentEncoding);
135 sb.append(", chunked: ");
136 sb.append(chunked);
137 sb.append(']');
138 return sb.toString();
139 }
140
141 }