kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.internal; kitaev@213: kitaev@213: import java.io.EOFException; kitaev@213: import java.io.IOException; kitaev@213: import java.util.zip.DataFormatException; kitaev@213: import java.util.zip.Inflater; kitaev@213: import java.util.zip.ZipException; kitaev@213: kitaev@213: import org.tmatesoft.hg.core.HgBadStateException; kitaev@213: kitaev@213: kitaev@213: /** kitaev@213: * DataAccess counterpart for InflaterInputStream. kitaev@213: * XXX is it really needed to be subclass of FilterDataAccess? kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class InflaterDataAccess extends FilterDataAccess { kitaev@213: kitaev@213: private final Inflater inflater; kitaev@213: private final byte[] buffer; kitaev@213: private final byte[] singleByte = new byte[1]; kitaev@213: private int decompressedPos = 0; kitaev@213: private int decompressedLength; kitaev@213: kitaev@213: public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength) { kitaev@213: this(dataAccess, offset, compressedLength, -1, new Inflater(), 512); kitaev@213: } kitaev@213: kitaev@213: public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength, int actualLength) { kitaev@213: this(dataAccess, offset, compressedLength, actualLength, new Inflater(), 512); kitaev@213: } kitaev@213: kitaev@213: public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength, int actualLength, Inflater inflater, int bufSize) { kitaev@213: super(dataAccess, offset, compressedLength); kitaev@213: this.inflater = inflater; kitaev@213: this.decompressedLength = actualLength; kitaev@213: buffer = new byte[bufSize]; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public InflaterDataAccess reset() throws IOException { kitaev@213: super.reset(); kitaev@213: inflater.reset(); kitaev@213: decompressedPos = 0; kitaev@213: return this; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: protected int available() { kitaev@213: return length() - decompressedPos; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public boolean isEmpty() { kitaev@213: // can't use super.available() <= 0 because even when 0 < super.count < 6(?) kitaev@213: // decompressedPos might be already == length() kitaev@213: return available() <= 0; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public int length() { kitaev@213: if (decompressedLength != -1) { kitaev@213: return decompressedLength; kitaev@213: } kitaev@213: decompressedLength = 0; // guard to avoid endless loop in case length() would get invoked from below. kitaev@213: int c = 0; kitaev@213: try { kitaev@213: int oldPos = decompressedPos; kitaev@213: byte[] dummy = new byte[buffer.length]; kitaev@213: int toRead; kitaev@213: while ((toRead = super.available()) > 0) { kitaev@213: if (toRead > buffer.length) { kitaev@213: toRead = buffer.length; kitaev@213: } kitaev@213: super.readBytes(buffer, 0, toRead); kitaev@213: inflater.setInput(buffer, 0, toRead); kitaev@213: try { kitaev@213: while (!inflater.needsInput()) { kitaev@213: c += inflater.inflate(dummy, 0, dummy.length); kitaev@213: } kitaev@213: } catch (DataFormatException ex) { kitaev@213: throw new HgBadStateException(ex); kitaev@213: } kitaev@213: } kitaev@213: decompressedLength = c + oldPos; kitaev@213: reset(); kitaev@213: seek(oldPos); kitaev@213: return decompressedLength; kitaev@213: } catch (IOException ex) { kitaev@213: decompressedLength = -1; // better luck next time? kitaev@213: throw new HgBadStateException(ex); // XXX perhaps, checked exception kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public void seek(int localOffset) throws IOException { kitaev@213: if (localOffset < 0 /* || localOffset >= length() */) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: if (localOffset >= decompressedPos) { kitaev@213: skip((int) (localOffset - decompressedPos)); kitaev@213: } else { kitaev@213: reset(); kitaev@213: skip((int) localOffset); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public void skip(int bytes) throws IOException { kitaev@213: if (bytes < 0) { kitaev@213: bytes += decompressedPos; kitaev@213: if (bytes < 0) { kitaev@213: throw new IOException("Underflow. Rewind past start of the slice."); kitaev@213: } kitaev@213: reset(); kitaev@213: // fall-through kitaev@213: } kitaev@213: while (!isEmpty() && bytes > 0) { kitaev@213: readByte(); kitaev@213: bytes--; kitaev@213: } kitaev@213: if (bytes != 0) { kitaev@213: throw new IOException("Underflow. Rewind past end of the slice"); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public byte readByte() throws IOException { kitaev@213: readBytes(singleByte, 0, 1); kitaev@213: return singleByte[0]; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public void readBytes(byte[] b, int off, int len) throws IOException { kitaev@213: try { kitaev@213: int n; kitaev@213: while (len > 0) { kitaev@213: while ((n = inflater.inflate(b, off, len)) == 0) { kitaev@213: // FIXME few last bytes (checksum?) may be ignored by inflater, thus inflate may return 0 in kitaev@213: // perfectly legal conditions (when all data already expanded, but there are still some bytes kitaev@213: // in the input stream kitaev@213: if (inflater.finished() || inflater.needsDictionary()) { kitaev@213: throw new EOFException(); kitaev@213: } kitaev@213: if (inflater.needsInput()) { kitaev@213: // fill: kitaev@213: int toRead = super.available(); kitaev@213: if (toRead > buffer.length) { kitaev@213: toRead = buffer.length; kitaev@213: } kitaev@213: super.readBytes(buffer, 0, toRead); kitaev@213: inflater.setInput(buffer, 0, toRead); kitaev@213: } kitaev@213: } kitaev@213: off += n; kitaev@213: len -= n; kitaev@213: decompressedPos += n; kitaev@213: if (len == 0) { kitaev@213: return; // filled kitaev@213: } kitaev@213: } kitaev@213: } catch (DataFormatException e) { kitaev@213: String s = e.getMessage(); kitaev@213: throw new ZipException(s != null ? s : "Invalid ZLIB data format"); kitaev@213: } kitaev@213: } kitaev@213: }