1 7 8 package java.util.zip; 9 10 import java.io.FilterOutputStream ; 11 import java.io.IOException ; 12 import java.io.OutputStream ; 13 14 26 27 public class InflaterOutputStream extends FilterOutputStream { 28 29 protected final Inflater inf; 30 31 32 protected final byte[] buf; 33 34 35 private final byte[] wbuf = new byte[1]; 36 37 38 private boolean usesDefaultInflater = false; 39 40 41 private boolean closed = false; 42 43 46 private void ensureOpen() throws IOException { 47 if (closed) { 48 throw new IOException ("Stream closed"); 49 } 50 } 51 52 59 public InflaterOutputStream(OutputStream out) { 60 this(out, new Inflater ()); 61 usesDefaultInflater = true; 62 } 63 64 72 public InflaterOutputStream(OutputStream out, Inflater infl) { 73 this(out, infl, 512); 74 } 75 76 86 public InflaterOutputStream(OutputStream out, Inflater infl, int bufLen) { 87 super(out); 88 89 if (out == null) 91 throw new NullPointerException ("Null output"); 92 if (infl == null) 93 throw new NullPointerException ("Null inflater"); 94 if (bufLen <= 0) 95 throw new IllegalArgumentException ("Buffer size < 1"); 96 97 inf = infl; 99 buf = new byte[bufLen]; 100 } 101 102 108 public void close() throws IOException { 109 if (!closed) { 110 try { 112 finish(); 113 } finally { 114 out.close(); 115 closed = true; 116 } 117 } 118 } 119 120 127 public void flush() throws IOException { 128 ensureOpen(); 129 130 if (!inf.finished()) { 132 try { 133 while (!inf.finished() && !inf.needsInput()) { 134 int n; 135 136 n = inf.inflate(buf, 0, buf.length); 138 if (n < 1) { 139 break; 140 } 141 142 out.write(buf, 0, n); 144 } 145 super.flush(); 146 } catch (DataFormatException ex) { 147 String msg = ex.getMessage(); 149 if (msg == null) { 150 msg = "Invalid ZLIB data format"; 151 } 152 throw new ZipException (msg); 153 } 154 } 155 } 156 157 165 public void finish() throws IOException { 166 ensureOpen(); 167 168 flush(); 170 if (usesDefaultInflater) { 171 inf.end(); 172 } 173 } 174 175 184 public void write(int b) throws IOException { 185 wbuf[0] = (byte) b; 187 write(wbuf, 0, 1); 188 } 189 190 204 public void write(byte[] b, int off, int len) throws IOException { 205 ensureOpen(); 207 if (b == null) { 208 throw new NullPointerException ("Null buffer for read"); 209 } else if (off < 0 || len < 0 || len > b.length - off) { 210 throw new IndexOutOfBoundsException (); 211 } else if (len == 0) { 212 return; 213 } 214 215 try { 217 for (;;) { 218 int n; 219 220 if (inf.needsInput()) { 222 int part; 223 224 if (len < 1) { 225 break; 226 } 227 228 part = (len < 512 ? len : 512); 229 inf.setInput(b, off, part); 230 off += part; 231 len -= part; 232 } 233 234 do { 236 n = inf.inflate(buf, 0, buf.length); 237 if (n > 0) { 238 out.write(buf, 0, n); 239 } 240 } while (n > 0); 241 242 if (inf.finished()) { 244 break; 245 } 246 if (inf.needsDictionary()) { 247 throw new ZipException ("ZLIB dictionary missing"); 248 } 249 } 250 } catch (DataFormatException ex) { 251 String msg = ex.getMessage(); 253 if (msg == null) { 254 msg = "Invalid ZLIB data format"; 255 } 256 throw new ZipException (msg); 257 } 258 } 259 } 260 | Popular Tags |