| [4256] | 1 | /*
|
|---|
| 2 | * CPXOutputStream.java
|
|---|
| 3 | *
|
|---|
| 4 | *
|
|---|
| 5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
|---|
| 6 | * Copyright (C) 2004 Mark McKay
|
|---|
| 7 | *
|
|---|
| 8 | * This library is free software; you can redistribute it and/or
|
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | * License as published by the Free Software Foundation; either
|
|---|
| 11 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This library is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * Lesser General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | * License along with this library; if not, write to the Free Software
|
|---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 21 | *
|
|---|
| 22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 23 | * projects can be found at http://www.kitfox.com
|
|---|
| 24 | *
|
|---|
| 25 | * Created on February 12, 2004, 12:50 PM
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | package com.kitfox.svg.xml.cpx;
|
|---|
| 29 |
|
|---|
| 30 | import java.io.*;
|
|---|
| 31 | import java.util.zip.*;
|
|---|
| 32 | import java.security.*;
|
|---|
| 33 | import javax.crypto.*;
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * @author Mark McKay
|
|---|
| 37 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 38 | */
|
|---|
| 39 | public class CPXOutputStream extends FilterOutputStream implements CPXConsts {
|
|---|
| 40 |
|
|---|
| 41 | Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
|
|---|
| 42 |
|
|---|
| 43 | /** Creates a new instance of CPXOutputStream */
|
|---|
| 44 | public CPXOutputStream(OutputStream os) throws IOException {
|
|---|
| 45 | super(os);
|
|---|
| 46 |
|
|---|
| 47 | //Write magic number
|
|---|
| 48 | os.write(MAGIC_NUMBER);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Writes the specified <code>byte</code> to this output stream.
|
|---|
| 53 | * <p>
|
|---|
| 54 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 55 | * calls the <code>write</code> method of its underlying output stream,
|
|---|
| 56 | * that is, it performs <tt>out.write(b)</tt>.
|
|---|
| 57 | * <p>
|
|---|
| 58 | * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
|
|---|
| 59 | *
|
|---|
| 60 | * @param b the <code>byte</code>.
|
|---|
| 61 | * @exception IOException if an I/O error occurs.
|
|---|
| 62 | */
|
|---|
| 63 | public void write(int b) throws IOException {
|
|---|
| 64 | final byte[] buf = new byte[1];
|
|---|
| 65 | buf[0] = (byte)b;
|
|---|
| 66 | write(buf, 0, 1);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Writes <code>b.length</code> bytes to this output stream.
|
|---|
| 71 | * <p>
|
|---|
| 72 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 73 | * calls its <code>write</code> method of three arguments with the
|
|---|
| 74 | * arguments <code>b</code>, <code>0</code>, and
|
|---|
| 75 | * <code>b.length</code>.
|
|---|
| 76 | * <p>
|
|---|
| 77 | * Note that this method does not call the one-argument
|
|---|
| 78 | * <code>write</code> method of its underlying stream with the single
|
|---|
| 79 | * argument <code>b</code>.
|
|---|
| 80 | *
|
|---|
| 81 | * @param b the data to be written.
|
|---|
| 82 | * @exception IOException if an I/O error occurs.
|
|---|
| 83 | * @see java.io.FilterOutputStream#write(byte[], int, int)
|
|---|
| 84 | */
|
|---|
| 85 | public void write(byte b[]) throws IOException {
|
|---|
| 86 | write(b, 0, b.length);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | byte[] deflateBuffer = new byte[2048];
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Writes <code>len</code> bytes from the specified
|
|---|
| 93 | * <code>byte</code> array starting at offset <code>off</code> to
|
|---|
| 94 | * this output stream.
|
|---|
| 95 | * <p>
|
|---|
| 96 | * The <code>write</code> method of <code>FilterOutputStream</code>
|
|---|
| 97 | * calls the <code>write</code> method of one argument on each
|
|---|
| 98 | * <code>byte</code> to output.
|
|---|
| 99 | * <p>
|
|---|
| 100 | * Note that this method does not call the <code>write</code> method
|
|---|
| 101 | * of its underlying input stream with the same arguments. Subclasses
|
|---|
| 102 | * of <code>FilterOutputStream</code> should provide a more efficient
|
|---|
| 103 | * implementation of this method.
|
|---|
| 104 | *
|
|---|
| 105 | * @param b the data.
|
|---|
| 106 | * @param off the start offset in the data.
|
|---|
| 107 | * @param len the number of bytes to write.
|
|---|
| 108 | * @exception IOException if an I/O error occurs.
|
|---|
| 109 | * @see java.io.FilterOutputStream#write(int)
|
|---|
| 110 | */
|
|---|
| 111 | public void write(byte b[], int off, int len) throws IOException
|
|---|
| 112 | {
|
|---|
| 113 | deflater.setInput(b, off, len);
|
|---|
| 114 |
|
|---|
| 115 | processAllData();
|
|---|
| 116 | /*
|
|---|
| 117 | int numDeflatedBytes;
|
|---|
| 118 | while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
|
|---|
| 119 | {
|
|---|
| 120 | // byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 121 | // out.write(cipherBytes);
|
|---|
| 122 | out.write(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 123 | }
|
|---|
| 124 | */
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | protected void processAllData() throws IOException
|
|---|
| 128 | {
|
|---|
| 129 | int numDeflatedBytes;
|
|---|
| 130 | while ((numDeflatedBytes = deflater.deflate(deflateBuffer)) != 0)
|
|---|
| 131 | {
|
|---|
| 132 | // byte[] cipherBuf = cipher.update(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 133 | // out.write(cipherBytes);
|
|---|
| 134 | out.write(deflateBuffer, 0, numDeflatedBytes);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * Flushes this output stream and forces any buffered output bytes
|
|---|
| 140 | * to be written out to the stream.
|
|---|
| 141 | * <p>
|
|---|
| 142 | * The <code>flush</code> method of <code>FilterOutputStream</code>
|
|---|
| 143 | * calls the <code>flush</code> method of its underlying output stream.
|
|---|
| 144 | *
|
|---|
| 145 | * @exception IOException if an I/O error occurs.
|
|---|
| 146 | * @see java.io.FilterOutputStream#out
|
|---|
| 147 | */
|
|---|
| 148 | public void flush() throws IOException {
|
|---|
| 149 | out.flush();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Closes this output stream and releases any system resources
|
|---|
| 154 | * associated with the stream.
|
|---|
| 155 | * <p>
|
|---|
| 156 | * The <code>close</code> method of <code>FilterOutputStream</code>
|
|---|
| 157 | * calls its <code>flush</code> method, and then calls the
|
|---|
| 158 | * <code>close</code> method of its underlying output stream.
|
|---|
| 159 | *
|
|---|
| 160 | * @exception IOException if an I/O error occurs.
|
|---|
| 161 | * @see java.io.FilterOutputStream#flush()
|
|---|
| 162 | * @see java.io.FilterOutputStream#out
|
|---|
| 163 | */
|
|---|
| 164 | public void close() throws IOException {
|
|---|
| 165 | deflater.finish();
|
|---|
| 166 | processAllData();
|
|---|
| 167 |
|
|---|
| 168 | try {
|
|---|
| 169 | flush();
|
|---|
| 170 | } catch (IOException ignored) {
|
|---|
| 171 | }
|
|---|
| 172 | out.close();
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|