001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.oozie.compression;
019
020 import java.io.ByteArrayOutputStream;
021 import java.io.DataInputStream;
022 import java.io.IOException;
023 import java.util.zip.GZIPInputStream;
024 import java.util.zip.GZIPOutputStream;
025 import org.apache.commons.io.IOUtils;
026
027 /**
028 * Class to compress and decompress data using Gzip codec
029 *
030 */
031 public class GzipCompressionCodec implements CompressionCodec {
032
033 public static final String CODEC_NAME = "gz";
034
035 public byte[] compressBytes(byte[] header, byte[] data) throws IOException {
036 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(2000);
037 byteOutput.write(header);
038 GZIPOutputStream gzipOut = new GZIPOutputStream(byteOutput);
039 gzipOut.write(data);
040 gzipOut.close();
041 return byteOutput.toByteArray();
042 }
043
044 public byte[] compressString(byte[] header, String data) throws IOException {
045 ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(1000);
046 byteOutput.write(header);
047 GZIPOutputStream gzipOut = new GZIPOutputStream(byteOutput);
048 gzipOut.write(data.getBytes(CodecFactory.UTF_8_ENCODING));
049 gzipOut.close();
050 return byteOutput.toByteArray();
051 }
052
053 public String decompressToString(DataInputStream dais) throws IOException {
054 GZIPInputStream gzipIn = new GZIPInputStream(dais);
055 String decompress = IOUtils.toString(gzipIn, CodecFactory.UTF_8_ENCODING);
056 gzipIn.close();
057 return decompress;
058 }
059
060 public byte[] decompressToBytes(DataInputStream dais) throws IOException {
061 GZIPInputStream gzipIn = new GZIPInputStream(dais);
062 byte[] decompress = IOUtils.toByteArray(gzipIn);
063 gzipIn.close();
064 return decompress;
065 }
066
067 }