author | Claus Gittinger <cg@exept.de> |
Mon, 13 Feb 2017 20:44:08 +0100 | |
changeset 4317 | 03c69a595f88 |
parent 4311 | 55d6e8abb73b |
child 4644 | a55695d86845 |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
2190 | 3 |
All Rights Reserved |
1308 | 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 |
" |
|
1195 | 12 |
"{ Package: 'stx:libbasic2' }" |
1059 | 13 |
|
4205 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1059 | 16 |
Stream subclass:#CompressionStream |
1193 | 17 |
instanceVariableNames:'onStream hitEOF binary position readLimit mode inputBytes |
4205 | 18 |
outputBytes zstream suppressHeaderAndChecksum propagateClose' |
1193 | 19 |
classVariableNames:'BlockSize' |
20 |
poolDictionaries:'' |
|
4258 | 21 |
category:'Streams-Compressed' |
1059 | 22 |
! |
23 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
24 |
!CompressionStream class methodsFor:'documentation'! |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
25 |
|
1308 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
2190 | 29 |
All Rights Reserved |
1308 | 30 |
|
31 |
This software is furnished under a license and may be used |
|
32 |
only in accordance with the terms of that license and with the |
|
33 |
inclusion of the above copyright notice. This software may not |
|
34 |
be provided or otherwise made available to, or used by, any |
|
35 |
other person. No title to or ownership of the software is |
|
36 |
hereby transferred. |
|
37 |
" |
|
38 |
! |
|
39 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
40 |
documentation |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
41 |
" |
4258 | 42 |
Abstract superclass of streams that compress or decompress data |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
43 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
44 |
[author:] |
3182 | 45 |
Claus Atzkern |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
46 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
47 |
[instance variables:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
48 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
49 |
[class variables:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
50 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
51 |
[see also:] |
3182 | 52 |
ZipStream |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
53 |
" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
54 |
! ! |
1193 | 55 |
|
1059 | 56 |
!CompressionStream class methodsFor:'initialization'! |
57 |
||
58 |
initialize |
|
59 |
BlockSize := 6. |
|
60 |
! ! |
|
61 |
||
62 |
!CompressionStream class methodsFor:'instance creation'! |
|
63 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
64 |
readOpenOn:aStream |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
65 |
"open to read data from an compressed stream" |
1193 | 66 |
|
2195 | 67 |
^ self |
68 |
readOpenOn:aStream |
|
69 |
suppressHeaderAndChecksum:(self defaultSuppressHeaderAndChecksum) |
|
70 |
! |
|
71 |
||
72 |
readOpenOn:aStream suppressHeaderAndChecksum:aBoolean |
|
73 |
"open to read data from an compressed stream" |
|
74 |
||
75 |
^ self basicNew |
|
76 |
openWithMode:#readonly |
|
77 |
on:aStream |
|
78 |
suppressHeaderAndChecksum:aBoolean |
|
1059 | 79 |
! |
80 |
||
81 |
writeOpenOn:aStream |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
82 |
"open to write data compressed to stream" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
83 |
|
2195 | 84 |
^ self |
85 |
writeOpenOn:aStream |
|
86 |
suppressHeaderAndChecksum:(self defaultSuppressHeaderAndChecksum) |
|
87 |
! |
|
88 |
||
89 |
writeOpenOn:aStream suppressHeaderAndChecksum:aBoolean |
|
90 |
"open to write data compressed to stream" |
|
91 |
||
92 |
^ self basicNew |
|
93 |
openWithMode:#writeonly |
|
94 |
on:aStream |
|
95 |
suppressHeaderAndChecksum:aBoolean |
|
96 |
! ! |
|
97 |
||
98 |
!CompressionStream class methodsFor:'defaults'! |
|
99 |
||
100 |
defaultSuppressHeaderAndChecksum |
|
101 |
^ true |
|
1059 | 102 |
! ! |
103 |
||
4311 | 104 |
!CompressionStream class methodsFor:'queries'! |
105 |
||
106 |
isAbstract |
|
107 |
"Return if this class is an abstract class. |
|
108 |
True is returned here for myself only; false for subclasses. |
|
109 |
Abstract subclasses must redefine this again." |
|
110 |
||
111 |
^ self == CompressionStream. |
|
112 |
! ! |
|
113 |
||
1059 | 114 |
!CompressionStream methodsFor:'accessing'! |
115 |
||
116 |
binary |
|
117 |
"switch to binary mode - default is text |
|
118 |
" |
|
119 |
binary := true. |
|
120 |
! |
|
121 |
||
4205 | 122 |
propagateClose |
123 |
^ propagateClose |
|
124 |
! |
|
125 |
||
126 |
propagateClose:aBoolean |
|
127 |
"close the underlying stream if the zip stream is closed" |
|
128 |
||
129 |
propagateClose := aBoolean. |
|
130 |
! |
|
131 |
||
1059 | 132 |
text |
1193 | 133 |
"switch to text mode - default is text" |
134 |
||
1059 | 135 |
binary := false. |
136 |
! ! |
|
137 |
||
138 |
!CompressionStream methodsFor:'error handling'! |
|
139 |
||
140 |
errorNotOpen |
|
1193 | 141 |
"report an error, that the stream has not been opened" |
142 |
||
1059 | 143 |
self zerror:'not open'. |
144 |
! |
|
145 |
||
146 |
errorReadOnly |
|
1193 | 147 |
"report an error, that the stream is a readOnly stream" |
148 |
||
1059 | 149 |
self zerror:'is readonly' |
150 |
! |
|
151 |
||
152 |
errorWriteOnly |
|
1193 | 153 |
"report an error, that the stream is a writeOnly stream" |
154 |
||
1059 | 155 |
self zerror:'is writeonly' |
156 |
! |
|
157 |
||
158 |
invalidArgument |
|
1193 | 159 |
"called if a method is invoked with invalid parameters" |
160 |
||
1059 | 161 |
self zerror:'invalid arguments'. |
162 |
! |
|
163 |
||
164 |
zerror:anError |
|
165 |
|error| |
|
166 |
||
167 |
zstream isNil ifTrue:[ |
|
4262 | 168 |
error := 'not open'. |
1059 | 169 |
] ifFalse:[ |
4262 | 170 |
anError isNumber ifTrue:[ |
171 |
anError == 1 ifTrue:[ error := 'stream at end' ] |
|
172 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ] |
|
173 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ] |
|
174 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data corrupted' ] |
|
175 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ] |
|
176 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ] |
|
177 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ] |
|
178 |
ifFalse:[ |
|
179 |
error := 'compressing error: ', anError printString |
|
180 |
]]]]]]]. |
|
181 |
] ifFalse:[ |
|
182 |
error := anError printString |
|
183 |
]. |
|
1059 | 184 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
185 |
StreamError raiseErrorString:(self class name , ': ', error). |
1059 | 186 |
! ! |
187 |
||
188 |
!CompressionStream methodsFor:'finalization'! |
|
189 |
||
190 |
executor |
|
2190 | 191 |
"redefined to return a lightweight copy |
1193 | 192 |
- all we need is the memory handle" |
193 |
||
2190 | 194 |
^ self class basicNew finalizeCopy:zstream. |
1059 | 195 |
! |
196 |
||
197 |
finalize |
|
1061 | 198 |
"the compressin-stream was garbage collected; |
1193 | 199 |
close the underlying zip-stream" |
200 |
||
1061 | 201 |
self closeZStream. |
1059 | 202 |
! |
203 |
||
204 |
finalizeCopy:aZStream |
|
1193 | 205 |
"used for finalization to close the underlying zip-stream" |
206 |
||
1059 | 207 |
zstream := aZStream. |
208 |
! ! |
|
209 |
||
210 |
!CompressionStream methodsFor:'low level'! |
|
211 |
||
2190 | 212 |
z_nextAvailableInto:aCollection startingAt:offset maxCount:maxCount |
1079 | 213 |
"read the next available bytes into a collection, a string or byteArray; |
1193 | 214 |
returns the size read" |
215 |
||
1079 | 216 |
|start count avail| |
217 |
||
218 |
avail := readLimit - position. |
|
219 |
avail > 0 ifFalse:[^ 0]. |
|
220 |
||
221 |
count := aCollection size - offset + 1. |
|
2190 | 222 |
count < 0 ifTrue:[ |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
223 |
self zerror:'invalid arguments' |
2190 | 224 |
]. |
225 |
count == 0 ifTrue:[ |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
226 |
^ 0 |
1079 | 227 |
]. |
2190 | 228 |
|
229 |
count := avail min:count. |
|
230 |
maxCount notNil ifTrue:[ count := count min:maxCount ]. |
|
231 |
||
232 |
start := position. |
|
1079 | 233 |
position := position + count. |
234 |
||
2194 | 235 |
%{ |
2192 | 236 |
unsigned char * _dstPt; |
237 |
int _count = __intVal( count ); |
|
238 |
int _offset = __intVal( offset ); |
|
239 |
unsigned char * _srcPt; |
|
240 |
OBJ _srcObj = __INST( outputBytes ); |
|
1079 | 241 |
|
242 |
if( __isBytes(aCollection) ) { |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
243 |
_dstPt = (unsigned char *) (__byteArrayVal(aCollection)); |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
244 |
} else if (__isStringLike(aCollection)) { |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
245 |
_dstPt = (unsigned char *) (__stringVal( aCollection)); |
2192 | 246 |
} else { |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
247 |
goto error; |
2192 | 248 |
} |
1079 | 249 |
|
2192 | 250 |
_dstPt = _dstPt + _offset - 1; |
1079 | 251 |
|
2192 | 252 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj ); |
253 |
_srcPt += __intVal( start ); |
|
1079 | 254 |
|
2192 | 255 |
memcpy(_dstPt, _srcPt, _count); |
1079 | 256 |
|
2192 | 257 |
RETURN(__MKSMALLINT(_count)); |
1079 | 258 |
|
2192 | 259 |
error: ; |
1079 | 260 |
%}. |
261 |
||
262 |
^ self zerror:'invalid argument' |
|
263 |
! |
|
264 |
||
1193 | 265 |
zclose |
266 |
"low level close of the zip stream" |
|
267 |
||
268 |
^ self subclassResponsibility |
|
269 |
! |
|
270 |
||
271 |
zdeflate |
|
272 |
"low level - deflate |
|
273 |
returns false if the deflate operation is finished otherwise true" |
|
274 |
||
275 |
^ self subclassResponsibility |
|
276 |
! |
|
277 |
||
278 |
zdeflateInit |
|
279 |
"low level - deflateInit |
|
280 |
initialize the deflate mode, write header" |
|
281 |
||
282 |
^ self subclassResponsibility |
|
283 |
! |
|
284 |
||
285 |
zget_avail_out |
|
286 |
"low level - get the number of available out bytes" |
|
287 |
||
288 |
^ self subclassResponsibility |
|
289 |
! |
|
290 |
||
291 |
zinflate |
|
292 |
"low level - inflate |
|
293 |
returns nil if at uncompress is finished, or the number of |
|
294 |
available bytes in the output-buffer" |
|
295 |
||
296 |
^ self subclassResponsibility |
|
297 |
! |
|
298 |
||
299 |
zinflateInit |
|
300 |
"low level - inflateInit |
|
301 |
initialize the inflate mode, read and check header" |
|
302 |
||
303 |
^ self subclassResponsibility |
|
304 |
! |
|
305 |
||
1059 | 306 |
zopen |
307 |
"low level - opens the zip stream |
|
1193 | 308 |
create the resources" |
309 |
||
1059 | 310 |
^ self subclassResponsibility |
311 |
! |
|
312 |
||
313 |
zset_avail_in:count |
|
1193 | 314 |
"set the 'avail_in' and compute the crc" |
315 |
||
1059 | 316 |
^ self subclassResponsibility |
317 |
! ! |
|
318 |
||
319 |
!CompressionStream methodsFor:'private'! |
|
320 |
||
1061 | 321 |
closeZStream |
1193 | 322 |
"close the zip-stream" |
323 |
||
1061 | 324 |
onStream := mode := nil. |
325 |
hitEOF := true. |
|
326 |
||
2467 | 327 |
zstream notNil ifTrue:[ |
328 |
self unregisterForFinalization. |
|
329 |
self zclose. |
|
330 |
zstream := nil. |
|
1061 | 331 |
]. |
2467 | 332 |
|
333 |
"Modified: / 08-08-2010 / 14:40:41 / cg" |
|
1061 | 334 |
! |
335 |
||
1081 | 336 |
contentsSpecies |
1193 | 337 |
"return the kind of object to be returned by sub-collection builders" |
338 |
||
3284 | 339 |
binary ifTrue:[^ ByteArray]. |
1193 | 340 |
^ String |
1081 | 341 |
! |
342 |
||
3284 | 343 |
fillBuffer |
344 |
"Fill the inputBytes buffer. |
|
345 |
returns true if data is available for reading; |
|
346 |
false if the stream is at end. |
|
347 |
Updates the readLimit and position." |
|
348 |
||
349 |
hitEOF ifTrue:[ |
|
350 |
^ false. |
|
351 |
]. |
|
352 |
||
353 |
position >= readLimit ifTrue:[ |
|
354 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
|
355 |
|n| |
|
356 |
||
3293 | 357 |
[ |
358 |
n := onStream nextAvailableBytes:(inputBytes size) into:inputBytes startingAt:1. |
|
359 |
n == 0 ifTrue:[ |
|
360 |
onStream atEnd ifTrue:[ |
|
361 |
hitEOF := true. |
|
362 |
^ false. |
|
363 |
] ifFalse:[ |
|
364 |
"must be a socket or pipe" |
|
365 |
onStream readWait. |
|
366 |
]. |
|
367 |
]. |
|
368 |
n == 0 |
|
369 |
] whileTrue. |
|
3284 | 370 |
self zset_avail_in:n. |
371 |
]. |
|
372 |
readLimit isNil ifTrue:[ |
|
373 |
hitEOF := true. |
|
374 |
^ false |
|
375 |
]. |
|
376 |
position := 0. |
|
377 |
]. |
|
378 |
^ true |
|
379 |
! |
|
380 |
||
1059 | 381 |
onStreamPutBytes:count from:data |
1193 | 382 |
"write compressed data to the (output) stream" |
383 |
||
1059 | 384 |
onStream nextPutBytes:count from:data startingAt:1 |
385 |
! ! |
|
386 |
||
387 |
!CompressionStream methodsFor:'queries'! |
|
388 |
||
389 |
atEnd |
|
1193 | 390 |
"return true if the end of the compressed input stream has been reached" |
391 |
||
3284 | 392 |
^ self fillBuffer not |
1062 | 393 |
! |
394 |
||
1059 | 395 |
isBinary |
396 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 397 |
The default when created is false" |
398 |
||
1059 | 399 |
^ binary |
400 |
! |
|
401 |
||
2195 | 402 |
isHeaderAndChecksumSuppressed |
403 |
"answer true if the checksum and header are suppressed; |
|
404 |
the default is true (backward compatibility)" |
|
405 |
||
406 |
suppressHeaderAndChecksum isNil ifTrue:[ |
|
407 |
^ self class defaultSuppressHeaderAndChecksum |
|
408 |
]. |
|
409 |
^ suppressHeaderAndChecksum |
|
410 |
! |
|
411 |
||
1059 | 412 |
isOpen |
1193 | 413 |
"return true, if this stream is open" |
414 |
||
1059 | 415 |
^ onStream notNil |
416 |
! |
|
417 |
||
1193 | 418 |
isReadable |
419 |
"return true, if this stream can be read from" |
|
420 |
||
1059 | 421 |
^ mode == #readonly |
422 |
! |
|
423 |
||
1193 | 424 |
isWritable |
425 |
"return true, if this stream can be written to" |
|
426 |
||
1059 | 427 |
^ mode == #writeonly |
428 |
! ! |
|
429 |
||
430 |
!CompressionStream methodsFor:'reading'! |
|
431 |
||
432 |
contents |
|
1193 | 433 |
"return the entire contents of and close the stream" |
434 |
||
1081 | 435 |
|species stream bfsize buffer count| |
1059 | 436 |
|
3284 | 437 |
mode ~~ #readonly ifTrue:[ |
438 |
self errorWriteOnly |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
439 |
]. |
3284 | 440 |
species := self contentsSpecies. |
441 |
self fillBuffer ifFalse:[ |
|
442 |
"at end" |
|
443 |
^ species new. |
|
444 |
]. |
|
1059 | 445 |
|
3284 | 446 |
bfsize := outputBytes size. |
1081 | 447 |
buffer := species new:bfsize. |
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
448 |
stream := WriteStream on:(species new:bfsize). |
1079 | 449 |
|
3284 | 450 |
[ |
451 |
count := self z_nextAvailableInto:buffer startingAt:1 maxCount:nil. |
|
1079 | 452 |
|
3284 | 453 |
count == bfsize ifTrue:[ |
454 |
stream nextPutAll:buffer. |
|
455 |
] ifFalse:[ |
|
456 |
count > 0 ifTrue:[ |
|
457 |
stream nextPutAll:buffer startingAt:1 to:count. |
|
458 |
] |
|
459 |
]. |
|
460 |
self fillBuffer |
|
461 |
] whileTrue. |
|
1062 | 462 |
self close. |
1193 | 463 |
^ stream contents |
1059 | 464 |
! |
465 |
||
466 |
next |
|
4265 | 467 |
"return the next element, |
468 |
as character (text mode) or byte (binary mode). |
|
469 |
Possibly raises ReadPastEnd error, if there are no more elements" |
|
1193 | 470 |
|
1059 | 471 |
|byte| |
472 |
||
3099 | 473 |
(byte := self nextByte) isNil ifTrue:[ |
474 |
"there is no more element; the stream is at end" |
|
475 |
^ nil |
|
1059 | 476 |
]. |
477 |
binary ifTrue:[^ byte ]. |
|
1193 | 478 |
^ Character value:byte |
1082 | 479 |
! |
480 |
||
481 |
next:n |
|
482 |
"return the next count elements of the stream as a collection. |
|
1193 | 483 |
Redefined to return a String or ByteArray and for optimization" |
484 |
||
3284 | 485 |
|data count species| |
1082 | 486 |
|
3284 | 487 |
mode ~~ #readonly ifTrue:[ |
488 |
self errorWriteOnly |
|
489 |
]. |
|
1082 | 490 |
species := self contentsSpecies. |
491 |
||
3284 | 492 |
hitEOF ifTrue:[ |
493 |
"This normally raises an error. If you proceed, you get what has been collected (nothing)." |
|
494 |
self pastEndRead. |
|
495 |
^ species new |
|
1082 | 496 |
]. |
497 |
data := species new:n. |
|
3284 | 498 |
count := self next:n into:data startingAt:1. |
499 |
count = n ifTrue:[ |
|
500 |
^ data. |
|
1082 | 501 |
]. |
3284 | 502 |
"This normally raises an error. If you proceed, you get what has been collected." |
503 |
self pastEndRead. |
|
504 |
^ data copyFrom:1 to:count. |
|
1082 | 505 |
! |
506 |
||
2191 | 507 |
next:n into:aBuffer startingAt:startIndex |
508 |
"read the next n elements of the stream into aBuffer. |
|
509 |
Return the number of bytes read." |
|
510 |
||
2192 | 511 |
|count remaining offset| |
2191 | 512 |
|
3284 | 513 |
mode ~~ #readonly ifTrue:[ |
514 |
self errorWriteOnly |
|
515 |
]. |
|
516 |
self fillBuffer ifFalse:[ |
|
517 |
self pastEndRead isNil ifTrue:[^ 0]. |
|
2191 | 518 |
]. |
519 |
offset := startIndex. |
|
520 |
remaining := n. |
|
521 |
||
3284 | 522 |
[ |
523 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
|
524 |
offset := count + offset. |
|
525 |
remaining := remaining - count. |
|
3292 | 526 |
remaining ~~ 0 and:[self fillBuffer] |
3284 | 527 |
] whileTrue. |
2191 | 528 |
^ n - remaining |
529 |
! |
|
530 |
||
3099 | 531 |
nextByte |
532 |
"return the next element, a byte |
|
3284 | 533 |
raise an error, if there are no more elements" |
534 |
||
535 |
mode ~~ #readonly ifTrue:[ |
|
536 |
self errorWriteOnly |
|
537 |
]. |
|
4265 | 538 |
position >= readLimit ifTrue:[ |
539 |
self fillBuffer ifFalse:[ |
|
540 |
"there is no more element; the stream is at end" |
|
541 |
^ self pastEndRead. |
|
542 |
]. |
|
543 |
]. |
|
3284 | 544 |
position := position + 1. |
545 |
^ outputBytes at:position. |
|
546 |
! |
|
547 |
||
548 |
nextByteOrNil |
|
549 |
"return the next element, a byte |
|
550 |
return nil, if there are no more elements" |
|
551 |
||
552 |
mode ~~ #readonly ifTrue:[ |
|
553 |
self errorWriteOnly |
|
554 |
]. |
|
4265 | 555 |
position >= readLimit ifTrue:[ |
556 |
self fillBuffer ifFalse:[ |
|
557 |
"there is no more element; the stream is at end" |
|
558 |
^ nil. |
|
559 |
]. |
|
3284 | 560 |
]. |
561 |
position := position + 1. |
|
562 |
^ outputBytes at:position. |
|
563 |
! |
|
564 |
||
3316 | 565 |
nextBytes:numBytes into:aByteCollection startingAt:initialIndex |
566 |
"can do it faster here than in super class" |
|
567 |
||
568 |
^ self next:numBytes into:aByteCollection startingAt:initialIndex. |
|
569 |
! |
|
570 |
||
3284 | 571 |
nextOrNil |
4265 | 572 |
"return the next element, |
573 |
as character (text mode) or byte (binary mode). |
|
3099 | 574 |
return nil, if there are no more elements" |
575 |
||
576 |
|byte| |
|
577 |
||
3284 | 578 |
(byte := self nextByteOrNil) isNil ifTrue:[ |
3099 | 579 |
"there is no more element; the stream is at end" |
580 |
^ nil |
|
581 |
]. |
|
3284 | 582 |
binary ifTrue:[^ byte]. |
583 |
^ Character value:byte |
|
3099 | 584 |
! |
585 |
||
2190 | 586 |
skip:count |
1082 | 587 |
"skip count objects, return the receiver |
1193 | 588 |
redefined for optimization" |
589 |
||
1082 | 590 |
|n avail| |
591 |
||
3284 | 592 |
mode ~~ #readonly ifTrue:[ |
593 |
self errorWriteOnly |
|
1083 | 594 |
]. |
595 |
||
3284 | 596 |
n := count. |
597 |
n <= 0 ifTrue:[ |
|
598 |
n ~~ 0 ifTrue:[ |
|
599 |
"don't know how to unread ..." |
|
600 |
PositionError raiseRequest |
|
601 |
]. |
|
602 |
^ self |
|
603 |
]. |
|
1082 | 604 |
|
3284 | 605 |
[self fillBuffer] whileTrue:[ |
606 |
avail := readLimit - position. |
|
607 |
avail >= n ifTrue:[ |
|
608 |
position := position + n. |
|
609 |
^ self |
|
610 |
]. |
|
611 |
position := readLimit := 0. "/ reset input |
|
612 |
n := n - avail. |
|
1082 | 613 |
]. |
1059 | 614 |
! ! |
615 |
||
616 |
!CompressionStream methodsFor:'startup & release'! |
|
617 |
||
618 |
close |
|
1193 | 619 |
"close the zip-stream" |
620 |
||
1059 | 621 |
hitEOF := true. |
1193 | 622 |
zstream notNil ifTrue:[ |
4205 | 623 |
self flush. |
624 |
self closeZStream. |
|
625 |
]. |
|
626 |
||
627 |
(propagateClose and:[onStream notNil]) ifTrue:[ |
|
628 |
onStream close. |
|
1059 | 629 |
]. |
630 |
! |
|
631 |
||
632 |
openWithMode:aMode on:aStream |
|
1061 | 633 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
634 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
635 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
636 |
" |
2195 | 637 |
|
638 |
^ self |
|
639 |
openWithMode:aMode |
|
640 |
on:aStream |
|
641 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
642 |
! |
|
643 |
||
644 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
645 |
"open the zip-stream on a stream |
|
646 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
647 |
in the ZipStream |
|
648 |
" |
|
649 |
||
650 |
^ self |
|
651 |
streamOpenWithMode:aMode |
|
652 |
on:aStream |
|
653 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
654 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
655 |
|
4258 | 656 |
setInputStream:aStream |
657 |
"change the input stream (i.e. continue reading from aStream). |
|
658 |
Useful if the input arrives in chunks, and we have to continue decompressing |
|
659 |
from the next chunk (passing a readStream on the next chunk here)." |
|
660 |
||
661 |
mode ~~ #readonly ifTrue:[ |
|
662 |
self errorWriteOnly |
|
663 |
]. |
|
664 |
onStream := aStream. |
|
665 |
! |
|
666 |
||
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
667 |
streamOpenWithMode:aMode on:aStream |
2195 | 668 |
|
669 |
^ self |
|
670 |
streamOpenWithMode:aMode |
|
671 |
on:aStream |
|
672 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
673 |
! |
|
674 |
||
675 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
676 |
"open the compression stream on a stream |
2195 | 677 |
#readonly uncompress the data derived from the read-stream, aStream |
678 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 679 |
" |
1059 | 680 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
681 |
aStream isNil ifTrue:[ |
2195 | 682 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
683 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
684 |
|
4205 | 685 |
propagateClose := false. |
2190 | 686 |
onStream := aStream. |
1059 | 687 |
mode := aMode. |
1829 | 688 |
outputBytes := ExternalBytes unprotectedNew:16384. |
689 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 690 |
readLimit := position := 0. |
691 |
binary := false. |
|
2195 | 692 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 693 |
|
694 |
self zopen. |
|
695 |
self registerForFinalization. |
|
696 |
||
697 |
hitEOF := false. |
|
698 |
||
699 |
aMode == #readonly ifTrue:[ |
|
2195 | 700 |
self zinflateInit. |
1059 | 701 |
] ifFalse:[ |
2195 | 702 |
self zdeflateInit |
1059 | 703 |
]. |
704 |
! ! |
|
705 |
||
706 |
!CompressionStream methodsFor:'writing'! |
|
707 |
||
1062 | 708 |
contents:contents |
1193 | 709 |
"write the entire contents to and close the stream" |
710 |
||
3284 | 711 |
self nextPutAll:contents. |
1062 | 712 |
self close. |
713 |
! |
|
714 |
||
1059 | 715 |
flush |
1193 | 716 |
"flush the input and output buffer" |
717 |
||
1829 | 718 |
|continue availOut| |
1059 | 719 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
720 |
self isWritable ifFalse:[^ self]. |
1059 | 721 |
self zset_avail_in:position. |
722 |
||
723 |
position := 0. |
|
724 |
||
1829 | 725 |
[ continue := self zdeflate. |
2190 | 726 |
availOut := self zget_avail_out. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
727 |
|
2190 | 728 |
availOut > 0 ifTrue:[ |
729 |
self onStreamPutBytes:availOut from:outputBytes |
|
730 |
]. |
|
1829 | 731 |
|
1827 | 732 |
] doWhile:[ continue == true ]. |
1059 | 733 |
! |
734 |
||
1193 | 735 |
nextPut:aByteOrCharacter |
736 |
"write the argument, aByteOrCharacter" |
|
1059 | 737 |
|
1193 | 738 |
position == inputBytes size ifTrue:[self flush]. |
739 |
position := position + 1. |
|
740 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 741 |
! |
742 |
||
743 |
nextPutAll:aCollection |
|
744 |
|limit| |
|
745 |
||
746 |
limit := inputBytes size. |
|
747 |
||
748 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 749 |
position == limit ifTrue:[self flush]. |
750 |
position := position + 1. |
|
751 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 752 |
]. |
1193 | 753 |
! ! |
1059 | 754 |
|
1193 | 755 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 756 |
|
1193 | 757 |
version |
4205 | 758 |
^ '$Header$' |
2293 | 759 |
! |
760 |
||
761 |
version_CVS |
|
4205 | 762 |
^ '$Header$' |
1059 | 763 |
! ! |
764 |
||
3099 | 765 |
|
1059 | 766 |
CompressionStream initialize! |