1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 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 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.core.internal.compression; 17 18 import static software.amazon.awssdk.utils.IoUtils.closeQuietly; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.IOException; 22 import java.io.UncheckedIOException; 23 import java.util.zip.GZIPOutputStream; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 import software.amazon.awssdk.annotations.SdkInternalApi; 27 import software.amazon.awssdk.core.SdkBytes; 28 29 @SdkInternalApi 30 public final class GzipCompressor implements Compressor { 31 32 private static final String COMPRESSOR_TYPE = "gzip"; 33 private static final Logger log = LoggerFactory.getLogger(GzipCompressor.class); 34 35 @Override compressorType()36 public String compressorType() { 37 return COMPRESSOR_TYPE; 38 } 39 40 @Override compress(SdkBytes content)41 public SdkBytes compress(SdkBytes content) { 42 GZIPOutputStream gzipOutputStream = null; 43 try { 44 ByteArrayOutputStream compressedOutputStream = new ByteArrayOutputStream(); 45 gzipOutputStream = new GZIPOutputStream(compressedOutputStream); 46 gzipOutputStream.write(content.asByteArray()); 47 gzipOutputStream.close(); 48 return SdkBytes.fromByteArray(compressedOutputStream.toByteArray()); 49 } catch (IOException e) { 50 throw new UncheckedIOException(e); 51 } finally { 52 closeQuietly(gzipOutputStream, log); 53 } 54 } 55 }