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