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