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