Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/internal/InflaterDataAccess.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
| author | Alexander Kitaev <kitaev@gmail.com> |
|---|---|
| date | Tue, 10 May 2011 10:52:53 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 212:edb2e2829352 | 213:6ec4af642ba8 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.internal; | |
| 18 | |
| 19 import java.io.EOFException; | |
| 20 import java.io.IOException; | |
| 21 import java.util.zip.DataFormatException; | |
| 22 import java.util.zip.Inflater; | |
| 23 import java.util.zip.ZipException; | |
| 24 | |
| 25 import org.tmatesoft.hg.core.HgBadStateException; | |
| 26 | |
| 27 | |
| 28 /** | |
| 29 * DataAccess counterpart for InflaterInputStream. | |
| 30 * XXX is it really needed to be subclass of FilterDataAccess? | |
| 31 * | |
| 32 * @author Artem Tikhomirov | |
| 33 * @author TMate Software Ltd. | |
| 34 */ | |
| 35 public class InflaterDataAccess extends FilterDataAccess { | |
| 36 | |
| 37 private final Inflater inflater; | |
| 38 private final byte[] buffer; | |
| 39 private final byte[] singleByte = new byte[1]; | |
| 40 private int decompressedPos = 0; | |
| 41 private int decompressedLength; | |
| 42 | |
| 43 public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength) { | |
| 44 this(dataAccess, offset, compressedLength, -1, new Inflater(), 512); | |
| 45 } | |
| 46 | |
| 47 public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength, int actualLength) { | |
| 48 this(dataAccess, offset, compressedLength, actualLength, new Inflater(), 512); | |
| 49 } | |
| 50 | |
| 51 public InflaterDataAccess(DataAccess dataAccess, int offset, int compressedLength, int actualLength, Inflater inflater, int bufSize) { | |
| 52 super(dataAccess, offset, compressedLength); | |
| 53 this.inflater = inflater; | |
| 54 this.decompressedLength = actualLength; | |
| 55 buffer = new byte[bufSize]; | |
| 56 } | |
| 57 | |
| 58 @Override | |
| 59 public InflaterDataAccess reset() throws IOException { | |
| 60 super.reset(); | |
| 61 inflater.reset(); | |
| 62 decompressedPos = 0; | |
| 63 return this; | |
| 64 } | |
| 65 | |
| 66 @Override | |
| 67 protected int available() { | |
| 68 return length() - decompressedPos; | |
| 69 } | |
| 70 | |
| 71 @Override | |
| 72 public boolean isEmpty() { | |
| 73 // can't use super.available() <= 0 because even when 0 < super.count < 6(?) | |
| 74 // decompressedPos might be already == length() | |
| 75 return available() <= 0; | |
| 76 } | |
| 77 | |
| 78 @Override | |
| 79 public int length() { | |
| 80 if (decompressedLength != -1) { | |
| 81 return decompressedLength; | |
| 82 } | |
| 83 decompressedLength = 0; // guard to avoid endless loop in case length() would get invoked from below. | |
| 84 int c = 0; | |
| 85 try { | |
| 86 int oldPos = decompressedPos; | |
| 87 byte[] dummy = new byte[buffer.length]; | |
| 88 int toRead; | |
| 89 while ((toRead = super.available()) > 0) { | |
| 90 if (toRead > buffer.length) { | |
| 91 toRead = buffer.length; | |
| 92 } | |
| 93 super.readBytes(buffer, 0, toRead); | |
| 94 inflater.setInput(buffer, 0, toRead); | |
| 95 try { | |
| 96 while (!inflater.needsInput()) { | |
| 97 c += inflater.inflate(dummy, 0, dummy.length); | |
| 98 } | |
| 99 } catch (DataFormatException ex) { | |
| 100 throw new HgBadStateException(ex); | |
| 101 } | |
| 102 } | |
| 103 decompressedLength = c + oldPos; | |
| 104 reset(); | |
| 105 seek(oldPos); | |
| 106 return decompressedLength; | |
| 107 } catch (IOException ex) { | |
| 108 decompressedLength = -1; // better luck next time? | |
| 109 throw new HgBadStateException(ex); // XXX perhaps, checked exception | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 @Override | |
| 114 public void seek(int localOffset) throws IOException { | |
| 115 if (localOffset < 0 /* || localOffset >= length() */) { | |
| 116 throw new IllegalArgumentException(); | |
| 117 } | |
| 118 if (localOffset >= decompressedPos) { | |
| 119 skip((int) (localOffset - decompressedPos)); | |
| 120 } else { | |
| 121 reset(); | |
| 122 skip((int) localOffset); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 @Override | |
| 127 public void skip(int bytes) throws IOException { | |
| 128 if (bytes < 0) { | |
| 129 bytes += decompressedPos; | |
| 130 if (bytes < 0) { | |
| 131 throw new IOException("Underflow. Rewind past start of the slice."); | |
| 132 } | |
| 133 reset(); | |
| 134 // fall-through | |
| 135 } | |
| 136 while (!isEmpty() && bytes > 0) { | |
| 137 readByte(); | |
| 138 bytes--; | |
| 139 } | |
| 140 if (bytes != 0) { | |
| 141 throw new IOException("Underflow. Rewind past end of the slice"); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 @Override | |
| 146 public byte readByte() throws IOException { | |
| 147 readBytes(singleByte, 0, 1); | |
| 148 return singleByte[0]; | |
| 149 } | |
| 150 | |
| 151 @Override | |
| 152 public void readBytes(byte[] b, int off, int len) throws IOException { | |
| 153 try { | |
| 154 int n; | |
| 155 while (len > 0) { | |
| 156 while ((n = inflater.inflate(b, off, len)) == 0) { | |
| 157 // FIXME few last bytes (checksum?) may be ignored by inflater, thus inflate may return 0 in | |
| 158 // perfectly legal conditions (when all data already expanded, but there are still some bytes | |
| 159 // in the input stream | |
| 160 if (inflater.finished() || inflater.needsDictionary()) { | |
| 161 throw new EOFException(); | |
| 162 } | |
| 163 if (inflater.needsInput()) { | |
| 164 // fill: | |
| 165 int toRead = super.available(); | |
| 166 if (toRead > buffer.length) { | |
| 167 toRead = buffer.length; | |
| 168 } | |
| 169 super.readBytes(buffer, 0, toRead); | |
| 170 inflater.setInput(buffer, 0, toRead); | |
| 171 } | |
| 172 } | |
| 173 off += n; | |
| 174 len -= n; | |
| 175 decompressedPos += n; | |
| 176 if (len == 0) { | |
| 177 return; // filled | |
| 178 } | |
| 179 } | |
| 180 } catch (DataFormatException e) { | |
| 181 String s = e.getMessage(); | |
| 182 throw new ZipException(s != null ? s : "Invalid ZLIB data format"); | |
| 183 } | |
| 184 } | |
| 185 } |
