| 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 |
|
|---|
| 37 | package com.kitfox.svg.xml.cpx;
|
|---|
| 38 |
|
|---|
| 39 | import java.io.*;
|
|---|
| 40 | import java.util.zip.*;
|
|---|
| 41 | import java.security.*;
|
|---|
| 42 | import javax.crypto.*;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * @author Mark McKay
|
|---|
| 46 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 47 | */
|
|---|
| 48 | public class CPXOutputStream extends FilterOutputStream implements CPXConsts {
|
|---|
| 49 |
|
|---|
| 50 | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
|
|---|
| 51 |
|
|---|
| 52 | /** Creates a new instance of CPXOutputStream */
|
|---|
| 53 | public CPXOutputStream(OutputStream os) throws IOException {
|
|---|
| 54 | super(os);
|
|---|
| 55 |
|
|---|
| 56 | //Write magic number
|
|---|
| 57 | os.write(MAGIC_NUMBER);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Writes the specified <code>byte</code> to this output stream.
|
|---|
| 62 | * <p>
|
|---|
| 63 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 64 | * calls the <code>write</code> method of its underlying output stream,
|
|---|
| 65 | * that is, it performs <tt>out.write(b)</tt>.
|
|---|
| 66 | * <p>
|
|---|
| 67 | * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
|
|---|
| 68 | *
|
|---|
| 69 | * @param b the <code>byte</code>.
|
|---|
| 70 | * @exception IOException if an I/O error occurs.
|
|---|
| 71 | */
|
|---|
| 72 | public void write(int b) throws IOException {
|
|---|
| 73 | final byte[] buf = new byte[1];
|
|---|
| 74 | buf[0] = (byte)b;
|
|---|
| 75 | write(buf, 0, 1);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Writes <code>b.length</code> bytes to this output stream.
|
|---|
| 80 | * <p>
|
|---|
| 81 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 82 | * calls its <code>write</code> method of three arguments with the
|
|---|
| 83 | * arguments <code>b</code>, <code>0</code>, and
|
|---|
| 84 | * <code>b.length</code>.
|
|---|
| 85 | * <p>
|
|---|
| 86 | * Note that this method does not call the one-argument
|
|---|
| 87 | * <code>write</code> method of its underlying stream with the single
|
|---|
| 88 | * argument <code>b</code>.
|
|---|
| 89 | *
|
|---|
| 90 | * @param b the data to be written.
|
|---|
| 91 | * @exception IOException if an I/O error occurs.
|
|---|
| 92 | * @see java.io.FilterOutputStream#write(byte[], int, int)
|
|---|
| 93 | */
|
|---|
| 94 | public void write(byte b[]) throws IOException {
|
|---|
| 95 | write(b, 0, b.length);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | byte[] deflateBuffer = new byte[2048];
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Writes <code>len</code> bytes from the specified
|
|---|
| 102 | * <code>byte</code> array starting at offset <code>off</code> to
|
|---|
| 103 | * this output stream.
|
|---|
| 104 | * <p>
|
|---|
| 105 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 106 | * calls the <code>write</code> method of one argument on each
|
|---|
| 107 | * <code>byte</code> to output.
|
|---|
| 108 | * <p>
|
|---|
| 109 | * Note that this method does not call the <code>write</code> method
|
|---|
| 110 | * of its underlying input stream with the same arguments. Subclasses
|
|---|
| 111 | * of <code>FilterOutputStream</code> should provide a more efficient
|
|---|
| 112 | * implementation of this method.
|
|---|
| 113 | *
|
|---|
| 114 | * @param b the data.
|
|---|
| 115 | * @param off the start offset in the data.
|
|---|
| 116 | * @param len the number of bytes to write.
|
|---|
| 117 | * @exception IOException if an I/O error occurs.
|
|---|
| 118 | * @see java.io.FilterOutputStream#write(int)
|
|---|
| 119 | */
|
|---|
| 120 | public void write(byte b[], int off, int len) throws IOException
|
|---|
| 121 | {
|
|---|
| 122 | deflater.setInput(b, off, len);
|
|---|
| 123 |
|
|---|
| 124 | processAllData();
|
|---|
| 125 | /*
|
|---|
| 126 | int numDeflatedBytes;
|
|---|
| 127 | while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
|
|---|
| 128 | {
|
|---|
| 129 | // byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 130 | // out.write(cipherBytes);
|
|---|
| 131 | out.write(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 132 | }
|
|---|
| 133 | */
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | protected void processAllData() throws IOException
|
|---|
| 137 | {
|
|---|
| 138 | int numDeflatedBytes;
|
|---|
| 139 | while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
|
|---|
| 140 | {
|
|---|
| 141 | // byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 142 | // out.write(cipherBytes);
|
|---|
| 143 | out.write(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Flushes this output stream and forces any buffered output bytes
|
|---|
| 149 | * to be written out to the stream.
|
|---|
| 150 | * <p>
|
|---|
| 151 | * The <code>flush</code> method of <code>FilterOutputStream</code>
|
|---|
| 152 | * calls the <code>flush</code> method of its underlying output stream.
|
|---|
| 153 | *
|
|---|
| 154 | * @exception IOException if an I/O error occurs.
|
|---|
| 155 | * @see java.io.FilterOutputStream#out
|
|---|
| 156 | */
|
|---|
| 157 | public void flush() throws IOException {
|
|---|
| 158 | out.flush();
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Closes this output stream and releases any system resources
|
|---|
| 163 | * associated with the stream.
|
|---|
| 164 | * <p>
|
|---|
| 165 | * The <code>close</code> method of <code>FilterOutputStream</code>
|
|---|
| 166 | * calls its <code>flush</code> method, and then calls the
|
|---|
| 167 | * <code>close</code> method of its underlying output stream.
|
|---|
| 168 | *
|
|---|
| 169 | * @exception IOException if an I/O error occurs.
|
|---|
| 170 | * @see java.io.FilterOutputStream#flush()
|
|---|
| 171 | * @see java.io.FilterOutputStream#out
|
|---|
| 172 | */
|
|---|
| 173 | public void close() throws IOException {
|
|---|
| 174 | deflater.finish();
|
|---|
| 175 | processAllData();
|
|---|
| 176 |
|
|---|
| 177 | try {
|
|---|
| 178 | flush();
|
|---|
| 179 | } catch (IOException ignored) {
|
|---|
| 180 | }
|
|---|
| 181 | out.close();
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|