Mercurial > 510Connectbot
annotate src/com/jcraft/jzlib/DeflaterOutputStream.java @ 416:07c1da90deab
avoid null pointer
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 27 Oct 2014 13:58:42 -0700 |
parents | 46c2115ae1c8 |
children |
rev | line source |
---|---|
357
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 /* |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved. |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 Redistribution and use in source and binary forms, with or without |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 modification, are permitted provided that the following conditions are met: |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 1. Redistributions of source code must retain the above copyright notice, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 this list of conditions and the following disclaimer. |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
11 2. Redistributions in binary form must reproduce the above copyright |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 notice, this list of conditions and the following disclaimer in |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
13 the documentation and/or other materials provided with the distribution. |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
15 3. The names of the authors may not be used to endorse or promote products |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
16 derived from this software without specific prior written permission. |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
17 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
28 */ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
29 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
30 package com.jcraft.jzlib; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
31 import java.io.*; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
32 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
33 public class DeflaterOutputStream extends FilterOutputStream { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
34 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
35 protected final Deflater deflater; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
36 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
37 protected byte[] buffer; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
38 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
39 private boolean closed = false; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
40 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
41 private boolean syncFlush = false; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
42 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
43 private final byte[] buf1 = new byte[1]; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
44 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
45 protected boolean mydeflater = false; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
46 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
47 private boolean close_out = true; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
48 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
49 protected static final int DEFAULT_BUFSIZE = 512; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
50 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
51 public DeflaterOutputStream(OutputStream out) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
52 this(out, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
53 new Deflater(JZlib.Z_DEFAULT_COMPRESSION), |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
54 DEFAULT_BUFSIZE, true); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
55 mydeflater = true; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
56 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
57 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
58 public DeflaterOutputStream(OutputStream out, Deflater def) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
59 this(out, def, DEFAULT_BUFSIZE, true); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
60 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
61 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
62 public DeflaterOutputStream(OutputStream out, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
63 Deflater deflater, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
64 int size) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
65 this(out, deflater, size, true); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
66 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
67 public DeflaterOutputStream(OutputStream out, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
68 Deflater deflater, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
69 int size, |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
70 boolean close_out) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
71 super(out); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
72 if (out == null || deflater == null) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
73 throw new NullPointerException(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
74 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
75 else if (size <= 0) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
76 throw new IllegalArgumentException("buffer size must be greater than 0"); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
77 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
78 this.deflater = deflater; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
79 buffer = new byte[size]; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
80 this.close_out = close_out; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
81 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
82 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
83 public void write(int b) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
84 buf1[0] = (byte)(b & 0xff); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
85 write(buf1, 0, 1); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
86 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
87 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
88 public void write(byte[] b, int off, int len) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
89 if (deflater.finished()) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
90 throw new IOException("finished"); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
91 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
92 else if (off<0 | len<0 | off+len>b.length) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
93 throw new IndexOutOfBoundsException(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
94 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
95 else if (len == 0) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
96 return; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
97 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
98 else { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
99 int flush = syncFlush ? JZlib.Z_SYNC_FLUSH : JZlib.Z_NO_FLUSH; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
100 deflater.setInput(b, off, len, true); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
101 while (deflater.avail_in>0) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
102 int err = deflate(flush); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
103 if (err == JZlib.Z_STREAM_END) |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
104 break; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
105 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
106 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
107 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
108 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
109 public void finish() throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
110 while (!deflater.finished()) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
111 deflate(JZlib.Z_FINISH); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
112 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
113 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
114 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
115 public void close() throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
116 if (!closed) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
117 finish(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
118 if (mydeflater){ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
119 deflater.end(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
120 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
121 if(close_out) |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
122 out.close(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
123 closed = true; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
124 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
125 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
126 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
127 protected int deflate(int flush) throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
128 deflater.setOutput(buffer, 0, buffer.length); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
129 int err = deflater.deflate(flush); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
130 switch(err) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
131 case JZlib.Z_OK: |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
132 case JZlib.Z_STREAM_END: |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
133 break; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
134 case JZlib.Z_BUF_ERROR: |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
135 if(deflater.avail_in<=0 && flush!=JZlib.Z_FINISH){ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
136 // flush() without any data |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
137 break; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
138 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
139 default: |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
140 throw new IOException("failed to deflate: error="+err+" avail_out="+deflater.avail_out); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
141 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
142 int len = deflater.next_out_index; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
143 if (len > 0) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
144 out.write(buffer, 0, len); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
145 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
146 return err; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
147 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
148 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
149 public void flush() throws IOException { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
150 if (syncFlush && !deflater.finished()) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
151 while (true) { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
152 int err = deflate(JZlib.Z_SYNC_FLUSH); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
153 if (deflater.next_out_index < buffer.length) |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
154 break; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
155 if (err == JZlib.Z_STREAM_END) |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
156 break; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
157 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
158 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
159 out.flush(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
160 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
161 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
162 public long getTotalIn() { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
163 return deflater.getTotalIn(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
164 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
165 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
166 public long getTotalOut() { |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
167 return deflater.getTotalOut(); |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
168 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
169 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
170 public void setSyncFlush(boolean syncFlush){ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
171 this.syncFlush = syncFlush; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
172 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
173 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
174 public boolean getSyncFlush(){ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
175 return this.syncFlush; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
176 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
177 |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
178 public Deflater getDeflater(){ |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
179 return deflater; |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
180 } |
46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
181 } |