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.impl.io;
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.Header;
39 import org.apache.hc.core5.http.HttpEntity;
40 import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
41 import org.apache.hc.core5.http.io.entity.EmptyInputStream;
42 import org.apache.hc.core5.io.Closer;
43
44 class IncomingHttpEntity implements HttpEntity {
45
46 private final InputStream content;
47 private final long len;
48 private final boolean chunked;
49 private final Header contentType;
50 private final Header contentEncoding;
51
52 IncomingHttpEntity(final InputStream content, final long len, final boolean chunked, final HeaderHeader.html#Header">Header contentType, final Header contentEncoding) {
53 this.content = content;
54 this.len = len;
55 this.chunked = chunked;
56 this.contentType = contentType;
57 this.contentEncoding = contentEncoding;
58 }
59
60 @Override
61 public boolean isRepeatable() {
62 return false;
63 }
64
65 @Override
66 public boolean isChunked() {
67 return chunked;
68 }
69
70 @Override
71 public long getContentLength() {
72 return len;
73 }
74
75 @Override
76 public String getContentType() {
77 return contentType != null ? contentType.getValue() : null;
78 }
79
80 @Override
81 public String getContentEncoding() {
82 return contentEncoding != null ? contentEncoding.getValue() : null;
83 }
84
85 @Override
86 public InputStream getContent() throws IOException, IllegalStateException {
87 return content;
88 }
89
90 @Override
91 public boolean isStreaming() {
92 return content != null && content != EmptyInputStream.INSTANCE;
93 }
94
95 @Override
96 public void writeTo(final OutputStream outStream) throws IOException {
97 AbstractHttpEntity.writeTo(this, outStream);
98 }
99
100 @Override
101 public Supplier<List<? extends Header>> getTrailers() {
102 return null;
103 }
104
105 @Override
106 public Set<String> getTrailerNames() {
107 return Collections.emptySet();
108 }
109
110 @Override
111 public void close() throws IOException {
112 Closer.close(content);
113 }
114
115 @Override
116 public String toString() {
117 final StringBuilder sb = new StringBuilder();
118 sb.append('[');
119 sb.append("Content-Type: ");
120 sb.append(getContentType());
121 sb.append(',');
122 sb.append("Content-Encoding: ");
123 sb.append(getContentEncoding());
124 sb.append(',');
125 final long len = getContentLength();
126 if (len >= 0) {
127 sb.append("Content-Length: ");
128 sb.append(len);
129 sb.append(',');
130 }
131 sb.append("Chunked: ");
132 sb.append(isChunked());
133 sb.append(']');
134 return sb.toString();
135 }
136
137 }