comparison src/com/jcraft/jzlib/ZOutputStream.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children 46c2115ae1c8
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2001 Lapo Luchini.
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 THE AUTHORS
21 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 * This program is based on zlib-1.1.3, so all credit should go authors
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
32 * and contributors of zlib.
33 */
34
35 package com.jcraft.jzlib;
36 import java.io.*;
37
38 public class ZOutputStream extends OutputStream {
39
40 protected ZStream z = new ZStream();
41 protected int bufsize = 512;
42 protected int flush = JZlib.Z_NO_FLUSH;
43 protected byte[] buf = new byte[bufsize],
44 buf1 = new byte[1];
45 protected boolean compress;
46
47 protected OutputStream out;
48
49 public ZOutputStream(OutputStream out) {
50 super();
51 this.out = out;
52 z.inflateInit();
53 compress = false;
54 }
55
56 public ZOutputStream(OutputStream out, int level) {
57 this(out, level, false);
58 }
59 public ZOutputStream(OutputStream out, int level, boolean nowrap) {
60 super();
61 this.out = out;
62 z.deflateInit(level, nowrap);
63 compress = true;
64 }
65
66 public void write(int b) throws IOException {
67 buf1[0] = (byte)b;
68 write(buf1, 0, 1);
69 }
70
71 public void write(byte b[], int off, int len) throws IOException {
72 if (len == 0)
73 return;
74
75 int err;
76 z.next_in = b;
77 z.next_in_index = off;
78 z.avail_in = len;
79
80 do {
81 z.next_out = buf;
82 z.next_out_index = 0;
83 z.avail_out = bufsize;
84
85 if (compress)
86 err = z.deflate(flush);
87 else
88 err = z.inflate(flush);
89
90 if (err != JZlib.Z_OK)
91 throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
92
93 out.write(buf, 0, bufsize - z.avail_out);
94 }
95 while (z.avail_in > 0 || z.avail_out == 0);
96 }
97
98 public int getFlushMode() {
99 return (flush);
100 }
101
102 public void setFlushMode(int flush) {
103 this.flush = flush;
104 }
105
106 public void finish() throws IOException {
107 int err;
108
109 do {
110 z.next_out = buf;
111 z.next_out_index = 0;
112 z.avail_out = bufsize;
113
114 if (compress) { err = z.deflate(JZlib.Z_FINISH); }
115 else { err = z.inflate(JZlib.Z_FINISH); }
116
117 if (err != JZlib.Z_STREAM_END && err != JZlib.Z_OK)
118 throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
119
120 if (bufsize - z.avail_out > 0) {
121 out.write(buf, 0, bufsize - z.avail_out);
122 }
123 }
124 while (z.avail_in > 0 || z.avail_out == 0);
125
126 flush();
127 }
128 public void end() {
129 if (z == null)
130 return;
131
132 if (compress) { z.deflateEnd(); }
133 else { z.inflateEnd(); }
134
135 z.free();
136 z = null;
137 }
138 public void close() throws IOException {
139 try {
140 try {finish();}
141 catch (IOException ignored) {}
142 }
143 finally {
144 end();
145 out.close();
146 out = null;
147 }
148 }
149
150 /**
151 * Returns the total number of bytes input so far.
152 */
153 public long getTotalIn() {
154 return z.total_in;
155 }
156
157 /**
158 * Returns the total number of bytes output so far.
159 */
160 public long getTotalOut() {
161 return z.total_out;
162 }
163
164 public void flush() throws IOException {
165 out.flush();
166 }
167
168 }