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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.commons.compress.compressors.gzip;
020
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.util.zip.GZIPInputStream;
024
025 import org.apache.commons.compress.compressors.CompressorInputStream;
026
027 /**
028 * Implements the "gz" compression format as an input stream.
029 * This classes wraps the standard java classes for working with gz.
030 */
031 public class GzipCompressorInputStream extends CompressorInputStream {
032 /* reference to the compressed stream */
033 private final GZIPInputStream in;
034
035 /**
036 * Constructs a new GZip compressed input stream by the referenced
037 * InputStream.
038 *
039 * @param inputStream the InputStream from which this object should be created of
040 * @throws IOException if the stream could not be created
041 */
042 public GzipCompressorInputStream(InputStream inputStream) throws IOException {
043 in = new GZIPInputStream(inputStream);
044 }
045
046 /** {@inheritDoc} */
047 @Override
048 public int read() throws IOException {
049 int read = in.read();
050 this.count(read < 0 ? -1 : 1);
051 return read;
052 }
053
054 /**
055 * {@inheritDoc}
056 *
057 * @since Apache Commons Compress 1.1
058 */
059 @Override
060 public int read(byte[] b) throws IOException {
061 int read = in.read(b);
062 this.count(read);
063 return read;
064 }
065
066 /**
067 * {@inheritDoc}
068 *
069 * @since Apache Commons Compress 1.1
070 */
071 @Override
072 public int read(byte[] b, int from, int length) throws IOException {
073 int read = in.read(b, from, length);
074 this.count(read);
075 return read;
076 }
077
078 /**
079 * Checks if the signature matches what is expected for a gzip file.
080 *
081 * @param signature
082 * the bytes to check
083 * @param length
084 * the number of bytes to check
085 * @return true, if this stream is a gzipped compressed stream, false otherwise
086 *
087 * @since Apache Commons Compress 1.1
088 */
089 public static boolean matches(byte[] signature, int length) {
090
091 if (length < 2) {
092 return false;
093 }
094
095 if (signature[0] != 31) {
096 return false;
097 }
098
099 if (signature[1] != -117) {
100 return false;
101 }
102
103 return true;
104 }
105
106 /**
107 * Closes the input stream (unless it is System.in).
108 *
109 * @since 1.2
110 */
111 @Override
112 public void close() throws IOException {
113 if (this.in != System.in) {
114 this.in.close();
115 }
116 }
117 }