source: josm/trunk/src/com/kitfox/svg/xml/cpx/CPXOutputStream.java@ 7676

Last change on this file since 7676 was 7676, checked in by stoecker, 10 years ago

update SVG code to current SVN (fix line endings), see #10479

File size: 6.4 KB
Line 
1/*
2 * SVG Salamander
3 * Copyright (c) 2004, Mark McKay
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * - Redistributions of source code must retain the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
32 * projects can be found at http://www.kitfox.com
33 *
34 * Created on February 12, 2004, 12:50 PM
35 */
36
37package com.kitfox.svg.xml.cpx;
38
39import java.io.*;
40import java.util.zip.*;
41
42/**
43 * @author Mark McKay
44 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
45 */
46public class CPXOutputStream extends FilterOutputStream implements CPXConsts {
47
48 Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
49
50 /** Creates a new instance of CPXOutputStream */
51 public CPXOutputStream(OutputStream os) throws IOException {
52 super(os);
53
54 //Write magic number
55 os.write(MAGIC_NUMBER);
56 }
57
58 /**
59 * Writes the specified <code>byte</code> to this output stream.
60 * <p>
61 * The <code>write</code> method of <code>FilterOutputStream</code>
62 * calls the <code>write</code> method of its underlying output stream,
63 * that is, it performs <tt>out.write(b)</tt>.
64 * <p>
65 * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
66 *
67 * @param b the <code>byte</code>.
68 * @exception IOException if an I/O error occurs.
69 */
70 public void write(int b) throws IOException {
71 final byte[] buf = new byte[1];
72 buf[0] = (byte)b;
73 write(buf, 0, 1);
74 }
75
76 /**
77 * Writes <code>b.length</code> bytes to this output stream.
78 * <p>
79 * The <code>write</code> method of <code>FilterOutputStream</code>
80 * calls its <code>write</code> method of three arguments with the
81 * arguments <code>b</code>, <code>0</code>, and
82 * <code>b.length</code>.
83 * <p>
84 * Note that this method does not call the one-argument
85 * <code>write</code> method of its underlying stream with the single
86 * argument <code>b</code>.
87 *
88 * @param b the data to be written.
89 * @exception IOException if an I/O error occurs.
90 * @see java.io.FilterOutputStream#write(byte[], int, int)
91 */
92 public void write(byte b[]) throws IOException {
93 write(b, 0, b.length);
94 }
95
96 byte[] deflateBuffer = new byte[2048];
97
98 /**
99 * Writes <code>len</code> bytes from the specified
100 * <code>byte</code> array starting at offset <code>off</code> to
101 * this output stream.
102 * <p>
103 * The <code>write</code> method of <code>FilterOutputStream</code>
104 * calls the <code>write</code> method of one argument on each
105 * <code>byte</code> to output.
106 * <p>
107 * Note that this method does not call the <code>write</code> method
108 * of its underlying input stream with the same arguments. Subclasses
109 * of <code>FilterOutputStream</code> should provide a more efficient
110 * implementation of this method.
111 *
112 * @param b the data.
113 * @param off the start offset in the data.
114 * @param len the number of bytes to write.
115 * @exception IOException if an I/O error occurs.
116 * @see java.io.FilterOutputStream#write(int)
117 */
118 public void write(byte b[], int off, int len) throws IOException
119 {
120 deflater.setInput(b, off, len);
121
122 processAllData();
123 /*
124 int numDeflatedBytes;
125 while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
126 {
127// byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
128// out.write(cipherBytes);
129out.write(deflateBuffer, 0, numDeflatedBytes);
130 }
131 */
132 }
133
134 protected void processAllData() throws IOException
135 {
136 int numDeflatedBytes;
137 while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
138 {
139// byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
140// out.write(cipherBytes);
141out.write(deflateBuffer, 0, numDeflatedBytes);
142 }
143 }
144
145 /**
146 * Flushes this output stream and forces any buffered output bytes
147 * to be written out to the stream.
148 * <p>
149 * The <code>flush</code> method of <code>FilterOutputStream</code>
150 * calls the <code>flush</code> method of its underlying output stream.
151 *
152 * @exception IOException if an I/O error occurs.
153 * @see java.io.FilterOutputStream#out
154 */
155 public void flush() throws IOException {
156 out.flush();
157 }
158
159 /**
160 * Closes this output stream and releases any system resources
161 * associated with the stream.
162 * <p>
163 * The <code>close</code> method of <code>FilterOutputStream</code>
164 * calls its <code>flush</code> method, and then calls the
165 * <code>close</code> method of its underlying output stream.
166 *
167 * @exception IOException if an I/O error occurs.
168 * @see java.io.FilterOutputStream#flush()
169 * @see java.io.FilterOutputStream#out
170 */
171 public void close() throws IOException {
172 deflater.finish();
173 processAllData();
174
175 try {
176 flush();
177 } catch (IOException ignored) {
178 }
179 out.close();
180 }
181}
Note: See TracBrowser for help on using the repository browser.