1 /* |
1 /* |
2 * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. |
2 * Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
29 class CompressOutputStream extends FilterOutputStream |
29 class CompressOutputStream extends FilterOutputStream |
30 implements CompressConstants |
30 implements CompressConstants |
31 { |
31 { |
32 |
32 |
33 public CompressOutputStream(OutputStream out) { |
33 public CompressOutputStream(OutputStream out) { |
34 super(out); |
34 super(out); |
35 } |
35 } |
36 |
36 |
37 // buffer of 6-bit codes to pack into next 32-bit word |
37 // buffer of 6-bit codes to pack into next 32-bit word |
38 int buf[] = new int[5]; |
38 int buf[] = new int[5]; |
39 |
39 |
40 // number of valid codes pending in buffer |
40 // number of valid codes pending in buffer |
41 int bufPos = 0; |
41 int bufPos = 0; |
42 |
42 |
43 public void write(int b) throws IOException { |
43 public void write(int b) throws IOException { |
44 b &= 0xFF; // force argument to a byte |
44 b &= 0xFF; // force argument to a byte |
45 |
45 |
46 int pos = codeTable.indexOf((char)b); |
46 int pos = codeTable.indexOf((char)b); |
47 if (pos != -1) |
47 if (pos != -1) |
48 writeCode(BASE + pos); |
48 writeCode(BASE + pos); |
49 else { |
49 else { |
50 writeCode(RAW); |
50 writeCode(RAW); |
51 writeCode(b >> 4); |
51 writeCode(b >> 4); |
52 writeCode(b & 0xF); |
52 writeCode(b & 0xF); |
53 } |
53 } |
54 } |
54 } |
55 |
55 |
56 public void write(byte b[], int off, int len) throws IOException { |
56 public void write(byte b[], int off, int len) throws IOException { |
57 /* |
57 /* |
58 * This is quite an inefficient implementation, because it has to |
58 * This is quite an inefficient implementation, because it has to |
59 * call the other write method for every byte in the array. It |
59 * call the other write method for every byte in the array. It |
60 * could be optimized for performance by doing all the processing |
60 * could be optimized for performance by doing all the processing |
61 * in this method. |
61 * in this method. |
62 */ |
62 */ |
63 for (int i = 0; i < len; i++) |
63 for (int i = 0; i < len; i++) |
64 write(b[off + i]); |
64 write(b[off + i]); |
65 } |
65 } |
66 |
66 |
67 public void flush() throws IOException { |
67 public void flush() throws IOException { |
68 while (bufPos > 0) |
68 while (bufPos > 0) |
69 writeCode(NOP); |
69 writeCode(NOP); |
70 } |
70 } |
71 |
71 |
72 private void writeCode(int c) throws IOException { |
72 private void writeCode(int c) throws IOException { |
73 buf[bufPos++] = c; |
73 buf[bufPos++] = c; |
74 if (bufPos == 5) { // write next word when we have 5 codes |
74 if (bufPos == 5) { // write next word when we have 5 codes |
75 int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) | |
75 int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) | |
76 (buf[3] << 6) | buf[4]; |
76 (buf[3] << 6) | buf[4]; |
77 out.write((pack >>> 24) & 0xFF); |
77 out.write((pack >>> 24) & 0xFF); |
78 out.write((pack >>> 16) & 0xFF); |
78 out.write((pack >>> 16) & 0xFF); |
79 out.write((pack >>> 8) & 0xFF); |
79 out.write((pack >>> 8) & 0xFF); |
80 out.write((pack >>> 0) & 0xFF); |
80 out.write((pack >>> 0) & 0xFF); |
81 bufPos = 0; |
81 bufPos = 0; |
82 } |
82 } |
83 } |
83 } |
84 } |
84 } |