1 7 8 package java.util.zip; 9 10 import java.io.FilterInputStream ; 11 import java.io.InputStream ; 12 import java.io.IOException ; 13 import java.io.EOFException ; 14 15 24 public 25 class InflaterInputStream extends FilterInputStream { 26 29 protected Inflater inf; 30 31 34 protected byte[] buf; 35 36 39 protected int len; 40 41 private boolean closed = false; 42 private boolean reachEOF = false; 44 45 48 private void ensureOpen() throws IOException { 49 if (closed) { 50 throw new IOException ("Stream closed"); 51 } 52 } 53 54 55 63 public InflaterInputStream(InputStream in, Inflater inf, int size) { 64 super(in); 65 if (in == null || inf == null) { 66 throw new NullPointerException (); 67 } else if (size <= 0) { 68 throw new IllegalArgumentException ("buffer size <= 0"); 69 } 70 this.inf = inf; 71 buf = new byte[size]; 72 } 73 74 80 public InflaterInputStream(InputStream in, Inflater inf) { 81 this(in, inf, 512); 82 } 83 84 boolean usesDefaultInflater = false; 85 86 90 public InflaterInputStream(InputStream in) { 91 this(in, new Inflater ()); 92 usesDefaultInflater = true; 93 } 94 95 private byte[] singleByteBuf = new byte[1]; 96 97 103 public int read() throws IOException { 104 ensureOpen(); 105 return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff; 106 } 107 108 119 public int read(byte[] b, int off, int len) throws IOException { 120 ensureOpen(); 121 if ((off | len | (off + len) | (b.length - (off + len))) < 0) { 122 throw new IndexOutOfBoundsException (); 123 } else if (len == 0) { 124 return 0; 125 } 126 try { 127 int n; 128 while ((n = inf.inflate(b, off, len)) == 0) { 129 if (inf.finished() || inf.needsDictionary()) { 130 reachEOF = true; 131 return -1; 132 } 133 if (inf.needsInput()) { 134 fill(); 135 } 136 } 137 return n; 138 } catch (DataFormatException e) { 139 String s = e.getMessage(); 140 throw new ZipException (s != null ? s : "Invalid ZLIB data format"); 141 } 142 } 143 144 154 public int available() throws IOException { 155 ensureOpen(); 156 if (reachEOF) { 157 return 0; 158 } else { 159 return 1; 160 } 161 } 162 163 private byte[] b = new byte[512]; 164 165 172 public long skip(long n) throws IOException { 173 if (n < 0) { 174 throw new IllegalArgumentException ("negative skip length"); 175 } 176 ensureOpen(); 177 int max = (int)Math.min(n, Integer.MAX_VALUE); 178 int total = 0; 179 while (total < max) { 180 int len = max - total; 181 if (len > b.length) { 182 len = b.length; 183 } 184 len = read(b, 0, len); 185 if (len == -1) { 186 reachEOF = true; 187 break; 188 } 189 total += len; 190 } 191 return total; 192 } 193 194 199 public void close() throws IOException { 200 if (!closed) { 201 if (usesDefaultInflater) 202 inf.end(); 203 in.close(); 204 closed = true; 205 } 206 } 207 208 212 protected void fill() throws IOException { 213 ensureOpen(); 214 len = in.read(buf, 0, buf.length); 215 if (len == -1) { 216 throw new EOFException ("Unexpected end of ZLIB input stream"); 217 } 218 inf.setInput(buf, 0, len); 219 } 220 221 232 public boolean markSupported() { 233 return false; 234 } 235 236 246 public synchronized void mark(int readlimit) { 247 } 248 249 261 public synchronized void reset() throws IOException { 262 throw new IOException ("mark/reset not supported"); 263 } 264 } 265 | Popular Tags |