author | Stefan Vogel <sv@exept.de> |
Fri, 20 Mar 2020 13:01:09 +0100 | |
changeset 5469 | d78065ee4cff |
parent 5434 | b8bab8d9763b |
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 |
|
5434 | 288 |
returns false if the deflate operation is finished, otherwise true" |
1193 | 289 |
|
290 |
^ self subclassResponsibility |
|
5434 | 291 |
|
292 |
"Modified (comment): / 27-01-2020 / 15:41:08 / Stefan Vogel" |
|
1193 | 293 |
! |
294 |
||
295 |
zdeflateInit |
|
296 |
"low level - deflateInit |
|
297 |
initialize the deflate mode, write header" |
|
298 |
||
299 |
^ self subclassResponsibility |
|
300 |
! |
|
301 |
||
302 |
zget_avail_out |
|
303 |
"low level - get the number of available out bytes" |
|
304 |
||
305 |
^ self subclassResponsibility |
|
306 |
! |
|
307 |
||
308 |
zinflate |
|
309 |
"low level - inflate |
|
310 |
returns nil if at uncompress is finished, or the number of |
|
311 |
available bytes in the output-buffer" |
|
312 |
||
313 |
^ self subclassResponsibility |
|
314 |
! |
|
315 |
||
316 |
zinflateInit |
|
317 |
"low level - inflateInit |
|
318 |
initialize the inflate mode, read and check header" |
|
319 |
||
320 |
^ self subclassResponsibility |
|
321 |
! |
|
322 |
||
1059 | 323 |
zopen |
324 |
"low level - opens the zip stream |
|
1193 | 325 |
create the resources" |
326 |
||
1059 | 327 |
^ self subclassResponsibility |
328 |
! |
|
329 |
||
330 |
zset_avail_in:count |
|
1193 | 331 |
"set the 'avail_in' and compute the crc" |
332 |
||
1059 | 333 |
^ self subclassResponsibility |
334 |
! ! |
|
335 |
||
336 |
!CompressionStream methodsFor:'private'! |
|
337 |
||
1061 | 338 |
closeZStream |
1193 | 339 |
"close the zip-stream" |
340 |
||
1061 | 341 |
onStream := mode := nil. |
342 |
hitEOF := true. |
|
343 |
||
2467 | 344 |
zstream notNil ifTrue:[ |
345 |
self unregisterForFinalization. |
|
346 |
self zclose. |
|
347 |
zstream := nil. |
|
1061 | 348 |
]. |
2467 | 349 |
|
350 |
"Modified: / 08-08-2010 / 14:40:41 / cg" |
|
1061 | 351 |
! |
352 |
||
1081 | 353 |
contentsSpecies |
1193 | 354 |
"return the kind of object to be returned by sub-collection builders" |
355 |
||
3284 | 356 |
binary ifTrue:[^ ByteArray]. |
1193 | 357 |
^ String |
1081 | 358 |
! |
359 |
||
3284 | 360 |
fillBuffer |
361 |
"Fill the inputBytes buffer. |
|
362 |
returns true if data is available for reading; |
|
363 |
false if the stream is at end. |
|
364 |
Updates the readLimit and position." |
|
365 |
||
366 |
hitEOF ifTrue:[ |
|
367 |
^ false. |
|
368 |
]. |
|
369 |
||
370 |
position >= readLimit ifTrue:[ |
|
371 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
|
372 |
|n| |
|
373 |
||
3293 | 374 |
[ |
375 |
n := onStream nextAvailableBytes:(inputBytes size) into:inputBytes startingAt:1. |
|
376 |
n == 0 ifTrue:[ |
|
377 |
onStream atEnd ifTrue:[ |
|
378 |
hitEOF := true. |
|
379 |
^ false. |
|
380 |
] ifFalse:[ |
|
381 |
"must be a socket or pipe" |
|
382 |
onStream readWait. |
|
383 |
]. |
|
384 |
]. |
|
385 |
n == 0 |
|
386 |
] whileTrue. |
|
3284 | 387 |
self zset_avail_in:n. |
388 |
]. |
|
389 |
readLimit isNil ifTrue:[ |
|
390 |
hitEOF := true. |
|
391 |
^ false |
|
392 |
]. |
|
393 |
position := 0. |
|
394 |
]. |
|
395 |
^ true |
|
396 |
! |
|
397 |
||
1059 | 398 |
onStreamPutBytes:count from:data |
1193 | 399 |
"write compressed data to the (output) stream" |
400 |
||
1059 | 401 |
onStream nextPutBytes:count from:data startingAt:1 |
402 |
! ! |
|
403 |
||
404 |
!CompressionStream methodsFor:'queries'! |
|
405 |
||
406 |
atEnd |
|
1193 | 407 |
"return true if the end of the compressed input stream has been reached" |
408 |
||
3284 | 409 |
^ self fillBuffer not |
1062 | 410 |
! |
411 |
||
1059 | 412 |
isBinary |
413 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 414 |
The default when created is false" |
415 |
||
1059 | 416 |
^ binary |
417 |
! |
|
418 |
||
2195 | 419 |
isHeaderAndChecksumSuppressed |
420 |
"answer true if the checksum and header are suppressed; |
|
421 |
the default is true (backward compatibility)" |
|
422 |
||
423 |
suppressHeaderAndChecksum isNil ifTrue:[ |
|
424 |
^ self class defaultSuppressHeaderAndChecksum |
|
425 |
]. |
|
426 |
^ suppressHeaderAndChecksum |
|
427 |
! |
|
428 |
||
1059 | 429 |
isOpen |
1193 | 430 |
"return true, if this stream is open" |
431 |
||
1059 | 432 |
^ onStream notNil |
433 |
! |
|
434 |
||
1193 | 435 |
isReadable |
436 |
"return true, if this stream can be read from" |
|
437 |
||
1059 | 438 |
^ mode == #readonly |
439 |
! |
|
440 |
||
1193 | 441 |
isWritable |
442 |
"return true, if this stream can be written to" |
|
443 |
||
1059 | 444 |
^ mode == #writeonly |
445 |
! ! |
|
446 |
||
447 |
!CompressionStream methodsFor:'reading'! |
|
448 |
||
449 |
contents |
|
1193 | 450 |
"return the entire contents of and close the stream" |
451 |
||
1081 | 452 |
|species stream bfsize buffer count| |
1059 | 453 |
|
3284 | 454 |
mode ~~ #readonly ifTrue:[ |
455 |
self errorWriteOnly |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
456 |
]. |
3284 | 457 |
species := self contentsSpecies. |
458 |
self fillBuffer ifFalse:[ |
|
459 |
"at end" |
|
460 |
^ species new. |
|
461 |
]. |
|
1059 | 462 |
|
3284 | 463 |
bfsize := outputBytes size. |
1081 | 464 |
buffer := species new:bfsize. |
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
465 |
stream := WriteStream on:(species new:bfsize). |
1079 | 466 |
|
3284 | 467 |
[ |
468 |
count := self z_nextAvailableInto:buffer startingAt:1 maxCount:nil. |
|
1079 | 469 |
|
3284 | 470 |
count == bfsize ifTrue:[ |
471 |
stream nextPutAll:buffer. |
|
472 |
] ifFalse:[ |
|
473 |
count > 0 ifTrue:[ |
|
474 |
stream nextPutAll:buffer startingAt:1 to:count. |
|
475 |
] |
|
476 |
]. |
|
477 |
self fillBuffer |
|
478 |
] whileTrue. |
|
1062 | 479 |
self close. |
1193 | 480 |
^ stream contents |
1059 | 481 |
! |
482 |
||
483 |
next |
|
4265 | 484 |
"return the next element, |
485 |
as character (text mode) or byte (binary mode). |
|
486 |
Possibly raises ReadPastEnd error, if there are no more elements" |
|
1193 | 487 |
|
1059 | 488 |
|byte| |
489 |
||
3099 | 490 |
(byte := self nextByte) isNil ifTrue:[ |
491 |
"there is no more element; the stream is at end" |
|
492 |
^ nil |
|
1059 | 493 |
]. |
494 |
binary ifTrue:[^ byte ]. |
|
1193 | 495 |
^ Character value:byte |
1082 | 496 |
! |
497 |
||
498 |
next:n |
|
499 |
"return the next count elements of the stream as a collection. |
|
1193 | 500 |
Redefined to return a String or ByteArray and for optimization" |
501 |
||
3284 | 502 |
|data count species| |
1082 | 503 |
|
3284 | 504 |
mode ~~ #readonly ifTrue:[ |
505 |
self errorWriteOnly |
|
506 |
]. |
|
1082 | 507 |
species := self contentsSpecies. |
508 |
||
3284 | 509 |
hitEOF ifTrue:[ |
510 |
"This normally raises an error. If you proceed, you get what has been collected (nothing)." |
|
511 |
self pastEndRead. |
|
512 |
^ species new |
|
1082 | 513 |
]. |
514 |
data := species new:n. |
|
3284 | 515 |
count := self next:n into:data startingAt:1. |
516 |
count = n ifTrue:[ |
|
517 |
^ data. |
|
1082 | 518 |
]. |
3284 | 519 |
"This normally raises an error. If you proceed, you get what has been collected." |
520 |
self pastEndRead. |
|
521 |
^ data copyFrom:1 to:count. |
|
1082 | 522 |
! |
523 |
||
2191 | 524 |
next:n into:aBuffer startingAt:startIndex |
525 |
"read the next n elements of the stream into aBuffer. |
|
526 |
Return the number of bytes read." |
|
527 |
||
4644 | 528 |
|count remaining offset fillOK| |
2191 | 529 |
|
3284 | 530 |
mode ~~ #readonly ifTrue:[ |
531 |
self errorWriteOnly |
|
532 |
]. |
|
533 |
self fillBuffer ifFalse:[ |
|
534 |
self pastEndRead isNil ifTrue:[^ 0]. |
|
2191 | 535 |
]. |
536 |
offset := startIndex. |
|
537 |
remaining := n. |
|
538 |
||
3284 | 539 |
[ |
540 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
|
541 |
offset := count + offset. |
|
542 |
remaining := remaining - count. |
|
4644 | 543 |
remaining ~~ 0 and:[ fillOK := self fillBuffer] |
3284 | 544 |
] whileTrue. |
4644 | 545 |
remaining ~~ 0 ifTrue:[ |
546 |
self pastEndRead |
|
547 |
]. |
|
2191 | 548 |
^ n - remaining |
549 |
! |
|
550 |
||
3099 | 551 |
nextByte |
552 |
"return the next element, a byte |
|
3284 | 553 |
raise an error, if there are no more elements" |
554 |
||
555 |
mode ~~ #readonly ifTrue:[ |
|
556 |
self errorWriteOnly |
|
557 |
]. |
|
4265 | 558 |
position >= readLimit ifTrue:[ |
559 |
self fillBuffer ifFalse:[ |
|
560 |
"there is no more element; the stream is at end" |
|
561 |
^ self pastEndRead. |
|
562 |
]. |
|
563 |
]. |
|
3284 | 564 |
position := position + 1. |
565 |
^ outputBytes at:position. |
|
566 |
! |
|
567 |
||
568 |
nextByteOrNil |
|
569 |
"return the next element, a byte |
|
570 |
return nil, if there are no more elements" |
|
571 |
||
572 |
mode ~~ #readonly ifTrue:[ |
|
573 |
self errorWriteOnly |
|
574 |
]. |
|
4265 | 575 |
position >= readLimit ifTrue:[ |
576 |
self fillBuffer ifFalse:[ |
|
577 |
"there is no more element; the stream is at end" |
|
578 |
^ nil. |
|
579 |
]. |
|
3284 | 580 |
]. |
581 |
position := position + 1. |
|
582 |
^ outputBytes at:position. |
|
583 |
! |
|
584 |
||
3316 | 585 |
nextBytes:numBytes into:aByteCollection startingAt:initialIndex |
586 |
"can do it faster here than in super class" |
|
587 |
||
588 |
^ self next:numBytes into:aByteCollection startingAt:initialIndex. |
|
589 |
! |
|
590 |
||
3284 | 591 |
nextOrNil |
4265 | 592 |
"return the next element, |
593 |
as character (text mode) or byte (binary mode). |
|
3099 | 594 |
return nil, if there are no more elements" |
595 |
||
596 |
|byte| |
|
597 |
||
3284 | 598 |
(byte := self nextByteOrNil) isNil ifTrue:[ |
3099 | 599 |
"there is no more element; the stream is at end" |
600 |
^ nil |
|
601 |
]. |
|
3284 | 602 |
binary ifTrue:[^ byte]. |
603 |
^ Character value:byte |
|
3099 | 604 |
! |
605 |
||
2190 | 606 |
skip:count |
1082 | 607 |
"skip count objects, return the receiver |
1193 | 608 |
redefined for optimization" |
609 |
||
1082 | 610 |
|n avail| |
611 |
||
3284 | 612 |
mode ~~ #readonly ifTrue:[ |
613 |
self errorWriteOnly |
|
1083 | 614 |
]. |
615 |
||
3284 | 616 |
n := count. |
617 |
n <= 0 ifTrue:[ |
|
618 |
n ~~ 0 ifTrue:[ |
|
619 |
"don't know how to unread ..." |
|
620 |
PositionError raiseRequest |
|
621 |
]. |
|
622 |
^ self |
|
623 |
]. |
|
1082 | 624 |
|
3284 | 625 |
[self fillBuffer] whileTrue:[ |
626 |
avail := readLimit - position. |
|
627 |
avail >= n ifTrue:[ |
|
628 |
position := position + n. |
|
629 |
^ self |
|
630 |
]. |
|
631 |
position := readLimit := 0. "/ reset input |
|
632 |
n := n - avail. |
|
1082 | 633 |
]. |
1059 | 634 |
! ! |
635 |
||
636 |
!CompressionStream methodsFor:'startup & release'! |
|
637 |
||
638 |
close |
|
1193 | 639 |
"close the zip-stream" |
640 |
||
1059 | 641 |
hitEOF := true. |
1193 | 642 |
zstream notNil ifTrue:[ |
4205 | 643 |
self flush. |
644 |
self closeZStream. |
|
645 |
]. |
|
646 |
||
647 |
(propagateClose and:[onStream notNil]) ifTrue:[ |
|
648 |
onStream close. |
|
1059 | 649 |
]. |
650 |
! |
|
651 |
||
652 |
openWithMode:aMode on:aStream |
|
1061 | 653 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
654 |
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
|
655 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
656 |
" |
2195 | 657 |
|
658 |
^ self |
|
659 |
openWithMode:aMode |
|
660 |
on:aStream |
|
661 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
662 |
! |
|
663 |
||
664 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
665 |
"open the zip-stream on a stream |
|
666 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
667 |
in the ZipStream |
|
668 |
" |
|
669 |
||
670 |
^ self |
|
671 |
streamOpenWithMode:aMode |
|
672 |
on:aStream |
|
673 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
674 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
675 |
|
4258 | 676 |
setInputStream:aStream |
677 |
"change the input stream (i.e. continue reading from aStream). |
|
678 |
Useful if the input arrives in chunks, and we have to continue decompressing |
|
679 |
from the next chunk (passing a readStream on the next chunk here)." |
|
680 |
||
681 |
mode ~~ #readonly ifTrue:[ |
|
682 |
self errorWriteOnly |
|
683 |
]. |
|
684 |
onStream := aStream. |
|
685 |
! |
|
686 |
||
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
687 |
streamOpenWithMode:aMode on:aStream |
2195 | 688 |
|
689 |
^ self |
|
690 |
streamOpenWithMode:aMode |
|
691 |
on:aStream |
|
692 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
693 |
! |
|
694 |
||
695 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
696 |
"open the compression stream on a stream |
2195 | 697 |
#readonly uncompress the data derived from the read-stream, aStream |
698 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 699 |
" |
1059 | 700 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
701 |
aStream isNil ifTrue:[ |
2195 | 702 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
703 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
704 |
|
4205 | 705 |
propagateClose := false. |
2190 | 706 |
onStream := aStream. |
1059 | 707 |
mode := aMode. |
1829 | 708 |
outputBytes := ExternalBytes unprotectedNew:16384. |
709 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 710 |
readLimit := position := 0. |
711 |
binary := false. |
|
2195 | 712 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 713 |
|
714 |
self zopen. |
|
715 |
self registerForFinalization. |
|
716 |
||
717 |
hitEOF := false. |
|
718 |
||
719 |
aMode == #readonly ifTrue:[ |
|
2195 | 720 |
self zinflateInit. |
1059 | 721 |
] ifFalse:[ |
2195 | 722 |
self zdeflateInit |
1059 | 723 |
]. |
724 |
! ! |
|
725 |
||
726 |
!CompressionStream methodsFor:'writing'! |
|
727 |
||
1062 | 728 |
contents:contents |
1193 | 729 |
"write the entire contents to and close the stream" |
730 |
||
3284 | 731 |
self nextPutAll:contents. |
1062 | 732 |
self close. |
733 |
! |
|
734 |
||
1059 | 735 |
flush |
1193 | 736 |
"flush the input and output buffer" |
737 |
||
1829 | 738 |
|continue availOut| |
1059 | 739 |
|
5434 | 740 |
self isWritable ifFalse:[ |
741 |
^ self |
|
742 |
]. |
|
1059 | 743 |
self zset_avail_in:position. |
744 |
||
745 |
position := 0. |
|
5434 | 746 |
[ |
747 |
continue := self zdeflate. |
|
748 |
availOut := self zget_avail_out. |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
749 |
|
5434 | 750 |
availOut > 0 ifTrue:[ |
751 |
self onStreamPutBytes:availOut from:outputBytes |
|
752 |
]. |
|
753 |
continue |
|
754 |
] whileTrue. |
|
1829 | 755 |
|
5434 | 756 |
"Modified: / 27-01-2020 / 15:42:29 / Stefan Vogel" |
1059 | 757 |
! |
758 |
||
1193 | 759 |
nextPut:aByteOrCharacter |
5159 | 760 |
"write the argument, aByteOrCharacter. |
761 |
Answer aByteOrCharacter" |
|
1059 | 762 |
|
1193 | 763 |
position == inputBytes size ifTrue:[self flush]. |
764 |
position := position + 1. |
|
765 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
5159 | 766 |
^ aByteOrCharacter |
1829 | 767 |
! |
768 |
||
769 |
nextPutAll:aCollection |
|
5156 | 770 |
"Write each of the objects in aCollection to the receiver stream. |
771 |
Answer the receiver." |
|
772 |
||
1829 | 773 |
|limit| |
774 |
||
775 |
limit := inputBytes size. |
|
776 |
||
777 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 778 |
position == limit ifTrue:[self flush]. |
779 |
position := position + 1. |
|
780 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 781 |
]. |
1193 | 782 |
! ! |
1059 | 783 |
|
1193 | 784 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 785 |
|
1193 | 786 |
version |
4205 | 787 |
^ '$Header$' |
2293 | 788 |
! |
789 |
||
790 |
version_CVS |
|
4205 | 791 |
^ '$Header$' |
1059 | 792 |
! ! |
793 |
||
3099 | 794 |
|
1059 | 795 |
CompressionStream initialize! |