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 package org.apache.hc.client5.http.impl.cache;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.io.SequenceInputStream;
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.util.Args;
41 import org.apache.hc.core5.util.ByteArrayBuffer;
42
43 class CombinedEntity implements HttpEntity {
44
45 private final HttpEntity entity;
46 private final InputStream combinedStream;
47
48 CombinedEntity(final HttpEntity entity, final ByteArrayBuffer buf) throws IOException {
49 super();
50 this.entity = entity;
51 this.combinedStream = new SequenceInputStream(
52 new ByteArrayInputStream(buf.array(), 0, buf.length()),
53 entity.getContent());
54 }
55
56 @Override
57 public long getContentLength() {
58 return -1;
59 }
60
61 @Override
62 public String getContentType() {
63 return entity.getContentType();
64 }
65
66 @Override
67 public String getContentEncoding() {
68 return entity.getContentEncoding();
69 }
70
71 @Override
72 public boolean isChunked() {
73 return true;
74 }
75
76 @Override
77 public boolean isRepeatable() {
78 return false;
79 }
80
81 @Override
82 public boolean isStreaming() {
83 return true;
84 }
85
86 @Override
87 public InputStream getContent() throws IOException, IllegalStateException {
88 return this.combinedStream;
89 }
90
91 @Override
92 public Set<String> getTrailerNames() {
93 return entity.getTrailerNames();
94 }
95
96 @Override
97 public Supplier<List<? extends Header>> getTrailers() {
98 return entity.getTrailers();
99 }
100
101 @Override
102 public void writeTo(final OutputStream outStream) throws IOException {
103 Args.notNull(outStream, "Output stream");
104 try (InputStream inStream = getContent()) {
105 int l;
106 final byte[] tmp = new byte[2048];
107 while ((l = inStream.read(tmp)) != -1) {
108 outStream.write(tmp, 0, l);
109 }
110 }
111 }
112
113 @Override
114 public void close() throws IOException {
115 try {
116 combinedStream.close();
117 } finally {
118 entity.close();
119 }
120 }
121
122 }