1 |
/* |
/* |
2 |
* $Id$ |
* CompressedInputStream.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
46 |
* A DTP input stream that implements the FTP compressed transfer mode. |
* A DTP input stream that implements the FTP compressed transfer mode. |
47 |
* |
* |
48 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
49 |
*/ |
*/ |
50 |
class CompressedInputStream extends DTPInputStream |
class CompressedInputStream |
51 |
|
extends DTPInputStream |
52 |
{ |
{ |
53 |
|
|
54 |
static final int EOF = 64; |
static final int EOF = 64; |
65 |
int rep; // the compressed byte |
int rep; // the compressed byte |
66 |
int n = 0; // the number of compressed or raw bytes |
int n = 0; // the number of compressed or raw bytes |
67 |
|
|
68 |
CompressedInputStream (DTP dtp, InputStream in) |
CompressedInputStream(DTP dtp, InputStream in) |
69 |
{ |
{ |
70 |
super (dtp, in); |
super(dtp, in); |
71 |
} |
} |
72 |
|
|
73 |
public int read () throws IOException |
public int read() |
74 |
{ |
throws IOException |
75 |
if (transferComplete) |
{ |
76 |
{ |
if (transferComplete) |
77 |
return -1; |
{ |
78 |
} |
return -1; |
79 |
if (count == -1) |
} |
80 |
{ |
if (count == -1) |
81 |
readHeader (); |
{ |
82 |
} |
readHeader(); |
83 |
if (max < 1) |
} |
84 |
{ |
if (max < 1) |
85 |
close (); |
{ |
86 |
return -1; |
close(); |
87 |
} |
return -1; |
88 |
if (n > 0 && (state == COMPRESSED || state == FILLER)) |
} |
89 |
{ |
if (n > 0 && (state == COMPRESSED || state == FILLER)) |
90 |
n--; |
{ |
91 |
return rep; |
n--; |
92 |
} |
return rep; |
93 |
int c = in.read (); |
} |
94 |
if (c == -1) |
int c = in.read(); |
95 |
{ |
if (c == -1) |
96 |
close (); |
{ |
97 |
} |
close(); |
98 |
count++; |
} |
99 |
if (count >= max) |
count++; |
100 |
{ |
if (count >= max) |
101 |
count = -1; |
{ |
102 |
if (descriptor == EOF) |
count = -1; |
103 |
{ |
if (descriptor == EOF) |
104 |
close (); |
{ |
105 |
} |
close(); |
106 |
} |
} |
107 |
if (c == -1) |
} |
108 |
{ |
if (c == -1) |
109 |
return c; |
{ |
110 |
} |
return c; |
111 |
while (n == 0) // read code header |
} |
112 |
{ |
while (n == 0) // read code header |
113 |
state = (c & 0xc0); |
{ |
114 |
n = (c & 0x3f); |
state = (c & 0xc0); |
115 |
c = in.read (); |
n = (c & 0x3f); |
116 |
if (c == -1) |
c = in.read(); |
117 |
{ |
if (c == -1) |
118 |
return -1; |
{ |
119 |
} |
return -1; |
120 |
} |
} |
121 |
switch (state) |
} |
122 |
{ |
switch (state) |
123 |
case RAW: |
{ |
124 |
break; |
case RAW: |
125 |
case COMPRESSED: |
break; |
126 |
case FILLER: |
case COMPRESSED: |
127 |
rep = c; |
case FILLER: |
128 |
break; |
rep = c; |
129 |
default: |
break; |
130 |
throw new ProtocolException ("Illegal state: " + state); |
default: |
131 |
} |
throw new ProtocolException("Illegal state: " + state); |
132 |
n--; |
} |
133 |
return c; |
n--; |
134 |
} |
return c; |
135 |
|
} |
136 |
public int read (byte[] buf) throws IOException |
|
137 |
{ |
public int read(byte[] buf) |
138 |
return read (buf, 0, buf.length); |
throws IOException |
139 |
} |
{ |
140 |
|
return read(buf, 0, buf.length); |
141 |
public int read (byte[] buf, int off, int len) throws IOException |
} |
142 |
{ |
|
143 |
if (transferComplete) |
public int read(byte[] buf, int off, int len) |
144 |
{ |
throws IOException |
145 |
return -1; |
{ |
146 |
} |
if (transferComplete) |
147 |
if (count == -1) |
{ |
148 |
{ |
return -1; |
149 |
readHeader (); |
} |
150 |
} |
if (count == -1) |
151 |
if (max < 1) |
{ |
152 |
{ |
readHeader(); |
153 |
close (); |
} |
154 |
return -1; |
if (max < 1) |
155 |
} |
{ |
156 |
// TODO improve performance |
close(); |
157 |
for (int i = off; i < len; i++) |
return -1; |
158 |
{ |
} |
159 |
int c = read (); |
// TODO improve performance |
160 |
if (c == -1) |
for (int i = off; i < len; i++) |
161 |
{ |
{ |
162 |
close (); |
int c = read(); |
163 |
return i; |
if (c == -1) |
164 |
} |
{ |
165 |
buf[i] = (byte) c; |
close(); |
166 |
} |
return i; |
167 |
return len; |
} |
168 |
/* |
buf[i] = (byte) c; |
169 |
int l = in.read (buf, off, len); |
} |
170 |
if (l==-1) |
return len; |
171 |
{ |
/* |
172 |
close (); |
int l = in.read (buf, off, len); |
173 |
} |
if (l==-1) |
174 |
count += l; |
{ |
175 |
if (count>=max) |
close (); |
176 |
{ |
} |
177 |
count = -1; |
count += l; |
178 |
if (descriptor==EOF) |
if (count>=max) |
179 |
{ |
{ |
180 |
close (); |
count = -1; |
181 |
} |
if (descriptor==EOF) |
182 |
} |
{ |
183 |
return l; |
close (); |
184 |
*/ |
} |
185 |
} |
} |
186 |
|
return l; |
187 |
|
*/ |
188 |
|
} |
189 |
|
|
190 |
/** |
/** |
191 |
* Reads the block header. |
* Reads the block header. |
192 |
*/ |
*/ |
193 |
void readHeader () throws IOException |
void readHeader() |
194 |
{ |
throws IOException |
195 |
descriptor = in.read (); |
{ |
196 |
int max_hi = in.read (); |
descriptor = in.read(); |
197 |
int max_lo = in.read (); |
int max_hi = in.read(); |
198 |
max = (max_hi << 8) | max_lo; |
int max_lo = in.read(); |
199 |
count = 0; |
max = (max_hi << 8) | max_lo; |
200 |
} |
count = 0; |
201 |
|
} |
202 |
|
|
203 |
/** |
/** |
204 |
* Reads the code header. |
* Reads the code header. |
205 |
*/ |
*/ |
206 |
void readCodeHeader () throws IOException |
void readCodeHeader() |
207 |
{ |
throws IOException |
208 |
int code = in.read (); |
{ |
209 |
state = (code & 0xc0); |
int code = in.read(); |
210 |
n = (code & 0x3f); |
state = (code & 0xc0); |
211 |
} |
n = (code & 0x3f); |
212 |
|
} |
213 |
|
|
214 |
} |
} |
215 |
|
|