-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathCompressedEntity.java
More file actions
111 lines (92 loc) · 3.44 KB
/
CompressedEntity.java
File metadata and controls
111 lines (92 loc) · 3.44 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.clickhouse.client.api.internal;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Set;
public class CompressedEntity implements HttpEntity {
private HttpEntity httpEntity;
private final boolean isResponse;
private final CompressorStreamFactory compressorStreamFactory;
private final String compressionAlgo;
CompressedEntity(HttpEntity httpEntity, boolean isResponse, CompressorStreamFactory compressorStreamFactory) {
this.httpEntity = httpEntity;
this.isResponse = isResponse;
this.compressorStreamFactory = compressorStreamFactory;
this.compressionAlgo = getCompressionAlgoName(httpEntity.getContentEncoding());
}
@Override
public boolean isRepeatable() {
return httpEntity.isRepeatable();
}
@Override
public InputStream getContent() throws IOException, UnsupportedOperationException {
if (!isResponse) {
throw new UnsupportedOperationException("Unsupported: getting compressed content of request");
}
try {
return compressorStreamFactory.createCompressorInputStream(compressionAlgo, httpEntity.getContent());
} catch (CompressorException e) {
throw new IOException("Failed to create decompressing input stream", e);
}
}
@Override
public void writeTo(OutputStream outStream) throws IOException {
if (isResponse) {
// called by us to get compressed response
throw new UnsupportedOperationException("Unsupported: writing compressed response to elsewhere");
}
try (OutputStream compressingStream = compressorStreamFactory.createCompressorOutputStream(compressionAlgo, outStream)){
httpEntity.writeTo(compressingStream);
} catch (CompressorException e) {
throw new IOException("Failed to create compressing output stream", e);
}
}
@Override
public boolean isStreaming() {
return httpEntity.isStreaming();
}
@Override
public Supplier<List<? extends Header>> getTrailers() {
return httpEntity.getTrailers();
}
@Override
public void close() throws IOException {
httpEntity.close();
}
@Override
public long getContentLength() {
// compressed request length is unknown even if it is a byte[]
return isResponse ? httpEntity.getContentLength() : -1;
}
@Override
public String getContentType() {
return httpEntity.getContentType();
}
@Override
public String getContentEncoding() {
return httpEntity.getContentEncoding();
}
@Override
public boolean isChunked() {
return httpEntity.isChunked();
}
@Override
public Set<String> getTrailerNames() {
return httpEntity.getTrailerNames();
}
private String getCompressionAlgoName(String contentEncoding) {
String algo = contentEncoding;
if (algo.equalsIgnoreCase("gzip")) {
algo = CompressorStreamFactory.GZIP;
} else if (algo.equalsIgnoreCase("lz4")) {
algo = CompressorStreamFactory.LZ4_FRAMED;
}
return algo;
}
}