1059
|
1 |
"{ Package: 'ca:Compress' }"
|
|
2 |
|
|
3 |
Stream subclass:#CompressionStream
|
1061
|
4 |
instanceVariableNames:'onStream hitEOF binary position readLimit mode inputBytes
|
|
5 |
outputBytes zstream'
|
|
6 |
classVariableNames:'BlockSize'
|
|
7 |
poolDictionaries:''
|
|
8 |
category:'A-Compress'
|
1059
|
9 |
!
|
|
10 |
|
|
11 |
!CompressionStream class methodsFor:'initialization'!
|
|
12 |
|
|
13 |
initialize
|
|
14 |
BlockSize := 6.
|
|
15 |
! !
|
|
16 |
|
|
17 |
!CompressionStream class methodsFor:'instance creation'!
|
|
18 |
|
|
19 |
readOpenOn:aStream
|
|
20 |
"read data from an compressed stream
|
|
21 |
"
|
|
22 |
^ self basicNew openWithMode:#readonly on:aStream
|
|
23 |
!
|
|
24 |
|
|
25 |
writeOpenOn:aStream
|
|
26 |
"write data compressed to stream
|
|
27 |
"
|
|
28 |
^ self basicNew openWithMode:#writeonly on:aStream
|
|
29 |
! !
|
|
30 |
|
|
31 |
!CompressionStream class methodsFor:'test'!
|
|
32 |
|
1082
|
33 |
doTestNextN
|
|
34 |
"
|
|
35 |
CompressionStream doTestNextN
|
|
36 |
"
|
|
37 |
|stream time file zipCont nxtCont|
|
|
38 |
|
|
39 |
file := '/phys/exept/tmp/yyy/201--T22--D.20000415.SAGSA.DE0220523.gz' asFilename.
|
|
40 |
file isReadable ifFalse:[^ self error:'not existant'].
|
|
41 |
|
|
42 |
time := Time millisecondsToRun:[ |zipStream|
|
|
43 |
zipStream := stream := zipCont := nil.
|
|
44 |
[
|
|
45 |
stream := file readStream.
|
|
46 |
zipStream := ZipStream readOpenOn:stream.
|
|
47 |
zipCont := zipStream contents.
|
|
48 |
] valueNowOrOnUnwindDo:[
|
|
49 |
zipStream notNil ifTrue:[zipStream close].
|
|
50 |
stream notNil ifTrue:[stream close].
|
|
51 |
].
|
|
52 |
].
|
|
53 |
Transcript showCR:('STX Time : %1 Size: %2' bindWith:time with:(zipCont size)).
|
|
54 |
|
|
55 |
time := Time millisecondsToRun:[ |zipStream wstream|
|
|
56 |
zipStream := stream := nxtCont := nil.
|
|
57 |
[
|
|
58 |
stream := file readStream.
|
|
59 |
wstream := '' writeStream.
|
|
60 |
zipStream := ZipStream readOpenOn:stream.
|
|
61 |
|
|
62 |
[zipStream atEnd] whileFalse:[
|
|
63 |
wstream nextPutAll:(zipStream next:117)
|
|
64 |
].
|
|
65 |
nxtCont := wstream contents.
|
|
66 |
] valueNowOrOnUnwindDo:[
|
|
67 |
zipStream notNil ifTrue:[zipStream close].
|
|
68 |
stream notNil ifTrue:[stream close].
|
|
69 |
].
|
|
70 |
].
|
|
71 |
Transcript showCR:('NEXT Time : %1 Size: %2' bindWith:time with:(nxtCont size)).
|
|
72 |
|
|
73 |
nxtCont = zipCont ifTrue:[ Transcript showCR:'OK' ]
|
|
74 |
ifFalse:[ self error:'contents differs' ].
|
|
75 |
!
|
|
76 |
|
|
77 |
|
|
78 |
doTestSkipN
|
|
79 |
"
|
|
80 |
CompressionStream doTestSkipN
|
|
81 |
"
|
|
82 |
|stream time file skpCont nxtCont skip|
|
|
83 |
|
|
84 |
file := '/phys/exept/tmp/yyy/201--T22--D.20000415.SAGSA.DE0220523.gz' asFilename.
|
|
85 |
file isReadable ifFalse:[^ self error:'not existant'].
|
|
86 |
|
|
87 |
skip := 6885379.
|
|
88 |
|
|
89 |
time := Time millisecondsToRun:[ |zipStream wstream|
|
|
90 |
zipStream := stream := nxtCont := nil.
|
|
91 |
[
|
|
92 |
stream := file readStream.
|
|
93 |
wstream := '' writeStream.
|
|
94 |
zipStream := ZipStream readOpenOn:stream.
|
|
95 |
skip timesRepeat:[ zipStream next ].
|
|
96 |
|
|
97 |
[zipStream atEnd] whileFalse:[
|
|
98 |
wstream nextPutAll:(zipStream next:117)
|
|
99 |
].
|
|
100 |
nxtCont := wstream contents.
|
|
101 |
] valueNowOrOnUnwindDo:[
|
|
102 |
zipStream notNil ifTrue:[zipStream close].
|
|
103 |
stream notNil ifTrue:[stream close].
|
|
104 |
].
|
|
105 |
].
|
|
106 |
|
|
107 |
Transcript showCR:('STX Time : %1 Size: %2' bindWith:time with:(nxtCont size)).
|
|
108 |
|
|
109 |
time := Time millisecondsToRun:[ |zipStream wstream|
|
|
110 |
zipStream := stream := skpCont := nil.
|
|
111 |
[
|
|
112 |
stream := file readStream.
|
|
113 |
wstream := '' writeStream.
|
|
114 |
zipStream := ZipStream readOpenOn:stream.
|
|
115 |
zipStream skip:skip.
|
|
116 |
[zipStream atEnd] whileFalse:[
|
|
117 |
wstream nextPutAll:(zipStream next:117)
|
|
118 |
].
|
|
119 |
skpCont := wstream contents.
|
|
120 |
] valueNowOrOnUnwindDo:[
|
|
121 |
zipStream notNil ifTrue:[zipStream close].
|
|
122 |
stream notNil ifTrue:[stream close].
|
|
123 |
].
|
|
124 |
].
|
|
125 |
Transcript showCR:('NEXT Time : %1 Size: %2' bindWith:time with:(skpCont size)).
|
|
126 |
|
|
127 |
nxtCont = skpCont ifTrue:[ Transcript showCR:'OK' ]
|
|
128 |
ifFalse:[ self error:'contents differs' ].
|
|
129 |
|
|
130 |
!
|
|
131 |
|
|
132 |
doTestUnixAgainstClass
|
|
133 |
"
|
|
134 |
CompressionStream doTestUnixAgainstClass
|
|
135 |
"
|
1080
|
136 |
|stream time file zipCont cmdCont|
|
|
137 |
|
|
138 |
file := '/phys/exept/tmp/yyy/201--T22--D.20000415.SAGSA.DE0220523.gz' asFilename.
|
|
139 |
file isReadable ifFalse:[^ self error:'not existant'].
|
|
140 |
|
|
141 |
time := Time millisecondsToRun:[ |zipStream|
|
|
142 |
zipStream := stream := zipCont := nil.
|
|
143 |
[
|
|
144 |
stream := file readStream.
|
|
145 |
zipStream := ZipStream readOpenOn:stream.
|
|
146 |
zipCont := zipStream contents.
|
|
147 |
] valueNowOrOnUnwindDo:[
|
|
148 |
zipStream notNil ifTrue:[zipStream close].
|
|
149 |
stream notNil ifTrue:[stream close].
|
|
150 |
].
|
|
151 |
].
|
|
152 |
Transcript showCR:('STX Time : %1 Size: %2' bindWith:time with:(zipCont size)).
|
|
153 |
|
|
154 |
time := Time millisecondsToRun:[ |command|
|
|
155 |
cmdCont := stream := nil.
|
|
156 |
[
|
|
157 |
command := 'gunzip < ' , file pathName.
|
|
158 |
stream := PipeStream readingFrom:command.
|
|
159 |
cmdCont := stream contentsOfEntireFile.
|
|
160 |
|
|
161 |
] valueNowOrOnUnwindDo:[
|
|
162 |
stream notNil ifTrue:[stream close].
|
|
163 |
]
|
|
164 |
].
|
|
165 |
Transcript showCR:('UNIX Time : %1 Size: %2' bindWith:time with:(cmdCont size)).
|
|
166 |
|
|
167 |
cmdCont = zipCont ifTrue:[ Transcript showCR:'OK' ]
|
|
168 |
ifFalse:[ self error:'contents differs' ].
|
|
169 |
!
|
|
170 |
|
1059
|
171 |
test
|
|
172 |
"
|
|
173 |
CompressionStream test
|
|
174 |
"
|
|
175 |
|original compressed contents in out zip|
|
|
176 |
|
|
177 |
original := 'smalltalk.rc' asFilename contentsOfEntireFile.
|
|
178 |
|
|
179 |
in := original readStream.
|
|
180 |
|
|
181 |
[ |b|
|
|
182 |
out := #[] writeStream.
|
|
183 |
zip := self writeOpenOn:out.
|
|
184 |
|
|
185 |
[in atEnd] whileFalse:[
|
|
186 |
(b := in next) ifNotNil:[zip nextPut:b]
|
|
187 |
]
|
|
188 |
] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
|
|
189 |
|
|
190 |
compressed := out contents.
|
|
191 |
[ |b|
|
|
192 |
zip := self readOpenOn:(compressed readStream).
|
|
193 |
out := '' writeStream.
|
|
194 |
|
|
195 |
[ (b := zip next) notNil ] whileTrue:[ out nextPut:b ]
|
|
196 |
|
|
197 |
] valueNowOrOnUnwindDo:[
|
|
198 |
zip ifNotNil:[ zip close ].
|
|
199 |
contents := out contents.
|
|
200 |
|
|
201 |
Transcript showCR:(contents size).
|
|
202 |
Transcript showCR:(compressed size).
|
|
203 |
].
|
|
204 |
|
|
205 |
original = contents ifFalse:[
|
|
206 |
self halt:'contents differs'.
|
|
207 |
^ self
|
|
208 |
].
|
|
209 |
Transcript showCR:'OK'.
|
|
210 |
!
|
|
211 |
|
|
212 |
testFile
|
|
213 |
"
|
|
214 |
CompressionStream testFile
|
|
215 |
"
|
|
216 |
|fileContents in zip out gzipCmd|
|
|
217 |
|
|
218 |
fileContents := 'smalltalk.rc' asFilename contentsOfEntireFile.
|
|
219 |
|
|
220 |
in := fileContents readStream.
|
|
221 |
out := FileStream newFileNamed:'YYY.gz'.
|
|
222 |
out ifNil:[ ^ self ].
|
|
223 |
|
|
224 |
[ zip := self writeOpenOn:out.
|
|
225 |
|
|
226 |
[in atEnd] whileFalse:[ |buf|
|
|
227 |
buf := in nextAvailable:512.
|
|
228 |
buf do:[:n|
|
|
229 |
zip nextPut:n
|
|
230 |
]
|
|
231 |
].
|
|
232 |
] valueNowOrOnUnwindDo:[
|
|
233 |
zip ifNotNil:[ zip close ].
|
|
234 |
out close.
|
|
235 |
].
|
|
236 |
gzipCmd := 'gzip -dc YYY.gz > YYY; diff YYY smalltalk.rc'.
|
|
237 |
|
|
238 |
Transcript showCR:gzipCmd.
|
|
239 |
gzipCmd printCR.
|
|
240 |
! !
|
|
241 |
|
|
242 |
!CompressionStream methodsFor:'accessing'!
|
|
243 |
|
|
244 |
binary
|
|
245 |
"switch to binary mode - default is text
|
|
246 |
"
|
|
247 |
binary := true.
|
|
248 |
!
|
|
249 |
|
|
250 |
text
|
|
251 |
"switch to text mode - default is text
|
|
252 |
"
|
|
253 |
binary := false.
|
|
254 |
! !
|
|
255 |
|
|
256 |
!CompressionStream methodsFor:'error handling'!
|
|
257 |
|
|
258 |
errorNotOpen
|
|
259 |
"report an error, that the stream has not been opened
|
|
260 |
"
|
|
261 |
self zerror:'not open'.
|
|
262 |
!
|
|
263 |
|
|
264 |
errorReadOnly
|
|
265 |
"report an error, that the stream is a readOnly stream
|
|
266 |
"
|
|
267 |
self zerror:'is readonly'
|
|
268 |
!
|
|
269 |
|
|
270 |
errorWriteOnly
|
|
271 |
"report an error, that the stream is a writeOnly stream
|
|
272 |
"
|
|
273 |
self zerror:'is writeonly'
|
|
274 |
!
|
|
275 |
|
|
276 |
invalidArgument
|
|
277 |
"called if a method is invoked with invalid parameters.
|
|
278 |
"
|
|
279 |
self zerror:'invalid arguments'.
|
|
280 |
!
|
|
281 |
|
|
282 |
pastEnd
|
|
283 |
self zerror:'end of stream'.
|
|
284 |
!
|
|
285 |
|
|
286 |
zerror:anError
|
|
287 |
|error|
|
|
288 |
|
|
289 |
zstream isNil ifTrue:[
|
|
290 |
error := 'not open'.
|
|
291 |
] ifFalse:[
|
|
292 |
anError isNumber ifTrue:[
|
|
293 |
anError == 1 ifTrue:[ error := 'stream at end' ]
|
|
294 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ]
|
|
295 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ]
|
|
296 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ]
|
|
297 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ]
|
|
298 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ]
|
|
299 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ]
|
|
300 |
ifFalse:[
|
|
301 |
error := 'compressing error: ', anError printString
|
|
302 |
]]]]]]].
|
|
303 |
] ifFalse:[
|
|
304 |
error := anError printString
|
|
305 |
].
|
|
306 |
].
|
1061
|
307 |
self closeZStream.
|
1059
|
308 |
Stream streamErrorSignal raiseErrorString:(self class name , ': ', error).
|
|
309 |
! !
|
|
310 |
|
|
311 |
!CompressionStream methodsFor:'finalization'!
|
|
312 |
|
|
313 |
executor
|
|
314 |
"redefined to return a lightweight copy
|
|
315 |
- all we need is the memory handle.
|
|
316 |
"
|
|
317 |
^ self class basicNew finalizeCopy:zstream.
|
|
318 |
!
|
|
319 |
|
|
320 |
finalize
|
1061
|
321 |
"the compressin-stream was garbage collected;
|
|
322 |
close the underlying zip-stream
|
|
323 |
"
|
|
324 |
self closeZStream.
|
1059
|
325 |
!
|
|
326 |
|
|
327 |
finalizeCopy:aZStream
|
1061
|
328 |
"used for finalization to close the underlying zip-stream
|
|
329 |
"
|
1059
|
330 |
zstream := aZStream.
|
|
331 |
! !
|
|
332 |
|
|
333 |
!CompressionStream methodsFor:'low level'!
|
|
334 |
|
|
335 |
zclose
|
|
336 |
"low level close of the zip stream
|
|
337 |
"
|
|
338 |
^ self subclassResponsibility
|
|
339 |
!
|
|
340 |
|
|
341 |
zdeflate
|
|
342 |
"low level - deflate
|
1061
|
343 |
returns false if the deflate operation is finished otherwise true.
|
1059
|
344 |
"
|
|
345 |
^ self subclassResponsibility
|
|
346 |
!
|
|
347 |
|
|
348 |
zdeflateInit
|
|
349 |
"low level - deflateInit
|
1061
|
350 |
initialize the deflate mode, write header ...
|
1059
|
351 |
"
|
|
352 |
^ self subclassResponsibility
|
|
353 |
!
|
|
354 |
|
|
355 |
zget_avail_out
|
|
356 |
"low level - get the number of available out bytes
|
|
357 |
"
|
|
358 |
^ self subclassResponsibility
|
|
359 |
!
|
|
360 |
|
|
361 |
zinflate
|
|
362 |
"low level - inflate
|
1061
|
363 |
returns nil if at uncompress is finished, or the number of
|
|
364 |
available bytes in the output-buffer.
|
1059
|
365 |
"
|
|
366 |
^ self subclassResponsibility
|
|
367 |
!
|
|
368 |
|
|
369 |
zinflateInit
|
|
370 |
"low level - inflateInit
|
1061
|
371 |
initialize the inflate mode, read and check header ...
|
1059
|
372 |
"
|
|
373 |
^ self subclassResponsibility
|
|
374 |
!
|
|
375 |
|
1079
|
376 |
z_nextAvailableInto:aCollection startingAt:offset
|
|
377 |
"read the next available bytes into a collection, a string or byteArray;
|
|
378 |
returns the size read
|
|
379 |
"
|
|
380 |
|start count avail|
|
|
381 |
|
|
382 |
avail := readLimit - position.
|
|
383 |
avail > 0 ifFalse:[^ 0].
|
|
384 |
|
|
385 |
count := aCollection size - offset + 1.
|
|
386 |
|
|
387 |
count > 0 ifFalse:[
|
|
388 |
count < 0 ifTrue:[
|
|
389 |
self zerror:'invalid arguments'
|
|
390 |
].
|
|
391 |
^ 0
|
|
392 |
].
|
|
393 |
count := avail min:count.
|
|
394 |
start := position.
|
|
395 |
position := position + count.
|
|
396 |
|
|
397 |
%{ unsigned char * _dstPt;
|
|
398 |
|
|
399 |
if( __isBytes(aCollection) ) {
|
|
400 |
_dstPt = (unsigned char *) (__ByteArrayInstPtr(aCollection)->ba_element);
|
|
401 |
} else if (__isString(aCollection)) {
|
|
402 |
_dstPt = (unsigned char *) (__stringVal( aCollection));
|
|
403 |
} else
|
|
404 |
_dstPt = (unsigned char *) 0;
|
|
405 |
|
|
406 |
if( _dstPt )
|
|
407 |
{
|
|
408 |
int _loop, _count, _offset;
|
|
409 |
unsigned char * _srcPt;
|
|
410 |
OBJ _srcObj = __INST( outputBytes );
|
|
411 |
|
|
412 |
_offset = __intVal( offset );
|
|
413 |
_dstPt = _dstPt + _offset - 1;
|
|
414 |
|
|
415 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj );
|
|
416 |
_srcPt += __intVal( start );
|
|
417 |
_count = __intVal( count );
|
|
418 |
|
|
419 |
for( _loop = 0; _loop < _count; ++_loop )
|
|
420 |
* _dstPt++ = * _srcPt++;
|
|
421 |
|
|
422 |
RETURN(__MKSMALLINT(_count));
|
|
423 |
}
|
|
424 |
%}.
|
|
425 |
|
|
426 |
^ self zerror:'invalid argument'
|
|
427 |
!
|
|
428 |
|
1059
|
429 |
zopen
|
|
430 |
"low level - opens the zip stream
|
1061
|
431 |
create the resources ...
|
1059
|
432 |
"
|
|
433 |
^ self subclassResponsibility
|
|
434 |
!
|
|
435 |
|
|
436 |
zset_avail_in:count
|
|
437 |
"set the 'avail_in' and compute the crc
|
|
438 |
"
|
|
439 |
^ self subclassResponsibility
|
|
440 |
! !
|
|
441 |
|
|
442 |
!CompressionStream methodsFor:'private'!
|
|
443 |
|
1061
|
444 |
closeZStream
|
|
445 |
"close the zip-stream
|
|
446 |
"
|
|
447 |
onStream := mode := nil.
|
|
448 |
hitEOF := true.
|
|
449 |
|
|
450 |
zstream ifNotNil:[
|
|
451 |
self unregisterForFinalization.
|
|
452 |
self zclose.
|
|
453 |
zstream := nil.
|
|
454 |
].
|
|
455 |
!
|
|
456 |
|
1081
|
457 |
contentsSpecies
|
|
458 |
"return the kind of object to be returned by sub-collection builders
|
|
459 |
"
|
|
460 |
binary == true ifTrue:[ ^ ByteArray ].
|
|
461 |
^ String
|
|
462 |
!
|
|
463 |
|
1059
|
464 |
onStreamPutBytes:count from:data
|
|
465 |
"write compressed data to the (output) stream
|
|
466 |
"
|
|
467 |
onStream ifNil:[ self errorNotOpen ].
|
|
468 |
onStream nextPutBytes:count from:data startingAt:1
|
|
469 |
! !
|
|
470 |
|
|
471 |
!CompressionStream methodsFor:'queries'!
|
|
472 |
|
|
473 |
atEnd
|
|
474 |
"return true if the end of the compressed input stream has been reached
|
|
475 |
"
|
|
476 |
^ hitEOF ~~ false
|
|
477 |
!
|
|
478 |
|
1062
|
479 |
canReadWithoutBlocking
|
|
480 |
"returns true if data are available for reading;
|
|
481 |
false if the stream is at end.
|
|
482 |
updates the readLimit and position
|
|
483 |
"
|
|
484 |
mode == #readonly ifFalse:[
|
|
485 |
self errorReadOnly
|
|
486 |
].
|
|
487 |
hitEOF == true ifTrue:[ ^ false ].
|
|
488 |
|
|
489 |
position >= readLimit ifTrue:[
|
|
490 |
[ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
|
|
491 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
|
|
492 |
|
|
493 |
(n notNil and:[n > 0]) ifFalse:[
|
|
494 |
self pastEnd
|
|
495 |
].
|
|
496 |
self zset_avail_in:n.
|
|
497 |
].
|
|
498 |
readLimit ifNil:[
|
|
499 |
hitEOF := true.
|
|
500 |
^ false
|
|
501 |
].
|
|
502 |
position := 0.
|
|
503 |
].
|
|
504 |
^ true
|
|
505 |
!
|
|
506 |
|
1059
|
507 |
isBinary
|
|
508 |
"return true, if the stream is in binary (as opposed to text-) mode.
|
|
509 |
The default when created is false.
|
|
510 |
"
|
|
511 |
^ binary
|
|
512 |
!
|
|
513 |
|
|
514 |
isOpen
|
|
515 |
"return true, if this stream is open
|
|
516 |
"
|
|
517 |
^ onStream notNil
|
|
518 |
!
|
|
519 |
|
|
520 |
isReadOpen
|
|
521 |
"return true, if this stream can be read from
|
|
522 |
"
|
|
523 |
^ mode == #readonly
|
|
524 |
!
|
|
525 |
|
|
526 |
isWriteOpen
|
|
527 |
"return true, if this stream can be written to
|
|
528 |
"
|
|
529 |
^ mode == #writeonly
|
|
530 |
! !
|
|
531 |
|
|
532 |
!CompressionStream methodsFor:'reading'!
|
|
533 |
|
|
534 |
contents
|
1062
|
535 |
"return the entire contents of the stream;
|
|
536 |
after reading the stream is closed.
|
1059
|
537 |
"
|
1081
|
538 |
|species stream bfsize buffer count|
|
1059
|
539 |
|
|
540 |
mode == #readonly ifFalse:[ self errorReadOnly ].
|
1062
|
541 |
bfsize := outputBytes size.
|
1059
|
542 |
|
1081
|
543 |
species := self contentsSpecies.
|
|
544 |
buffer := species new:bfsize.
|
|
545 |
stream := (species new:bfsize) writeStream.
|
1079
|
546 |
|
|
547 |
[ self canReadWithoutBlocking ] whileTrue:[
|
|
548 |
count := self z_nextAvailableInto:buffer startingAt:1.
|
|
549 |
|
|
550 |
count == bfsize ifTrue:[
|
|
551 |
stream nextPutAll:buffer.
|
1062
|
552 |
] ifFalse:[
|
1079
|
553 |
count > 0 ifTrue:[
|
|
554 |
stream nextPutAll:(buffer copyFrom:1 to:count)
|
|
555 |
]
|
1062
|
556 |
].
|
1059
|
557 |
].
|
1062
|
558 |
self close.
|
|
559 |
^ stream contents
|
1059
|
560 |
!
|
|
561 |
|
|
562 |
next
|
|
563 |
"return the next element, a character or byte (textmode)
|
|
564 |
if there is more element, nil is returned
|
|
565 |
"
|
|
566 |
|byte|
|
|
567 |
|
1062
|
568 |
self canReadWithoutBlocking ifFalse:[
|
|
569 |
"there is no more element; the stream is at end"
|
|
570 |
^ nil
|
1059
|
571 |
].
|
|
572 |
position := position + 1.
|
|
573 |
byte := outputBytes at:position.
|
|
574 |
|
|
575 |
binary ifTrue:[^ byte ].
|
|
576 |
^ Character value:byte
|
1082
|
577 |
!
|
|
578 |
|
|
579 |
next:n
|
|
580 |
"return the next count elements of the stream as a collection.
|
|
581 |
Redefined to return a String or ByteArray and for optimization
|
|
582 |
"
|
|
583 |
|data count offset species|
|
|
584 |
|
|
585 |
species := self contentsSpecies.
|
|
586 |
|
|
587 |
self canReadWithoutBlocking ifFalse:[
|
|
588 |
^ species new
|
|
589 |
].
|
|
590 |
data := species new:n.
|
|
591 |
offset := 1.
|
|
592 |
|
|
593 |
[ self canReadWithoutBlocking ] whileTrue:[
|
|
594 |
count := self z_nextAvailableInto:data startingAt:offset.
|
|
595 |
offset := count + offset.
|
|
596 |
offset > n ifTrue:[^ data ]
|
|
597 |
].
|
|
598 |
^ data copyFrom:1 to:(offset - 1)
|
|
599 |
!
|
|
600 |
|
|
601 |
skip:count
|
|
602 |
"skip count objects, return the receiver
|
|
603 |
redefined for optimization
|
|
604 |
"
|
|
605 |
|n avail|
|
|
606 |
|
|
607 |
n := count.
|
|
608 |
|
1083
|
609 |
n > 0 ifFalse:[
|
|
610 |
n ~~ 0 ifTrue:[
|
|
611 |
"dont know how to unread ..."
|
|
612 |
PositionErrorSignal raiseRequest
|
|
613 |
].
|
|
614 |
^ self
|
|
615 |
].
|
|
616 |
|
1082
|
617 |
[ self canReadWithoutBlocking ] whileTrue:[
|
|
618 |
avail := readLimit - position.
|
|
619 |
|
|
620 |
avail >= n ifTrue:[
|
|
621 |
position := position + n.
|
|
622 |
^ self
|
|
623 |
].
|
|
624 |
position := readLimit := 0. "/ reset input
|
|
625 |
n := n - avail.
|
|
626 |
].
|
1059
|
627 |
! !
|
|
628 |
|
|
629 |
!CompressionStream methodsFor:'startup & release'!
|
|
630 |
|
|
631 |
close
|
|
632 |
"close the zip-stream
|
|
633 |
"
|
|
634 |
hitEOF := true.
|
|
635 |
|
|
636 |
zstream ifNotNil:[
|
|
637 |
self flush.
|
1061
|
638 |
self closeZStream.
|
1059
|
639 |
].
|
|
640 |
!
|
|
641 |
|
|
642 |
openWithMode:aMode on:aStream
|
1061
|
643 |
"open the zip-stream on a stream
|
|
644 |
#readonly uncompress the data derived from the read-stream, aStream
|
|
645 |
#writeonly compress the data and write to the write-stream, aStream
|
|
646 |
"
|
1059
|
647 |
(onStream := aStream) ifNil:[ self errorNotOpen ].
|
|
648 |
|
|
649 |
mode := aMode.
|
|
650 |
outputBytes := ExternalBytes unprotectedNew:8192.
|
|
651 |
inputBytes := ExternalBytes unprotectedNew:8192.
|
|
652 |
readLimit := position := 0.
|
|
653 |
binary := false.
|
|
654 |
|
|
655 |
self zopen.
|
|
656 |
self registerForFinalization.
|
|
657 |
|
|
658 |
hitEOF := false.
|
|
659 |
|
|
660 |
aMode == #readonly ifTrue:[
|
|
661 |
self zinflateInit.
|
|
662 |
] ifFalse:[
|
|
663 |
self zdeflateInit
|
|
664 |
].
|
|
665 |
! !
|
|
666 |
|
|
667 |
!CompressionStream methodsFor:'writing'!
|
|
668 |
|
1062
|
669 |
contents:contents
|
|
670 |
"write the entire contents to the stream;
|
|
671 |
after writing the stream is closed.
|
|
672 |
"
|
|
673 |
contents do:[:c| self nextPut:c ].
|
|
674 |
self close.
|
|
675 |
!
|
|
676 |
|
1059
|
677 |
flush
|
|
678 |
"flush the input and output buffer
|
|
679 |
"
|
|
680 |
|continue|
|
|
681 |
|
|
682 |
self isWriteOpen ifFalse:[ ^ self ].
|
|
683 |
self zset_avail_in:position.
|
|
684 |
|
|
685 |
position := 0.
|
|
686 |
continue := true.
|
|
687 |
|
|
688 |
[continue] whileTrue:[ |count|
|
|
689 |
count := self zget_avail_out.
|
|
690 |
|
|
691 |
count > 0 ifTrue:[
|
|
692 |
self onStreamPutBytes:count from:outputBytes
|
|
693 |
].
|
|
694 |
continue := self zdeflate.
|
|
695 |
].
|
|
696 |
!
|
|
697 |
|
|
698 |
nextPut:something
|
|
699 |
"write the argument, something
|
|
700 |
"
|
|
701 |
|byte|
|
|
702 |
|
|
703 |
byte := nil.
|
|
704 |
%{
|
|
705 |
int _bval;
|
|
706 |
|
|
707 |
if( __INST(mode) == @symbol(writeonly) )
|
|
708 |
{
|
|
709 |
if( __INST(binary) != true )
|
|
710 |
{
|
|
711 |
if( ! __isCharacter(something) )
|
|
712 |
goto bad;
|
|
713 |
|
|
714 |
_bval = __intVal(__characterVal(something)) & 0xFF;
|
|
715 |
}
|
|
716 |
else if( __isSmallInteger(something) )
|
|
717 |
{
|
|
718 |
_bval = __intVal(something);
|
|
719 |
|
|
720 |
if( (_bval < 0) || (_bval > 255) )
|
|
721 |
goto bad;
|
|
722 |
}
|
|
723 |
else
|
|
724 |
goto bad;
|
|
725 |
|
|
726 |
byte = __MKSMALLINT( _bval );
|
|
727 |
}
|
|
728 |
bad: ;
|
|
729 |
%}.
|
|
730 |
byte ifNil:[
|
1062
|
731 |
mode ~~ #writeonly ifTrue:[
|
|
732 |
zstream ifNil:[self errorNotOpen].
|
|
733 |
self errorWriteOnly.
|
|
734 |
].
|
1059
|
735 |
self invalidArguments.
|
|
736 |
].
|
|
737 |
position == inputBytes size ifTrue:[ self flush ].
|
|
738 |
position := position + 1.
|
|
739 |
inputBytes at:position put:byte.
|
|
740 |
!
|
|
741 |
|
|
742 |
nextPutByte:aByte
|
|
743 |
"write a byte; works in both binary and text modes.
|
|
744 |
"
|
|
745 |
binary ifTrue:[
|
|
746 |
self nextPut:aByte.
|
|
747 |
] ifFalse:[
|
|
748 |
binary := true.
|
|
749 |
self nextPut:aByte.
|
|
750 |
binary := false.
|
|
751 |
].
|
|
752 |
! !
|
|
753 |
|
|
754 |
CompressionStream initialize!
|