author | Claus Gittinger <cg@exept.de> |
Fri, 01 Oct 2004 09:52:49 +0200 | |
changeset 1482 | cea67ec35cc2 |
parent 1481 | 789e0450fb53 |
child 1782 | 5277788b0507 |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
1195 | 13 |
"{ Package: 'stx:libbasic2' }" |
1059 | 14 |
|
15 |
Stream subclass:#CompressionStream |
|
1193 | 16 |
instanceVariableNames:'onStream hitEOF binary position readLimit mode inputBytes |
17 |
outputBytes zstream' |
|
18 |
classVariableNames:'BlockSize' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compress' |
|
1059 | 21 |
! |
22 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
23 |
!CompressionStream class methodsFor:'documentation'! |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
24 |
|
1308 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
39 |
documentation |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
40 |
" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
41 |
Abstract superclass of streams that compress or deconpress data |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
42 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
43 |
[author:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
44 |
Claus Atzkern |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
45 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
46 |
[instance variables:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
47 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
48 |
[class variables:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
49 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
50 |
[see also:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
51 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
52 |
" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
53 |
! ! |
1193 | 54 |
|
1059 | 55 |
!CompressionStream class methodsFor:'initialization'! |
56 |
||
57 |
initialize |
|
58 |
BlockSize := 6. |
|
59 |
! ! |
|
60 |
||
61 |
!CompressionStream class methodsFor:'instance creation'! |
|
62 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
63 |
readOpenOn:aStream |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
64 |
"open to read data from an compressed stream" |
1193 | 65 |
|
1059 | 66 |
^ self basicNew openWithMode:#readonly on:aStream |
67 |
! |
|
68 |
||
69 |
writeOpenOn:aStream |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
70 |
"open to write data compressed to stream" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
71 |
|
1059 | 72 |
^ self basicNew openWithMode:#writeonly on:aStream |
73 |
! ! |
|
74 |
||
75 |
!CompressionStream methodsFor:'accessing'! |
|
76 |
||
77 |
binary |
|
78 |
"switch to binary mode - default is text |
|
79 |
" |
|
80 |
binary := true. |
|
81 |
! |
|
82 |
||
83 |
text |
|
1193 | 84 |
"switch to text mode - default is text" |
85 |
||
1059 | 86 |
binary := false. |
87 |
! ! |
|
88 |
||
89 |
!CompressionStream methodsFor:'error handling'! |
|
90 |
||
91 |
errorNotOpen |
|
1193 | 92 |
"report an error, that the stream has not been opened" |
93 |
||
1059 | 94 |
self zerror:'not open'. |
95 |
! |
|
96 |
||
97 |
errorReadOnly |
|
1193 | 98 |
"report an error, that the stream is a readOnly stream" |
99 |
||
1059 | 100 |
self zerror:'is readonly' |
101 |
! |
|
102 |
||
103 |
errorWriteOnly |
|
1193 | 104 |
"report an error, that the stream is a writeOnly stream" |
105 |
||
1059 | 106 |
self zerror:'is writeonly' |
107 |
! |
|
108 |
||
109 |
invalidArgument |
|
1193 | 110 |
"called if a method is invoked with invalid parameters" |
111 |
||
1059 | 112 |
self zerror:'invalid arguments'. |
113 |
! |
|
114 |
||
1481 | 115 |
pastEndRead |
1059 | 116 |
self zerror:'end of stream'. |
117 |
! |
|
118 |
||
119 |
zerror:anError |
|
120 |
|error| |
|
121 |
||
122 |
zstream isNil ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
123 |
error := 'not open'. |
1059 | 124 |
] ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
125 |
anError isNumber ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
126 |
anError == 1 ifTrue:[ error := 'stream at end' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
127 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
128 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
129 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
130 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
131 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
132 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
133 |
ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
134 |
error := 'compressing error: ', anError printString |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
135 |
]]]]]]]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
136 |
] ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
137 |
error := anError printString |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
138 |
]. |
1059 | 139 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
140 |
StreamError raiseErrorString:(self class name , ': ', error). |
1059 | 141 |
! ! |
142 |
||
143 |
!CompressionStream methodsFor:'finalization'! |
|
144 |
||
145 |
executor |
|
146 |
"redefined to return a lightweight copy |
|
1193 | 147 |
- all we need is the memory handle" |
148 |
||
1059 | 149 |
^ self class basicNew finalizeCopy:zstream. |
150 |
! |
|
151 |
||
152 |
finalize |
|
1061 | 153 |
"the compressin-stream was garbage collected; |
1193 | 154 |
close the underlying zip-stream" |
155 |
||
1061 | 156 |
self closeZStream. |
1059 | 157 |
! |
158 |
||
159 |
finalizeCopy:aZStream |
|
1193 | 160 |
"used for finalization to close the underlying zip-stream" |
161 |
||
1059 | 162 |
zstream := aZStream. |
163 |
! ! |
|
164 |
||
165 |
!CompressionStream methodsFor:'low level'! |
|
166 |
||
1079 | 167 |
z_nextAvailableInto:aCollection startingAt:offset |
168 |
"read the next available bytes into a collection, a string or byteArray; |
|
1193 | 169 |
returns the size read" |
170 |
||
1079 | 171 |
|start count avail| |
172 |
||
173 |
avail := readLimit - position. |
|
174 |
avail > 0 ifFalse:[^ 0]. |
|
175 |
||
176 |
count := aCollection size - offset + 1. |
|
177 |
||
178 |
count > 0 ifFalse:[ |
|
1195 | 179 |
count < 0 ifTrue:[ |
180 |
self zerror:'invalid arguments' |
|
181 |
]. |
|
182 |
^ 0 |
|
1079 | 183 |
]. |
184 |
count := avail min:count. |
|
185 |
start := position. |
|
186 |
position := position + count. |
|
187 |
||
188 |
%{ unsigned char * _dstPt; |
|
189 |
||
190 |
if( __isBytes(aCollection) ) { |
|
1195 | 191 |
_dstPt = (unsigned char *) (__ByteArrayInstPtr(aCollection)->ba_element); |
1079 | 192 |
} else if (__isString(aCollection)) { |
1195 | 193 |
_dstPt = (unsigned char *) (__stringVal( aCollection)); |
1079 | 194 |
} else |
1195 | 195 |
_dstPt = (unsigned char *) 0; |
1079 | 196 |
|
197 |
if( _dstPt ) |
|
198 |
{ |
|
1195 | 199 |
int _loop, _count, _offset; |
200 |
unsigned char * _srcPt; |
|
201 |
OBJ _srcObj = __INST( outputBytes ); |
|
1079 | 202 |
|
1195 | 203 |
_offset = __intVal( offset ); |
204 |
_dstPt = _dstPt + _offset - 1; |
|
1079 | 205 |
|
1195 | 206 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj ); |
207 |
_srcPt += __intVal( start ); |
|
208 |
_count = __intVal( count ); |
|
1079 | 209 |
|
1195 | 210 |
for( _loop = 0; _loop < _count; ++_loop ) |
211 |
* _dstPt++ = * _srcPt++; |
|
1079 | 212 |
|
1195 | 213 |
RETURN(__MKSMALLINT(_count)); |
1079 | 214 |
} |
215 |
%}. |
|
216 |
||
217 |
^ self zerror:'invalid argument' |
|
218 |
! |
|
219 |
||
1193 | 220 |
zclose |
221 |
"low level close of the zip stream" |
|
222 |
||
223 |
^ self subclassResponsibility |
|
224 |
! |
|
225 |
||
226 |
zdeflate |
|
227 |
"low level - deflate |
|
228 |
returns false if the deflate operation is finished otherwise true" |
|
229 |
||
230 |
^ self subclassResponsibility |
|
231 |
! |
|
232 |
||
233 |
zdeflateInit |
|
234 |
"low level - deflateInit |
|
235 |
initialize the deflate mode, write header" |
|
236 |
||
237 |
^ self subclassResponsibility |
|
238 |
! |
|
239 |
||
240 |
zget_avail_out |
|
241 |
"low level - get the number of available out bytes" |
|
242 |
||
243 |
^ self subclassResponsibility |
|
244 |
! |
|
245 |
||
246 |
zinflate |
|
247 |
"low level - inflate |
|
248 |
returns nil if at uncompress is finished, or the number of |
|
249 |
available bytes in the output-buffer" |
|
250 |
||
251 |
^ self subclassResponsibility |
|
252 |
! |
|
253 |
||
254 |
zinflateInit |
|
255 |
"low level - inflateInit |
|
256 |
initialize the inflate mode, read and check header" |
|
257 |
||
258 |
^ self subclassResponsibility |
|
259 |
! |
|
260 |
||
1059 | 261 |
zopen |
262 |
"low level - opens the zip stream |
|
1193 | 263 |
create the resources" |
264 |
||
1059 | 265 |
^ self subclassResponsibility |
266 |
! |
|
267 |
||
268 |
zset_avail_in:count |
|
1193 | 269 |
"set the 'avail_in' and compute the crc" |
270 |
||
1059 | 271 |
^ self subclassResponsibility |
272 |
! ! |
|
273 |
||
274 |
!CompressionStream methodsFor:'private'! |
|
275 |
||
1061 | 276 |
closeZStream |
1193 | 277 |
"close the zip-stream" |
278 |
||
1061 | 279 |
onStream := mode := nil. |
280 |
hitEOF := true. |
|
281 |
||
282 |
zstream ifNotNil:[ |
|
1195 | 283 |
self unregisterForFinalization. |
284 |
self zclose. |
|
285 |
zstream := nil. |
|
1061 | 286 |
]. |
287 |
! |
|
288 |
||
1081 | 289 |
contentsSpecies |
1193 | 290 |
"return the kind of object to be returned by sub-collection builders" |
291 |
||
292 |
binary == true ifTrue:[^ ByteArray]. |
|
293 |
^ String |
|
1081 | 294 |
! |
295 |
||
1059 | 296 |
onStreamPutBytes:count from:data |
1193 | 297 |
"write compressed data to the (output) stream" |
298 |
||
1059 | 299 |
onStream nextPutBytes:count from:data startingAt:1 |
300 |
! ! |
|
301 |
||
302 |
!CompressionStream methodsFor:'queries'! |
|
303 |
||
304 |
atEnd |
|
1193 | 305 |
"return true if the end of the compressed input stream has been reached" |
306 |
||
1059 | 307 |
^ hitEOF ~~ false |
308 |
! |
|
309 |
||
1062 | 310 |
canReadWithoutBlocking |
311 |
"returns true if data are available for reading; |
|
312 |
false if the stream is at end. |
|
1193 | 313 |
updates the readLimit and position" |
314 |
||
1062 | 315 |
mode == #readonly ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
316 |
self errorWriteOnly |
1062 | 317 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
318 |
hitEOF == true ifTrue:[^ false]. |
1062 | 319 |
|
320 |
position >= readLimit ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
321 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
322 |
|n| |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
323 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
324 |
n == 0 ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
325 |
hitEOF := true. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
326 |
^ false |
1482 | 327 |
"/ self pastEndRead |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
328 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
329 |
self zset_avail_in:n. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
330 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
331 |
readLimit isNil ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
332 |
hitEOF := true. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
333 |
^ false |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
334 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
335 |
position := 0. |
1062 | 336 |
]. |
337 |
^ true |
|
338 |
! |
|
339 |
||
1059 | 340 |
isBinary |
341 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 342 |
The default when created is false" |
343 |
||
1059 | 344 |
^ binary |
345 |
! |
|
346 |
||
347 |
isOpen |
|
1193 | 348 |
"return true, if this stream is open" |
349 |
||
1059 | 350 |
^ onStream notNil |
351 |
! |
|
352 |
||
1193 | 353 |
isReadable |
354 |
"return true, if this stream can be read from" |
|
355 |
||
1059 | 356 |
^ mode == #readonly |
357 |
! |
|
358 |
||
1193 | 359 |
isWritable |
360 |
"return true, if this stream can be written to" |
|
361 |
||
1059 | 362 |
^ mode == #writeonly |
363 |
! ! |
|
364 |
||
365 |
!CompressionStream methodsFor:'reading'! |
|
366 |
||
367 |
contents |
|
1193 | 368 |
"return the entire contents of and close the stream" |
369 |
||
1081 | 370 |
|species stream bfsize buffer count| |
1059 | 371 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
372 |
mode == #readonly ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
373 |
self errorWriteOnly |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
374 |
]. |
1062 | 375 |
bfsize := outputBytes size. |
1059 | 376 |
|
1081 | 377 |
species := self contentsSpecies. |
378 |
buffer := species new:bfsize. |
|
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
379 |
stream := WriteStream on:(species new:bfsize). |
1079 | 380 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
381 |
[self canReadWithoutBlocking] whileTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
382 |
count := self z_nextAvailableInto:buffer startingAt:1. |
1079 | 383 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
384 |
count == bfsize ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
385 |
stream nextPutAll:buffer. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
386 |
] ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
387 |
count > 0 ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
388 |
stream nextPutAll:buffer startingAt:1 to:count. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
389 |
] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
390 |
]. |
1059 | 391 |
]. |
1062 | 392 |
self close. |
1193 | 393 |
^ stream contents |
1059 | 394 |
! |
395 |
||
396 |
next |
|
397 |
"return the next element, a character or byte (textmode) |
|
1193 | 398 |
return nil, if there are no more elements" |
399 |
||
1059 | 400 |
|byte| |
401 |
||
1062 | 402 |
self canReadWithoutBlocking ifFalse:[ |
1195 | 403 |
"there is no more element; the stream is at end" |
404 |
^ nil |
|
1059 | 405 |
]. |
406 |
position := position + 1. |
|
407 |
byte := outputBytes at:position. |
|
408 |
||
409 |
binary ifTrue:[^ byte ]. |
|
1193 | 410 |
^ Character value:byte |
1082 | 411 |
! |
412 |
||
413 |
next:n |
|
414 |
"return the next count elements of the stream as a collection. |
|
1193 | 415 |
Redefined to return a String or ByteArray and for optimization" |
416 |
||
1082 | 417 |
|data count offset species| |
418 |
||
419 |
species := self contentsSpecies. |
|
420 |
||
421 |
self canReadWithoutBlocking ifFalse:[ |
|
1195 | 422 |
^ species new |
1082 | 423 |
]. |
424 |
data := species new:n. |
|
425 |
offset := 1. |
|
426 |
||
1193 | 427 |
[self canReadWithoutBlocking] whileTrue:[ |
1195 | 428 |
count := self z_nextAvailableInto:data startingAt:offset. |
429 |
offset := count + offset. |
|
430 |
offset > n ifTrue:[^ data] |
|
1082 | 431 |
]. |
432 |
^ data copyFrom:1 to:(offset - 1) |
|
433 |
! |
|
434 |
||
435 |
skip:count |
|
436 |
"skip count objects, return the receiver |
|
1193 | 437 |
redefined for optimization" |
438 |
||
1082 | 439 |
|n avail| |
440 |
||
441 |
n := count. |
|
442 |
||
1193 | 443 |
n <= 0 ifTrue:[ |
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
444 |
n ~~ 0 ifTrue:[ |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
445 |
"dont know how to unread ..." |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
446 |
PositionError raiseRequest |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
447 |
]. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
448 |
^ self |
1083 | 449 |
]. |
450 |
||
1193 | 451 |
[self canReadWithoutBlocking] whileTrue:[ |
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
452 |
avail := readLimit - position. |
1082 | 453 |
|
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
454 |
avail >= n ifTrue:[ |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
455 |
position := position + n. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
456 |
^ self |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
457 |
]. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
458 |
position := readLimit := 0. "/ reset input |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
459 |
n := n - avail. |
1082 | 460 |
]. |
1059 | 461 |
! ! |
462 |
||
463 |
!CompressionStream methodsFor:'startup & release'! |
|
464 |
||
465 |
close |
|
1193 | 466 |
"close the zip-stream" |
467 |
||
1059 | 468 |
hitEOF := true. |
1193 | 469 |
zstream notNil ifTrue:[ |
1195 | 470 |
self flush. |
471 |
self closeZStream. |
|
1059 | 472 |
]. |
473 |
! |
|
474 |
||
475 |
openWithMode:aMode on:aStream |
|
1061 | 476 |
"open the zip-stream on a stream |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
477 |
#readonly uncompress the data derived from the read-stream, aStream |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
478 |
#writeonly compress the data and write to the write-stream, aStream |
1061 | 479 |
" |
1059 | 480 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
481 |
aStream isNil ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
482 |
^ self errorNotOpen |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
483 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
484 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
485 |
onStream := aStream. |
1059 | 486 |
mode := aMode. |
487 |
outputBytes := ExternalBytes unprotectedNew:8192. |
|
488 |
inputBytes := ExternalBytes unprotectedNew:8192. |
|
489 |
readLimit := position := 0. |
|
490 |
binary := false. |
|
491 |
||
492 |
self zopen. |
|
493 |
self registerForFinalization. |
|
494 |
||
495 |
hitEOF := false. |
|
496 |
||
497 |
aMode == #readonly ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
498 |
self zinflateInit. |
1059 | 499 |
] ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
500 |
self zdeflateInit |
1059 | 501 |
]. |
502 |
! ! |
|
503 |
||
504 |
!CompressionStream methodsFor:'writing'! |
|
505 |
||
1062 | 506 |
contents:contents |
1193 | 507 |
"write the entire contents to and close the stream" |
508 |
||
509 |
contents do:[:c| self nextPut:c]. |
|
1062 | 510 |
self close. |
511 |
! |
|
512 |
||
1059 | 513 |
flush |
1193 | 514 |
"flush the input and output buffer" |
515 |
||
1059 | 516 |
|continue| |
517 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
518 |
self isWritable ifFalse:[^ self]. |
1059 | 519 |
self zset_avail_in:position. |
520 |
||
521 |
position := 0. |
|
522 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
523 |
[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
524 |
|count| |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
525 |
continue := self zdeflate. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
526 |
count := self zget_avail_out. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
527 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
528 |
count > 0 ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
529 |
self onStreamPutBytes:count from:outputBytes |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
530 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
531 |
] doWhile:continue. |
1059 | 532 |
! |
533 |
||
1193 | 534 |
nextPut:aByteOrCharacter |
535 |
"write the argument, aByteOrCharacter" |
|
1059 | 536 |
|
1193 | 537 |
position == inputBytes size ifTrue:[self flush]. |
538 |
position := position + 1. |
|
539 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
540 |
! ! |
|
1059 | 541 |
|
1193 | 542 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 543 |
|
1193 | 544 |
version |
1482 | 545 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.18 2004-10-01 07:52:49 cg Exp $' |
1059 | 546 |
! ! |
547 |
||
548 |
CompressionStream initialize! |