001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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 */
018package org.jwat.gzip;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.util.Date;
025import java.util.LinkedList;
026import java.util.List;
027import java.util.zip.DataFormatException;
028
029import org.jwat.common.Diagnostics;
030
031/**
032 * GZip entry container. Exposes methods for accessing the entry payload's
033 * input or output stream.
034 *
035 * @author nicl
036 */
037public class GzipEntry implements Closeable {
038
039    /** Size of buffer used in writeFrom(). */
040    public static final int WRITE_FROM_BUFFER_SIZE = 8192;
041
042    /** Is this entry compliant ie. error free. */
043    protected boolean bIsCompliant;
044
045    /** Starting offset of this entry in the input stream from whence it came. */
046    public long startOffset = -1;
047
048    /** Bytes consumed while validating this entry. */
049    public long consumed;
050
051    /** Leading magic. */
052    public int magic;
053    /** Compression mode. */
054    public short cm;
055    /** Flags. */
056    public short flg;
057    /** Date in seconds. */
058    public long mtime;
059    /** Date converted. */
060    public Date date;
061    /** Compression flags. */
062    public short xfl;
063    /** Operating system. */
064    public short os;
065
066    /** Is compressed data ASCII. */
067    public boolean bFText;
068
069    /** Optional FEXTRA XLEN values. */
070    public Integer xlen;
071    /** Optional FEXTRA data. */
072    public byte[] extraBytes;
073    /** List of FEXTRA data container objects. */
074    public List<GzipExtraData> extraData = new LinkedList<GzipExtraData>();
075    /** Optional FNAME in iso-8859-1 format. */
076    public String fname;
077    /** Optional FCOMMENT in iso-8859-1 format (new lines should be LF only).*/
078    public String fcomment;
079    /** Optional CRC16 if present. */
080    public Integer crc16;
081
082    /** Trailing CRC32. */
083    public int crc32;
084    /** Trailing uncompressed size modulo 2^32. */
085    public int isize;
086
087    /** CRC16 Calculate(d). */
088    public boolean bFhCrc;
089
090    /** Computed CRC16. */
091    public int comp_crc16;
092    /** Computed CRC32. */
093    public int comp_crc32;
094    /** Computed ISize. */
095    public int comp_isize;
096
097    /** Uncompressed size. */
098    public long uncompressed_size;
099
100    /** Compressed size. */
101    public long compressed_size;
102
103    /** Input stream to read uncompressed data. */
104    protected InputStream in;
105
106    /** Output stream to write compressed data. */
107    protected OutputStream out;
108
109    /** GZip reader responsible for reading this entry. */
110    protected GzipReader reader;
111
112    /** GZip writer responsible for writing this entry. */
113    protected GzipWriter writer;
114
115    /** End of uncompressed file status. */
116    protected boolean bEof = false;
117
118    /** Validation errors and warnings. */
119    public final Diagnostics diagnostics = new Diagnostics();
120
121    /**
122     * Construct GZip Entry object.
123     */
124    public GzipEntry() {
125        cm = GzipConstants.CM_DEFLATE;
126        os = GzipConstants.OS_UNKNOWN;
127    }
128
129    /**
130     * Release resources associated with this record.
131     * @throws IOException if an I/O error occurs while closing entry
132     */
133    public void close() throws IOException {
134        if (!bEof) {
135            bEof = true;
136            if (in != null) {
137                in.close();
138                in = null;
139            }
140            if (out != null) {
141                out.close();
142                out = null;
143            }
144            if (reader != null) {
145                consumed = reader.pbin.getConsumed() - startOffset;
146                reader.consumed += consumed;
147                reader = null;
148            }
149            if (writer != null) {
150                writer = null;
151            }
152        }
153    }
154
155    /**
156     * Returns a boolean indicating the ISO compliance status of this record.
157     * @return a boolean indicating the ISO compliance status of this record
158     */
159    public boolean isCompliant() {
160        return (!diagnostics.hasErrors() && !diagnostics.hasWarnings());
161    }
162
163    /**
164     * Returns this entry's offset relative to the start of the input stream.
165     * @return this entry's offset relative to the start of the input stream
166     */
167    public long getStartOffset() {
168        return startOffset;
169    }
170
171    /**
172     * Returns an input stream which must be used to read compressed data
173     * after it has been uncompressed or null, if the entry is being written.
174     * @return input stream to read uncompressed data or null
175     */
176    public InputStream getInputStream() {
177        if (reader == null) {
178            throw new IllegalStateException("Not in reading state!");
179        }
180        return in;
181    }
182
183    /**
184     * Returns an output stream which can be used to compress data or null,
185     * if the entry is being read.
186     * @return output stream to write uncompressed data or null
187     */
188    public OutputStream getOutputStream() {
189        if (writer == null) {
190            throw new IllegalStateException("Not in writing state!");
191        }
192        return out;
193    }
194
195    /**
196     * Read from input stream and write compressed data on output stream.
197     * This method writes the compressed data read from the input stream
198     * and closes the entry. Use @see(#getOutputStream) for more control.
199     * This method closes the GZip entry before returning thus rendering
200     * the OutputStream invalid.
201     * @param in input stream with uncompressed data
202     * @throws IOException if an I/O error occurs while transferring
203     */
204    public void writeFrom(InputStream in) throws IOException {
205        if (writer == null) {
206            throw new IllegalStateException("Not in writing state!");
207        }
208        if (in == null) {
209            throw new IllegalArgumentException("'in' is null!");
210        }
211        byte[] tmpBuf = new byte[WRITE_FROM_BUFFER_SIZE];
212        int read;
213        try {
214            while ((read = writer.readCompressed(in, tmpBuf, 0, tmpBuf.length)) != -1) {
215                writer.out.write(tmpBuf, 0, read);
216            }
217        }
218        catch (DataFormatException e) {
219            throw new IOException(e);
220        }
221        writer.writeTrailer(this);
222        // Subsequent getOutputStream calls will return null because the GZip
223        // entry is considered closed after the trailer has been written.
224        out = null;
225    }
226
227    @Override
228    public String toString() {
229        StringBuffer sb = new StringBuffer(128);
230        sb.append("magic: " + magic);
231        sb.append("gzipmagic: " + GzipConstants.GZIP_MAGIC);
232        sb.append("cm: " + cm);
233        sb.append("flg: " + flg);
234        sb.append("mtime: " + mtime);
235        sb.append("mtime(date): " + date);
236        sb.append("xlf: " + xfl);
237        sb.append("os: " + os);
238        if (xlen != null) {
239            sb.append("xlen: " + xlen);
240        }
241        if (fname != null) {
242            sb.append("fname: " + fname);
243        }
244        if (fcomment != null) {
245            sb.append("fcomment: " + fcomment);
246        }
247        if (crc16 != null) {
248            sb.append("crc16: " + crc16);
249        }
250        sb.append("comp_crc16: " + comp_crc16);
251        sb.append("crc32: " + crc32);
252        sb.append("isize: " + isize);
253        sb.append("comp_crc32: " + comp_crc32);
254        return sb.toString();
255    }
256
257}