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 |
|
|
33 |
test
|
|
34 |
"
|
|
35 |
CompressionStream test
|
|
36 |
"
|
|
37 |
|original compressed contents in out zip|
|
|
38 |
|
|
39 |
original := 'smalltalk.rc' asFilename contentsOfEntireFile.
|
|
40 |
|
|
41 |
in := original readStream.
|
|
42 |
|
|
43 |
[ |b|
|
|
44 |
out := #[] writeStream.
|
|
45 |
zip := self writeOpenOn:out.
|
|
46 |
|
|
47 |
[in atEnd] whileFalse:[
|
|
48 |
(b := in next) ifNotNil:[zip nextPut:b]
|
|
49 |
]
|
|
50 |
] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
|
|
51 |
|
|
52 |
compressed := out contents.
|
|
53 |
[ |b|
|
|
54 |
zip := self readOpenOn:(compressed readStream).
|
|
55 |
out := '' writeStream.
|
|
56 |
|
|
57 |
[ (b := zip next) notNil ] whileTrue:[ out nextPut:b ]
|
|
58 |
|
|
59 |
] valueNowOrOnUnwindDo:[
|
|
60 |
zip ifNotNil:[ zip close ].
|
|
61 |
contents := out contents.
|
|
62 |
|
|
63 |
Transcript showCR:(contents size).
|
|
64 |
Transcript showCR:(compressed size).
|
|
65 |
].
|
|
66 |
|
|
67 |
original = contents ifFalse:[
|
|
68 |
self halt:'contents differs'.
|
|
69 |
^ self
|
|
70 |
].
|
|
71 |
Transcript showCR:'OK'.
|
|
72 |
!
|
|
73 |
|
|
74 |
testFile
|
|
75 |
"
|
|
76 |
CompressionStream testFile
|
|
77 |
"
|
|
78 |
|fileContents in zip out gzipCmd|
|
|
79 |
|
|
80 |
fileContents := 'smalltalk.rc' asFilename contentsOfEntireFile.
|
|
81 |
|
|
82 |
in := fileContents readStream.
|
|
83 |
out := FileStream newFileNamed:'YYY.gz'.
|
|
84 |
out ifNil:[ ^ self ].
|
|
85 |
|
|
86 |
[ zip := self writeOpenOn:out.
|
|
87 |
|
|
88 |
[in atEnd] whileFalse:[ |buf|
|
|
89 |
buf := in nextAvailable:512.
|
|
90 |
buf do:[:n|
|
|
91 |
zip nextPut:n
|
|
92 |
]
|
|
93 |
].
|
|
94 |
] valueNowOrOnUnwindDo:[
|
|
95 |
zip ifNotNil:[ zip close ].
|
|
96 |
out close.
|
|
97 |
].
|
|
98 |
gzipCmd := 'gzip -dc YYY.gz > YYY; diff YYY smalltalk.rc'.
|
|
99 |
|
|
100 |
Transcript showCR:gzipCmd.
|
|
101 |
gzipCmd printCR.
|
|
102 |
! !
|
|
103 |
|
|
104 |
!CompressionStream methodsFor:'accessing'!
|
|
105 |
|
|
106 |
binary
|
|
107 |
"switch to binary mode - default is text
|
|
108 |
"
|
|
109 |
binary := true.
|
|
110 |
!
|
|
111 |
|
|
112 |
text
|
|
113 |
"switch to text mode - default is text
|
|
114 |
"
|
|
115 |
binary := false.
|
|
116 |
! !
|
|
117 |
|
|
118 |
!CompressionStream methodsFor:'error handling'!
|
|
119 |
|
|
120 |
errorNotOpen
|
|
121 |
"report an error, that the stream has not been opened
|
|
122 |
"
|
|
123 |
self zerror:'not open'.
|
|
124 |
!
|
|
125 |
|
|
126 |
errorReadOnly
|
|
127 |
"report an error, that the stream is a readOnly stream
|
|
128 |
"
|
|
129 |
self zerror:'is readonly'
|
|
130 |
!
|
|
131 |
|
|
132 |
errorWriteOnly
|
|
133 |
"report an error, that the stream is a writeOnly stream
|
|
134 |
"
|
|
135 |
self zerror:'is writeonly'
|
|
136 |
!
|
|
137 |
|
|
138 |
invalidArgument
|
|
139 |
"called if a method is invoked with invalid parameters.
|
|
140 |
"
|
|
141 |
self zerror:'invalid arguments'.
|
|
142 |
!
|
|
143 |
|
|
144 |
pastEnd
|
|
145 |
self zerror:'end of stream'.
|
|
146 |
!
|
|
147 |
|
|
148 |
zerror:anError
|
|
149 |
|error|
|
|
150 |
|
|
151 |
zstream isNil ifTrue:[
|
|
152 |
error := 'not open'.
|
|
153 |
] ifFalse:[
|
|
154 |
anError isNumber ifTrue:[
|
|
155 |
anError == 1 ifTrue:[ error := 'stream at end' ]
|
|
156 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ]
|
|
157 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ]
|
|
158 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ]
|
|
159 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ]
|
|
160 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ]
|
|
161 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ]
|
|
162 |
ifFalse:[
|
|
163 |
error := 'compressing error: ', anError printString
|
|
164 |
]]]]]]].
|
|
165 |
] ifFalse:[
|
|
166 |
error := anError printString
|
|
167 |
].
|
|
168 |
].
|
1061
|
169 |
self closeZStream.
|
1059
|
170 |
Stream streamErrorSignal raiseErrorString:(self class name , ': ', error).
|
|
171 |
! !
|
|
172 |
|
|
173 |
!CompressionStream methodsFor:'finalization'!
|
|
174 |
|
|
175 |
executor
|
|
176 |
"redefined to return a lightweight copy
|
|
177 |
- all we need is the memory handle.
|
|
178 |
"
|
|
179 |
^ self class basicNew finalizeCopy:zstream.
|
|
180 |
!
|
|
181 |
|
|
182 |
finalize
|
1061
|
183 |
"the compressin-stream was garbage collected;
|
|
184 |
close the underlying zip-stream
|
|
185 |
"
|
|
186 |
self closeZStream.
|
1059
|
187 |
!
|
|
188 |
|
|
189 |
finalizeCopy:aZStream
|
1061
|
190 |
"used for finalization to close the underlying zip-stream
|
|
191 |
"
|
1059
|
192 |
zstream := aZStream.
|
|
193 |
! !
|
|
194 |
|
|
195 |
!CompressionStream methodsFor:'low level'!
|
|
196 |
|
|
197 |
zclose
|
|
198 |
"low level close of the zip stream
|
|
199 |
"
|
|
200 |
^ self subclassResponsibility
|
|
201 |
!
|
|
202 |
|
|
203 |
zdeflate
|
|
204 |
"low level - deflate
|
1061
|
205 |
returns false if the deflate operation is finished otherwise true.
|
1059
|
206 |
"
|
|
207 |
^ self subclassResponsibility
|
|
208 |
!
|
|
209 |
|
|
210 |
zdeflateInit
|
|
211 |
"low level - deflateInit
|
1061
|
212 |
initialize the deflate mode, write header ...
|
1059
|
213 |
"
|
|
214 |
^ self subclassResponsibility
|
|
215 |
!
|
|
216 |
|
|
217 |
zget_avail_out
|
|
218 |
"low level - get the number of available out bytes
|
|
219 |
"
|
|
220 |
^ self subclassResponsibility
|
|
221 |
!
|
|
222 |
|
|
223 |
zinflate
|
|
224 |
"low level - inflate
|
1061
|
225 |
returns nil if at uncompress is finished, or the number of
|
|
226 |
available bytes in the output-buffer.
|
1059
|
227 |
"
|
|
228 |
^ self subclassResponsibility
|
|
229 |
!
|
|
230 |
|
|
231 |
zinflateInit
|
|
232 |
"low level - inflateInit
|
1061
|
233 |
initialize the inflate mode, read and check header ...
|
1059
|
234 |
"
|
|
235 |
^ self subclassResponsibility
|
|
236 |
!
|
|
237 |
|
|
238 |
zopen
|
|
239 |
"low level - opens the zip stream
|
1061
|
240 |
create the resources ...
|
1059
|
241 |
"
|
|
242 |
^ self subclassResponsibility
|
|
243 |
!
|
|
244 |
|
|
245 |
zset_avail_in:count
|
|
246 |
"set the 'avail_in' and compute the crc
|
|
247 |
"
|
|
248 |
^ self subclassResponsibility
|
|
249 |
! !
|
|
250 |
|
|
251 |
!CompressionStream methodsFor:'private'!
|
|
252 |
|
1061
|
253 |
closeZStream
|
|
254 |
"close the zip-stream
|
|
255 |
"
|
|
256 |
onStream := mode := nil.
|
|
257 |
hitEOF := true.
|
|
258 |
|
|
259 |
zstream ifNotNil:[
|
|
260 |
self unregisterForFinalization.
|
|
261 |
self zclose.
|
|
262 |
zstream := nil.
|
|
263 |
].
|
|
264 |
!
|
|
265 |
|
1059
|
266 |
onStreamPutBytes:count from:data
|
|
267 |
"write compressed data to the (output) stream
|
|
268 |
"
|
|
269 |
onStream ifNil:[ self errorNotOpen ].
|
|
270 |
onStream nextPutBytes:count from:data startingAt:1
|
|
271 |
! !
|
|
272 |
|
|
273 |
!CompressionStream methodsFor:'queries'!
|
|
274 |
|
|
275 |
atEnd
|
|
276 |
"return true if the end of the compressed input stream has been reached
|
|
277 |
"
|
|
278 |
^ hitEOF ~~ false
|
|
279 |
!
|
|
280 |
|
1062
|
281 |
canReadWithoutBlocking
|
|
282 |
"returns true if data are available for reading;
|
|
283 |
false if the stream is at end.
|
|
284 |
updates the readLimit and position
|
|
285 |
"
|
|
286 |
mode == #readonly ifFalse:[
|
|
287 |
self errorReadOnly
|
|
288 |
].
|
|
289 |
hitEOF == true ifTrue:[ ^ false ].
|
|
290 |
|
|
291 |
position >= readLimit ifTrue:[
|
|
292 |
[ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
|
|
293 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
|
|
294 |
|
|
295 |
(n notNil and:[n > 0]) ifFalse:[
|
|
296 |
self pastEnd
|
|
297 |
].
|
|
298 |
self zset_avail_in:n.
|
|
299 |
].
|
|
300 |
readLimit ifNil:[
|
|
301 |
hitEOF := true.
|
|
302 |
^ false
|
|
303 |
].
|
|
304 |
position := 0.
|
|
305 |
].
|
|
306 |
^ true
|
|
307 |
!
|
|
308 |
|
1059
|
309 |
isBinary
|
|
310 |
"return true, if the stream is in binary (as opposed to text-) mode.
|
|
311 |
The default when created is false.
|
|
312 |
"
|
|
313 |
^ binary
|
|
314 |
!
|
|
315 |
|
|
316 |
isOpen
|
|
317 |
"return true, if this stream is open
|
|
318 |
"
|
|
319 |
^ onStream notNil
|
|
320 |
!
|
|
321 |
|
|
322 |
isReadOpen
|
|
323 |
"return true, if this stream can be read from
|
|
324 |
"
|
|
325 |
^ mode == #readonly
|
|
326 |
!
|
|
327 |
|
|
328 |
isWriteOpen
|
|
329 |
"return true, if this stream can be written to
|
|
330 |
"
|
|
331 |
^ mode == #writeonly
|
|
332 |
! !
|
|
333 |
|
|
334 |
!CompressionStream methodsFor:'reading'!
|
|
335 |
|
|
336 |
contents
|
1062
|
337 |
"return the entire contents of the stream;
|
|
338 |
after reading the stream is closed.
|
1059
|
339 |
"
|
1062
|
340 |
|stream bfsize|
|
1059
|
341 |
|
|
342 |
mode == #readonly ifFalse:[ self errorReadOnly ].
|
|
343 |
|
1062
|
344 |
binary ifTrue:[ stream := ByteArray new:1024 ]
|
|
345 |
ifFalse:[ stream := String new:1024 ].
|
|
346 |
|
|
347 |
stream := stream writeStream.
|
|
348 |
bfsize := outputBytes size.
|
1059
|
349 |
|
1062
|
350 |
[ self canReadWithoutBlocking ] whileTrue:[ |data|
|
|
351 |
readLimit == bfsize ifTrue:[
|
|
352 |
data := outputBytes.
|
|
353 |
] ifFalse:[
|
|
354 |
data := outputBytes copyFrom:1 to:readLimit.
|
|
355 |
].
|
|
356 |
binary ifFalse:[
|
|
357 |
data := data asString
|
|
358 |
].
|
|
359 |
stream nextPutAll:data.
|
|
360 |
position := readLimit := 0. "reset input data"
|
1059
|
361 |
].
|
1062
|
362 |
self close.
|
|
363 |
^ stream contents
|
1059
|
364 |
!
|
|
365 |
|
|
366 |
next
|
|
367 |
"return the next element, a character or byte (textmode)
|
|
368 |
if there is more element, nil is returned
|
|
369 |
"
|
|
370 |
|byte|
|
|
371 |
|
1062
|
372 |
self canReadWithoutBlocking ifFalse:[
|
|
373 |
"there is no more element; the stream is at end"
|
|
374 |
^ nil
|
1059
|
375 |
].
|
|
376 |
position := position + 1.
|
|
377 |
byte := outputBytes at:position.
|
|
378 |
|
|
379 |
binary ifTrue:[^ byte ].
|
|
380 |
^ Character value:byte
|
|
381 |
! !
|
|
382 |
|
|
383 |
!CompressionStream methodsFor:'startup & release'!
|
|
384 |
|
|
385 |
close
|
|
386 |
"close the zip-stream
|
|
387 |
"
|
|
388 |
hitEOF := true.
|
|
389 |
|
|
390 |
zstream ifNotNil:[
|
|
391 |
self flush.
|
1061
|
392 |
self closeZStream.
|
1059
|
393 |
].
|
|
394 |
!
|
|
395 |
|
|
396 |
openWithMode:aMode on:aStream
|
1061
|
397 |
"open the zip-stream on a stream
|
|
398 |
#readonly uncompress the data derived from the read-stream, aStream
|
|
399 |
#writeonly compress the data and write to the write-stream, aStream
|
|
400 |
"
|
1059
|
401 |
(onStream := aStream) ifNil:[ self errorNotOpen ].
|
|
402 |
|
|
403 |
mode := aMode.
|
|
404 |
outputBytes := ExternalBytes unprotectedNew:8192.
|
|
405 |
inputBytes := ExternalBytes unprotectedNew:8192.
|
|
406 |
readLimit := position := 0.
|
|
407 |
binary := false.
|
|
408 |
|
|
409 |
self zopen.
|
|
410 |
self registerForFinalization.
|
|
411 |
|
|
412 |
hitEOF := false.
|
|
413 |
|
|
414 |
aMode == #readonly ifTrue:[
|
|
415 |
self zinflateInit.
|
|
416 |
] ifFalse:[
|
|
417 |
self zdeflateInit
|
|
418 |
].
|
|
419 |
! !
|
|
420 |
|
|
421 |
!CompressionStream methodsFor:'writing'!
|
|
422 |
|
1062
|
423 |
contents:contents
|
|
424 |
"write the entire contents to the stream;
|
|
425 |
after writing the stream is closed.
|
|
426 |
"
|
|
427 |
contents do:[:c| self nextPut:c ].
|
|
428 |
self close.
|
|
429 |
!
|
|
430 |
|
1059
|
431 |
flush
|
|
432 |
"flush the input and output buffer
|
|
433 |
"
|
|
434 |
|continue|
|
|
435 |
|
|
436 |
self isWriteOpen ifFalse:[ ^ self ].
|
|
437 |
self zset_avail_in:position.
|
|
438 |
|
|
439 |
position := 0.
|
|
440 |
continue := true.
|
|
441 |
|
|
442 |
[continue] whileTrue:[ |count|
|
|
443 |
count := self zget_avail_out.
|
|
444 |
|
|
445 |
count > 0 ifTrue:[
|
|
446 |
self onStreamPutBytes:count from:outputBytes
|
|
447 |
].
|
|
448 |
continue := self zdeflate.
|
|
449 |
].
|
|
450 |
!
|
|
451 |
|
|
452 |
nextPut:something
|
|
453 |
"write the argument, something
|
|
454 |
"
|
|
455 |
|byte|
|
|
456 |
|
|
457 |
byte := nil.
|
|
458 |
%{
|
|
459 |
int _bval;
|
|
460 |
|
|
461 |
if( __INST(mode) == @symbol(writeonly) )
|
|
462 |
{
|
|
463 |
if( __INST(binary) != true )
|
|
464 |
{
|
|
465 |
if( ! __isCharacter(something) )
|
|
466 |
goto bad;
|
|
467 |
|
|
468 |
_bval = __intVal(__characterVal(something)) & 0xFF;
|
|
469 |
}
|
|
470 |
else if( __isSmallInteger(something) )
|
|
471 |
{
|
|
472 |
_bval = __intVal(something);
|
|
473 |
|
|
474 |
if( (_bval < 0) || (_bval > 255) )
|
|
475 |
goto bad;
|
|
476 |
}
|
|
477 |
else
|
|
478 |
goto bad;
|
|
479 |
|
|
480 |
byte = __MKSMALLINT( _bval );
|
|
481 |
}
|
|
482 |
bad: ;
|
|
483 |
%}.
|
|
484 |
byte ifNil:[
|
1062
|
485 |
mode ~~ #writeonly ifTrue:[
|
|
486 |
zstream ifNil:[self errorNotOpen].
|
|
487 |
self errorWriteOnly.
|
|
488 |
].
|
1059
|
489 |
self invalidArguments.
|
|
490 |
].
|
|
491 |
position == inputBytes size ifTrue:[ self flush ].
|
|
492 |
position := position + 1.
|
|
493 |
inputBytes at:position put:byte.
|
|
494 |
!
|
|
495 |
|
|
496 |
nextPutByte:aByte
|
|
497 |
"write a byte; works in both binary and text modes.
|
|
498 |
"
|
|
499 |
binary ifTrue:[
|
|
500 |
self nextPut:aByte.
|
|
501 |
] ifFalse:[
|
|
502 |
binary := true.
|
|
503 |
self nextPut:aByte.
|
|
504 |
binary := false.
|
|
505 |
].
|
|
506 |
! !
|
|
507 |
|
|
508 |
CompressionStream initialize!
|