1 7 8 package java.util.zip; 9 10 import java.io.FilterInputStream ; 11 import java.io.InputStream ; 12 import java.io.IOException ; 13 14 26 27 public class DeflaterInputStream extends FilterInputStream { 28 29 protected final Deflater def; 30 31 32 protected final byte[] buf; 33 34 35 private byte[] rbuf = new byte[1]; 36 37 38 private boolean usesDefaultDeflater = false; 39 40 41 private boolean reachEOF = false; 42 43 46 private void ensureOpen() throws IOException { 47 if (in == null) { 48 throw new IOException ("Stream closed"); 49 } 50 } 51 52 59 public DeflaterInputStream(InputStream in) { 60 this(in, new Deflater ()); 61 usesDefaultDeflater = true; 62 } 63 64 72 public DeflaterInputStream(InputStream in, Deflater defl) { 73 this(in, defl, 512); 74 } 75 76 86 public DeflaterInputStream(InputStream in, Deflater defl, int bufLen) { 87 super(in); 88 89 if (in == null) 91 throw new NullPointerException ("Null input"); 92 if (defl == null) 93 throw new NullPointerException ("Null deflater"); 94 if (bufLen < 1) 95 throw new IllegalArgumentException ("Buffer size < 1"); 96 97 def = defl; 99 buf = new byte[bufLen]; 100 } 101 102 108 public void close() throws IOException { 109 if (in != null) { 110 try { 111 if (usesDefaultDeflater) { 113 def.end(); 114 } 115 116 in.close(); 117 } finally { 118 in = null; 119 } 120 } 121 } 122 123 132 public int read() throws IOException { 133 int len = read(rbuf, 0, 1); 135 if (len <= 0) 136 return -1; 137 return (rbuf[0] & 0xFF); 138 } 139 140 154 public int read(byte[] b, int off, int len) throws IOException { 155 ensureOpen(); 157 if (b == null) { 158 throw new NullPointerException ("Null buffer for read"); 159 } else if (off < 0 || len < 0 || len > b.length - off) { 160 throw new IndexOutOfBoundsException (); 161 } else if (len == 0) { 162 return 0; 163 } 164 165 int cnt = 0; 167 while (len > 0 && !def.finished()) { 168 int n; 169 170 if (def.needsInput()) { 172 n = in.read(buf, 0, buf.length); 173 if (n < 0) { 174 def.finish(); 176 } else if (n > 0) { 177 def.setInput(buf, 0, n); 178 } 179 } 180 181 n = def.deflate(b, off, len); 183 cnt += n; 184 off += n; 185 len -= n; 186 } 187 if (cnt == 0 && def.finished()) { 188 reachEOF = true; 189 cnt = -1; 190 } 191 192 return cnt; 193 } 194 195 207 public long skip(long n) throws IOException { 208 if (n < 0) { 209 throw new IllegalArgumentException ("negative skip length"); 210 } 211 ensureOpen(); 212 213 if (rbuf.length < 512) 215 rbuf = new byte[512]; 216 217 int total = (int)Math.min(n, Integer.MAX_VALUE); 218 long cnt = 0; 219 while (total > 0) { 220 int len = read(rbuf, 0, (total <= rbuf.length ? total : rbuf.length)); 222 223 if (len < 0) { 224 break; 225 } 226 cnt += len; 227 total -= len; 228 } 229 return cnt; 230 } 231 232 242 public int available() throws IOException { 243 ensureOpen(); 244 if (reachEOF) { 245 return 0; 246 } 247 return 1; 248 } 249 250 256 public boolean markSupported() { 257 return false; 258 } 259 260 265 public void mark(int limit) { 266 } 268 269 274 public void reset() throws IOException { 275 throw new IOException ("mark/reset not supported"); 276 } 277 } 278 | Popular Tags |