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