40 |
class CompressedInputStream extends DTPInputStream |
class CompressedInputStream extends DTPInputStream |
41 |
{ |
{ |
42 |
|
|
43 |
static final int EOF = 64; |
static final int EOF = 64; |
44 |
|
|
45 |
static final int RAW = 0x00; |
static final int RAW = 0x00; |
46 |
static final int COMPRESSED = 0x80; |
static final int COMPRESSED = 0x80; |
47 |
static final int FILLER = 0xc0; |
static final int FILLER = 0xc0; |
48 |
|
|
49 |
int descriptor; |
int descriptor; |
50 |
int max = -1; |
int max = -1; |
51 |
int count = -1; |
int count = -1; |
52 |
|
|
53 |
int state = RAW; // RAW | STATE | FILLER |
int state = RAW; // RAW | STATE | FILLER |
54 |
int rep; // the compressed byte |
int rep; // the compressed byte |
55 |
int n = 0; // the number of compressed or raw bytes |
int n = 0; // the number of compressed or raw bytes |
56 |
|
|
57 |
CompressedInputStream(DTP dtp, InputStream in) |
CompressedInputStream(DTP dtp, InputStream in) |
58 |
{ |
{ |
59 |
super(dtp, in); |
super(dtp, in); |
60 |
} |
} |
61 |
|
|
62 |
public int read() throws IOException |
public int read() throws IOException |
63 |
{ |
{ |
64 |
if (transferComplete) |
if (transferComplete) |
65 |
return -1; |
return -1; |
66 |
if (count == -1) |
if (count == -1) |
67 |
readHeader(); |
readHeader(); |
68 |
if (max < 1) |
if (max < 1) |
69 |
{ |
{ |
70 |
close(); |
close(); |
71 |
return -1; |
return -1; |
72 |
} |
} |
73 |
if (n > 0 && (state == COMPRESSED || state == FILLER)) |
if (n > 0 && (state == COMPRESSED || state == FILLER)) |
74 |
{ |
{ |
75 |
n--; |
n--; |
76 |
return rep; |
return rep; |
77 |
} |
} |
78 |
int c = super.read(); |
int c = in.read(); |
79 |
if (c == -1) |
if (c == -1) |
80 |
close(); |
close(); |
81 |
count++; |
count++; |
82 |
if (count >= max) |
if (count >= max) |
83 |
{ |
{ |
84 |
count = -1; |
count = -1; |
85 |
if (descriptor == EOF) |
if (descriptor == EOF) |
86 |
close(); |
close(); |
87 |
} |
} |
88 |
if (c == -1) |
if (c == -1) |
89 |
return c; |
return c; |
90 |
while (n == 0) // read code header |
while (n == 0) // read code header |
91 |
{ |
{ |
92 |
state = (c & 0xc0); |
state = (c & 0xc0); |
93 |
n = (c & 0x3f); |
n = (c & 0x3f); |
94 |
c = super.read(); |
c = in.read(); |
95 |
if (c == -1) |
if (c == -1) |
96 |
return -1; |
return -1; |
97 |
} |
} |
98 |
switch (state) |
switch (state) |
99 |
{ |
{ |
100 |
case RAW: |
case RAW: |
101 |
break; |
break; |
102 |
case COMPRESSED: |
case COMPRESSED: |
103 |
case FILLER: |
case FILLER: |
104 |
rep = c; |
rep = c; |
105 |
break; |
break; |
106 |
default: |
default: |
107 |
throw new ProtocolException("Illegal state: " + state); |
throw new ProtocolException("Illegal state: " + state); |
108 |
} |
} |
109 |
n--; |
n--; |
110 |
return c; |
return c; |
111 |
} |
} |
112 |
|
|
113 |
public int read(byte[]buf) throws IOException |
public int read(byte[] buf) throws IOException |
114 |
{ |
{ |
115 |
return read(buf, 0, buf.length); |
return read(buf, 0, buf.length); |
116 |
} |
} |
117 |
|
|
118 |
public int read(byte[]buf, int off, int len) throws IOException |
public int read(byte[] buf, int off, int len) throws IOException |
119 |
{ |
{ |
120 |
if (transferComplete) |
if (transferComplete) |
121 |
return -1; |
return -1; |
122 |
if (count == -1) |
if (count == -1) |
123 |
readHeader(); |
readHeader(); |
124 |
if (max < 1) |
if (max < 1) |
125 |
{ |
{ |
126 |
close(); |
close(); |
127 |
return -1; |
return -1; |
128 |
} |
} |
129 |
// TODO improve performance |
// TODO improve performance |
130 |
for (int i = off; i < len; i++) |
for (int i = off; i < len; i++) |
131 |
{ |
{ |
132 |
int c = read(); |
int c = read(); |
133 |
if (c == -1) |
if (c == -1) |
134 |
{ |
{ |
135 |
close(); |
close(); |
136 |
return i; |
return i; |
137 |
} |
} |
138 |
buf[i] = (byte) c; |
buf[i] = (byte) c; |
139 |
} |
} |
140 |
return len; |
return len; |
141 |
/* |
/* |
142 |
int l = super.read(buf, off, len); |
int l = in.read(buf, off, len); |
143 |
if (l==-1) |
if (l==-1) |
144 |
close(); |
close(); |
145 |
count += l; |
count += l; |
146 |
if (count>=max) |
if (count>=max) |
147 |
{ |
{ |
148 |
count = -1; |
count = -1; |
149 |
if (descriptor==EOF) |
if (descriptor==EOF) |
150 |
close(); |
close(); |
151 |
} |
} |
152 |
return l; |
return l; |
153 |
|
*/ |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
|
* Reads the block header. |
158 |
*/ |
*/ |
159 |
} |
void readHeader() throws IOException |
160 |
|
{ |
161 |
/** |
descriptor = in.read(); |
162 |
* Reads the block header. |
int max_hi = in.read(); |
163 |
*/ |
int max_lo = in.read(); |
164 |
void readHeader() throws IOException |
max = (max_hi << 8) | max_lo; |
165 |
{ |
count = 0; |
166 |
descriptor = super.read(); |
} |
167 |
int max_hi = super.read(); |
|
168 |
int max_lo = super.read(); |
/** |
169 |
max = (max_hi << 8) | max_lo; |
* Reads the code header. |
170 |
count = 0; |
*/ |
171 |
} |
void readCodeHeader() throws IOException |
172 |
|
{ |
173 |
/** |
int code = in.read(); |
174 |
* Reads the code header. |
state = (code & 0xc0); |
175 |
*/ |
n = (code & 0x3f); |
176 |
void readCodeHeader() throws IOException |
} |
177 |
{ |
|
|
int code = super.read(); |
|
|
state = (code & 0xc0); |
|
|
n = (code & 0x3f); |
|
|
} |
|
|
|
|
178 |
} |
} |