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 CompressInputStream extends FilterInputStream |
| 30 | implements CompressConstants |
| 31 | { |
| 32 | |
| 33 | public CompressInputStream(InputStream in) { |
| 34 | super(in); |
| 35 | } |
| 36 | |
| 37 | // buffer of unpacked 6-bit codes from last 32-word read |
| 38 | int buf[] = new int[5]; |
| 39 | |
| 40 | // position of next code to read in buffer (5 == end of buffer) |
| 41 | int bufPos = 5; |
| 42 | |
| 43 | public int read() throws IOException { |
| 44 | try { |
| 45 | int code; |
| 46 | do { |
| 47 | code = readCode(); |
| 48 | } while (code == NOP); // ignore NOP codes |
| 49 | |
| 50 | if (code >= BASE) |
| 51 | return codeTable.charAt(code - BASE); |
| 52 | else if (code == RAW) { |
| 53 | int high = readCode(); |
| 54 | int low = readCode(); |
| 55 | return (high << 4) | low; |
| 56 | } else |
| 57 | throw new IOException("unknown compression code: " + code); |
| 58 | } catch (EOFException e) { |
| 59 | return -1; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public int read(byte b[], int off, int len) throws IOException { |
| 64 | if (len <= 0) { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int c = read(); |
| 69 | if (c == -1) { |
| 70 | return -1; |
| 71 | } |
| 72 | b[off] = (byte)c; |
| 73 | |
| 74 | int i = 1; |
| 75 | /***** |
| 76 | try { |
| 77 | for (; i < len ; i++) { |
| 78 | c = read(); |
| 79 | if (c == -1) { |
| 80 | break; |
| 81 | } |
| 82 | if (b != null) { |
| 83 | b[off + i] = (byte)c; |
| 84 | } |
| 85 | } |
| 86 | } catch (IOException ee) { |
| 87 | } |
| 88 | *****/ |
| 89 | return i; |
| 90 | } |
| 91 | |
| 92 | private int readCode() throws IOException { |
| 93 | if (bufPos == 5) { |
| 94 | int b1 = in.read(); |
| 95 | int b2 = in.read(); |
| 96 | int b3 = in.read(); |
| 97 | int b4 = in.read(); |
| 98 | if ((b1 | b2 | b3 | b4) < 0) |
| 99 | throw new EOFException(); |
| 100 | int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; |
| 101 | buf[0] = (pack >>> 24) & 0x3F; |
| 102 | buf[1] = (pack >>> 18) & 0x3F; |
| 103 | buf[2] = (pack >>> 12) & 0x3F; |
| 104 | buf[3] = (pack >>> 6) & 0x3F; |
| 105 | buf[4] = (pack >>> 0) & 0x3F; |
| 106 | bufPos = 0; |
| 107 | } |
| 108 | return buf[bufPos++]; |
| 109 | } |
| 110 | } |