1 /* 2 * Copyright 2015 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.util.zip.GZIPInputStream; 23 import java.util.zip.GZIPOutputStream; 24 25 /** 26 * Encloses classes related to the compression and decompression of messages. 27 * 28 */ 29 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704") 30 public interface Codec extends Compressor, Decompressor { 31 /** 32 * A gzip compressor and decompressor. In the future this will likely support other 33 * compression methods, such as compression level. 34 */ 35 final class Gzip implements Codec { 36 @Override getMessageEncoding()37 public String getMessageEncoding() { 38 return "gzip"; 39 } 40 41 @Override compress(OutputStream os)42 public OutputStream compress(OutputStream os) throws IOException { 43 return new GZIPOutputStream(os); 44 } 45 46 @Override decompress(InputStream is)47 public InputStream decompress(InputStream is) throws IOException { 48 return new GZIPInputStream(is); 49 } 50 } 51 52 /** 53 * The "identity", or "none" codec. This codec is special in that it can be used to explicitly 54 * disable Call compression on a Channel that by default compresses. 55 */ 56 final class Identity implements Codec { 57 /** 58 * Special sentinel codec indicating that no compression should be used. Users should use 59 * reference equality to see if compression is disabled. 60 */ 61 public static final Codec NONE = new Identity(); 62 63 @Override decompress(InputStream is)64 public InputStream decompress(InputStream is) { 65 return is; 66 } 67 68 @Override getMessageEncoding()69 public String getMessageEncoding() { 70 return "identity"; 71 } 72 73 @Override compress(OutputStream os)74 public OutputStream compress(OutputStream os) { 75 return os; 76 } 77 Identity()78 private Identity() {} 79 } 80 } 81