forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEncoding.java
More file actions
143 lines (123 loc) · 4.18 KB
/
MessageEncoding.java
File metadata and controls
143 lines (123 loc) · 4.18 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package io.grpc;
import static com.google.common.base.Preconditions.checkArgument;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
/**
* Encloses classes related to the compression and decompression of messages.
*/
public final class MessageEncoding {
private static final ConcurrentMap<String, Decompressor> decompressors =
initializeDefaultDecompressors();
/**
* Special sentinel codec indicating that no compression should be used. Users should use
* reference equality to see if compression is disabled.
*/
public static final Codec NONE = new Codec() {
@Override
public InputStream decompress(InputStream is) throws IOException {
return is;
}
@Override
public String getMessageEncoding() {
return "identity";
}
@Override
public OutputStream compress(OutputStream os) throws IOException {
return os;
}
};
/**
* Represents a message compressor.
*/
public interface Compressor {
/**
* Returns the message encoding that this compressor uses.
*
* <p>This can be values such as "gzip", "deflate", "snappy", etc.
*/
String getMessageEncoding();
/**
* Wraps an existing output stream with a compressing output stream.
* @param os The output stream of uncompressed data
* @return An output stream that compresses
*/
OutputStream compress(OutputStream os) throws IOException;
}
/**
* Represents a message decompressor.
*/
public interface Decompressor {
/**
* Returns the message encoding that this compressor uses.
*
* <p>This can be values such as "gzip", "deflate", "snappy", etc.
*/
String getMessageEncoding();
/**
* Wraps an existing input stream with a decompressing input stream.
* @param is The input stream of uncompressed data
* @return An input stream that decompresses
*/
InputStream decompress(InputStream is) throws IOException;
}
/**
* Represents an object that can both compress and decompress messages.
*/
public interface Codec extends Compressor, Decompressor {}
/**
* A gzip compressor and decompressor. In the future this will likely support other
* compression methods, such as compression level.
*/
public static final class Gzip implements Codec {
@Override
public String getMessageEncoding() {
return "gzip";
}
@Override
public OutputStream compress(OutputStream os) throws IOException {
return new GZIPOutputStream(os);
}
@Override
public InputStream decompress(InputStream is) throws IOException {
return new GZIPInputStream(is);
}
}
/**
* Registers a decompressor for both decompression and message encoding negotiation.
* @throws IllegalArgumentException if another compressor by the same name is already registered.
*/
public static final void registerDecompressor(Decompressor d) {
Decompressor previousDecompressor = decompressors.putIfAbsent(d.getMessageEncoding(), d);
checkArgument(previousDecompressor == null,
"A decompressor was already registered: %s", previousDecompressor);
}
/**
* Provides a list of all message encodings that have decompressors available.
*/
public static Collection<String> getKnownMessageEncodings() {
return Collections.unmodifiableSet(decompressors.keySet());
}
/**
* Returns a decompressor for the given message encoding, or {@code null} if none has been
* registered.
*/
@Nullable
public static Decompressor lookupDecompressor(String messageEncoding) {
return decompressors.get(messageEncoding);
}
private static ConcurrentMap<String, Decompressor> initializeDefaultDecompressors() {
ConcurrentMap<String, Decompressor> defaultDecompressors =
new ConcurrentHashMap<String, Decompressor>();
Decompressor gzip = new Gzip();
defaultDecompressors.put(gzip.getMessageEncoding(), gzip);
return defaultDecompressors;
}
}