author | Claus Gittinger <cg@exept.de> |
Fri, 20 Jan 2017 01:09:43 +0100 | |
changeset 4265 | cbd62f582ae7 |
parent 4262 | a4efbb47dc1e |
child 4311 | 55d6e8abb73b |
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 |
|
4265 | 457 |
"return the next element, |
458 |
as character (text mode) or byte (binary mode). |
|
459 |
Possibly raises ReadPastEnd error, if there are no more elements" |
|
1193 | 460 |
|
1059 | 461 |
|byte| |
462 |
||
3099 | 463 |
(byte := self nextByte) isNil ifTrue:[ |
464 |
"there is no more element; the stream is at end" |
|
465 |
^ nil |
|
1059 | 466 |
]. |
467 |
binary ifTrue:[^ byte ]. |
|
1193 | 468 |
^ Character value:byte |
1082 | 469 |
! |
470 |
||
471 |
next:n |
|
472 |
"return the next count elements of the stream as a collection. |
|
1193 | 473 |
Redefined to return a String or ByteArray and for optimization" |
474 |
||
3284 | 475 |
|data count species| |
1082 | 476 |
|
3284 | 477 |
mode ~~ #readonly ifTrue:[ |
478 |
self errorWriteOnly |
|
479 |
]. |
|
1082 | 480 |
species := self contentsSpecies. |
481 |
||
3284 | 482 |
hitEOF ifTrue:[ |
483 |
"This normally raises an error. If you proceed, you get what has been collected (nothing)." |
|
484 |
self pastEndRead. |
|
485 |
^ species new |
|
1082 | 486 |
]. |
487 |
data := species new:n. |
|
3284 | 488 |
count := self next:n into:data startingAt:1. |
489 |
count = n ifTrue:[ |
|
490 |
^ data. |
|
1082 | 491 |
]. |
3284 | 492 |
"This normally raises an error. If you proceed, you get what has been collected." |
493 |
self pastEndRead. |
|
494 |
^ data copyFrom:1 to:count. |
|
1082 | 495 |
! |
496 |
||
2191 | 497 |
next:n into:aBuffer startingAt:startIndex |
498 |
"read the next n elements of the stream into aBuffer. |
|
499 |
Return the number of bytes read." |
|
500 |
||
2192 | 501 |
|count remaining offset| |
2191 | 502 |
|
3284 | 503 |
mode ~~ #readonly ifTrue:[ |
504 |
self errorWriteOnly |
|
505 |
]. |
|
506 |
self fillBuffer ifFalse:[ |
|
507 |
self pastEndRead isNil ifTrue:[^ 0]. |
|
2191 | 508 |
]. |
509 |
offset := startIndex. |
|
510 |
remaining := n. |
|
511 |
||
3284 | 512 |
[ |
513 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
|
514 |
offset := count + offset. |
|
515 |
remaining := remaining - count. |
|
3292 | 516 |
remaining ~~ 0 and:[self fillBuffer] |
3284 | 517 |
] whileTrue. |
2191 | 518 |
^ n - remaining |
519 |
! |
|
520 |
||
3099 | 521 |
nextByte |
522 |
"return the next element, a byte |
|
3284 | 523 |
raise an error, if there are no more elements" |
524 |
||
525 |
mode ~~ #readonly ifTrue:[ |
|
526 |
self errorWriteOnly |
|
527 |
]. |
|
4265 | 528 |
position >= readLimit ifTrue:[ |
529 |
self fillBuffer ifFalse:[ |
|
530 |
"there is no more element; the stream is at end" |
|
531 |
^ self pastEndRead. |
|
532 |
]. |
|
533 |
]. |
|
3284 | 534 |
position := position + 1. |
535 |
^ outputBytes at:position. |
|
536 |
! |
|
537 |
||
538 |
nextByteOrNil |
|
539 |
"return the next element, a byte |
|
540 |
return nil, if there are no more elements" |
|
541 |
||
542 |
mode ~~ #readonly ifTrue:[ |
|
543 |
self errorWriteOnly |
|
544 |
]. |
|
4265 | 545 |
position >= readLimit ifTrue:[ |
546 |
self fillBuffer ifFalse:[ |
|
547 |
"there is no more element; the stream is at end" |
|
548 |
^ nil. |
|
549 |
]. |
|
3284 | 550 |
]. |
551 |
position := position + 1. |
|
552 |
^ outputBytes at:position. |
|
553 |
! |
|
554 |
||
3316 | 555 |
nextBytes:numBytes into:aByteCollection startingAt:initialIndex |
556 |
"can do it faster here than in super class" |
|
557 |
||
558 |
^ self next:numBytes into:aByteCollection startingAt:initialIndex. |
|
559 |
! |
|
560 |
||
3284 | 561 |
nextOrNil |
4265 | 562 |
"return the next element, |
563 |
as character (text mode) or byte (binary mode). |
|
3099 | 564 |
return nil, if there are no more elements" |
565 |
||
566 |
|byte| |
|
567 |
||
3284 | 568 |
(byte := self nextByteOrNil) isNil ifTrue:[ |
3099 | 569 |
"there is no more element; the stream is at end" |
570 |
^ nil |
|
571 |
]. |
|
3284 | 572 |
binary ifTrue:[^ byte]. |
573 |
^ Character value:byte |
|
3099 | 574 |
! |
575 |
||
2190 | 576 |
skip:count |
1082 | 577 |
"skip count objects, return the receiver |
1193 | 578 |
redefined for optimization" |
579 |
||
1082 | 580 |
|n avail| |
581 |
||
3284 | 582 |
mode ~~ #readonly ifTrue:[ |
583 |
self errorWriteOnly |
|
1083 | 584 |
]. |
585 |
||
3284 | 586 |
n := count. |
587 |
n <= 0 ifTrue:[ |
|
588 |
n ~~ 0 ifTrue:[ |
|
589 |
"don't know how to unread ..." |
|
590 |
PositionError raiseRequest |
|
591 |
]. |
|
592 |
^ self |
|
593 |
]. |
|
1082 | 594 |
|
3284 | 595 |
[self fillBuffer] whileTrue:[ |
596 |
avail := readLimit - position. |
|
597 |
avail >= n ifTrue:[ |
|
598 |
position := position + n. |
|
599 |
^ self |
|
600 |
]. |
|
601 |
position := readLimit := 0. "/ reset input |
|
602 |
n := n - avail. |
|
1082 | 603 |
]. |
1059 | 604 |
! ! |
605 |
||
606 |
!CompressionStream methodsFor:'startup & release'! |
|
607 |
||
608 |
close |
|
1193 | 609 |
"close the zip-stream" |
610 |
||
1059 | 611 |
hitEOF := true. |
1193 | 612 |
zstream notNil ifTrue:[ |
4205 | 613 |
self flush. |
614 |
self closeZStream. |
|
615 |
]. |
|
616 |
||
617 |
(propagateClose and:[onStream notNil]) ifTrue:[ |
|
618 |
onStream close. |
|
1059 | 619 |
]. |
620 |
! |
|
621 |
||
622 |
openWithMode:aMode on:aStream |
|
1061 | 623 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
624 |
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
|
625 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
626 |
" |
2195 | 627 |
|
628 |
^ self |
|
629 |
openWithMode:aMode |
|
630 |
on:aStream |
|
631 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
632 |
! |
|
633 |
||
634 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
635 |
"open the zip-stream on a stream |
|
636 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
637 |
in the ZipStream |
|
638 |
" |
|
639 |
||
640 |
^ self |
|
641 |
streamOpenWithMode:aMode |
|
642 |
on:aStream |
|
643 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
644 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
645 |
|
4258 | 646 |
setInputStream:aStream |
647 |
"change the input stream (i.e. continue reading from aStream). |
|
648 |
Useful if the input arrives in chunks, and we have to continue decompressing |
|
649 |
from the next chunk (passing a readStream on the next chunk here)." |
|
650 |
||
651 |
mode ~~ #readonly ifTrue:[ |
|
652 |
self errorWriteOnly |
|
653 |
]. |
|
654 |
onStream := aStream. |
|
655 |
! |
|
656 |
||
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
657 |
streamOpenWithMode:aMode on:aStream |
2195 | 658 |
|
659 |
^ self |
|
660 |
streamOpenWithMode:aMode |
|
661 |
on:aStream |
|
662 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
663 |
! |
|
664 |
||
665 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
666 |
"open the compression stream on a stream |
2195 | 667 |
#readonly uncompress the data derived from the read-stream, aStream |
668 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 669 |
" |
1059 | 670 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
671 |
aStream isNil ifTrue:[ |
2195 | 672 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
673 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
674 |
|
4205 | 675 |
propagateClose := false. |
2190 | 676 |
onStream := aStream. |
1059 | 677 |
mode := aMode. |
1829 | 678 |
outputBytes := ExternalBytes unprotectedNew:16384. |
679 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 680 |
readLimit := position := 0. |
681 |
binary := false. |
|
2195 | 682 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 683 |
|
684 |
self zopen. |
|
685 |
self registerForFinalization. |
|
686 |
||
687 |
hitEOF := false. |
|
688 |
||
689 |
aMode == #readonly ifTrue:[ |
|
2195 | 690 |
self zinflateInit. |
1059 | 691 |
] ifFalse:[ |
2195 | 692 |
self zdeflateInit |
1059 | 693 |
]. |
694 |
! ! |
|
695 |
||
696 |
!CompressionStream methodsFor:'writing'! |
|
697 |
||
1062 | 698 |
contents:contents |
1193 | 699 |
"write the entire contents to and close the stream" |
700 |
||
3284 | 701 |
self nextPutAll:contents. |
1062 | 702 |
self close. |
703 |
! |
|
704 |
||
1059 | 705 |
flush |
1193 | 706 |
"flush the input and output buffer" |
707 |
||
1829 | 708 |
|continue availOut| |
1059 | 709 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
710 |
self isWritable ifFalse:[^ self]. |
1059 | 711 |
self zset_avail_in:position. |
712 |
||
713 |
position := 0. |
|
714 |
||
1829 | 715 |
[ continue := self zdeflate. |
2190 | 716 |
availOut := self zget_avail_out. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
717 |
|
2190 | 718 |
availOut > 0 ifTrue:[ |
719 |
self onStreamPutBytes:availOut from:outputBytes |
|
720 |
]. |
|
1829 | 721 |
|
1827 | 722 |
] doWhile:[ continue == true ]. |
1059 | 723 |
! |
724 |
||
1193 | 725 |
nextPut:aByteOrCharacter |
726 |
"write the argument, aByteOrCharacter" |
|
1059 | 727 |
|
1193 | 728 |
position == inputBytes size ifTrue:[self flush]. |
729 |
position := position + 1. |
|
730 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 731 |
! |
732 |
||
733 |
nextPutAll:aCollection |
|
734 |
|limit| |
|
735 |
||
736 |
limit := inputBytes size. |
|
737 |
||
738 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 739 |
position == limit ifTrue:[self flush]. |
740 |
position := position + 1. |
|
741 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 742 |
]. |
1193 | 743 |
! ! |
1059 | 744 |
|
1193 | 745 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 746 |
|
1193 | 747 |
version |
4205 | 748 |
^ '$Header$' |
2293 | 749 |
! |
750 |
||
751 |
version_CVS |
|
4205 | 752 |
^ '$Header$' |
1059 | 753 |
! ! |
754 |
||
3099 | 755 |
|
1059 | 756 |
CompressionStream initialize! |