J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | */ |
| 26 | |
| 27 | import java.io.*; |
| 28 | |
| 29 | class CompressOutputStream extends FilterOutputStream |
| 30 | implements CompressConstants |
| 31 | { |
| 32 | |
| 33 | public CompressOutputStream(OutputStream out) { |
| 34 | super(out); |
| 35 | } |
| 36 | |
| 37 | // buffer of 6-bit codes to pack into next 32-bit word |
| 38 | int buf[] = new int[5]; |
| 39 | |
| 40 | // number of valid codes pending in buffer |
| 41 | int bufPos = 0; |
| 42 | |
| 43 | public void write(int b) throws IOException { |
| 44 | b &= 0xFF; // force argument to a byte |
| 45 | |
| 46 | int pos = codeTable.indexOf((char)b); |
| 47 | if (pos != -1) |
| 48 | writeCode(BASE + pos); |
| 49 | else { |
| 50 | writeCode(RAW); |
| 51 | writeCode(b >> 4); |
| 52 | writeCode(b & 0xF); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public void write(byte b[], int off, int len) throws IOException { |
| 57 | /* |
| 58 | * This is quite an inefficient implementation, because it has to |
| 59 | * call the other write method for every byte in the array. It |
| 60 | * could be optimized for performance by doing all the processing |
| 61 | * in this method. |
| 62 | */ |
| 63 | for (int i = 0; i < len; i++) |
| 64 | write(b[off + i]); |
| 65 | } |
| 66 | |
| 67 | public void flush() throws IOException { |
| 68 | while (bufPos > 0) |
| 69 | writeCode(NOP); |
| 70 | } |
| 71 | |
| 72 | private void writeCode(int c) throws IOException { |
| 73 | buf[bufPos++] = c; |
| 74 | if (bufPos == 5) { // write next word when we have 5 codes |
| 75 | int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) | |
| 76 | (buf[3] << 6) | buf[4]; |
| 77 | out.write((pack >>> 24) & 0xFF); |
| 78 | out.write((pack >>> 16) & 0xFF); |
| 79 | out.write((pack >>> 8) & 0xFF); |
| 80 | out.write((pack >>> 0) & 0xFF); |
| 81 | bufPos = 0; |
| 82 | } |
| 83 | } |
| 84 | } |