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