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