77import java .io .ByteArrayOutputStream ;
88import java .io .IOException ;
99import java .io .UncheckedIOException ;
10+ import java .util .Collections ;
11+ import java .util .HashMap ;
1012import java .util .List ;
1113import java .util .Map ;
1214import java .util .Set ;
@@ -84,16 +86,30 @@ public Bytes decompress(Bytes bytes) {
8486 }
8587 }
8688
87- private static final Map <String , Compressor > COMPRESSOR_MAP = Map .of (
88- IdentityGrpcTransformer .NAME , IdentityGrpcTransformer .INSTANCE ,
89- GzipGrpcTransformer .NAME , GzipGrpcTransformer .INSTANCE );
90- private static final Map <String , Decompressor > DECOMPRESSOR_MAP = Map .of (
91- IdentityGrpcTransformer .NAME , IdentityGrpcTransformer .INSTANCE ,
92- GzipGrpcTransformer .NAME , GzipGrpcTransformer .INSTANCE );
89+ private static final Map <String , Compressor > COMPRESSOR_MAP = new HashMap <>();
90+ private static final Map <String , Decompressor > DECOMPRESSOR_MAP = new HashMap <>();
91+
92+ /** Register a Compressor, potentially overwriting an existing registration for `name`. */
93+ public static synchronized void registerCompressor (String name , Compressor compressor ) {
94+ COMPRESSOR_MAP .put (name , compressor );
95+ }
96+
97+ /** Register a Decompressor, potentially overwriting an existing registration for `name`. */
98+ public static synchronized void registerDecompressor (String name , Decompressor decompressor ) {
99+ DECOMPRESSOR_MAP .put (name , decompressor );
100+ }
101+
102+ static {
103+ registerCompressor (IdentityGrpcTransformer .NAME , IdentityGrpcTransformer .INSTANCE );
104+ registerCompressor (GzipGrpcTransformer .NAME , GzipGrpcTransformer .INSTANCE );
105+
106+ registerDecompressor (IdentityGrpcTransformer .NAME , IdentityGrpcTransformer .INSTANCE );
107+ registerDecompressor (GzipGrpcTransformer .NAME , GzipGrpcTransformer .INSTANCE );
108+ }
93109
94110 /** Return names of all known compressors. */
95111 public static Set <String > getCompressorNames () {
96- return COMPRESSOR_MAP .keySet ();
112+ return Collections . unmodifiableSet ( COMPRESSOR_MAP .keySet () );
97113 }
98114
99115 /** Return a known Compressor by its name, or null if unknown. */
@@ -103,7 +119,7 @@ public static Compressor getCompressor(String name) {
103119
104120 /** Return names of all known decompressors. */
105121 public static Set <String > getDecompressorNames () {
106- return DECOMPRESSOR_MAP .keySet ();
122+ return Collections . unmodifiableSet ( DECOMPRESSOR_MAP .keySet () );
107123 }
108124
109125 /** Return a known Decompressor by its name, or null if unknown. */
@@ -129,7 +145,7 @@ public static Decompressor determineDecompressor(List<String> encodingList) {
129145 } else {
130146 throw new IllegalStateException (
131147 "GRPC peer didn't provide grpc-encoding header and 'identity' is unsupported, only the following are supported: "
132- + GrpcCompression . getDecompressorNames ());
148+ + DECOMPRESSOR_MAP . keySet ());
133149 }
134150 } else if (encodingList .size () > 1 ) {
135151 throw new IllegalStateException ("GRPC peer specified multiple encodings at once: " + encodingList );
@@ -142,7 +158,7 @@ public static Decompressor determineDecompressor(List<String> encodingList) {
142158 return GrpcCompression .getDecompressor (encoding );
143159 } else {
144160 throw new IllegalStateException ("GRPC peer uses an unsupported encoding: '" + encoding
145- + "' while only the following are supported: " + GrpcCompression . getDecompressorNames ());
161+ + "' while only the following are supported: " + DECOMPRESSOR_MAP . keySet ());
146162 }
147163 }
148164 }
@@ -155,8 +171,8 @@ public static Decompressor determineDecompressor(List<String> encodingList) {
155171 */
156172 public static String determineCompressorName (List <String > acceptEncoding , String encoding ) {
157173 final List <String > supportedAcceptEncodings = acceptEncoding .stream ()
158- .filter (ae -> GrpcCompression . getCompressorNames (). stream ()
159- .anyMatch (sae -> ae .equals (sae ) || ae .startsWith (sae + ";" )))
174+ .filter (ae ->
175+ COMPRESSOR_MAP . keySet (). stream () .anyMatch (sae -> ae .equals (sae ) || ae .startsWith (sae + ";" )))
160176 .toList ();
161177
162178 if (supportedAcceptEncodings .isEmpty ()) {
0 commit comments