1059
|
1 |
"{ Package: 'ca:Compress' }"
|
|
2 |
|
|
3 |
Stream subclass:#CompressionStream
|
|
4 |
instanceVariableNames:'onStream hitEOF binary position readLimit mode inputBytes
|
|
5 |
outputBytes zstream'
|
|
6 |
classVariableNames:'BlockSize'
|
|
7 |
poolDictionaries:''
|
|
8 |
category:'A-Compress'
|
|
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 |
].
|
|
169 |
self zclose.
|
|
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
|
|
183 |
self zclose.
|
|
184 |
zstream := nil.
|
|
185 |
!
|
|
186 |
|
|
187 |
finalizeCopy:aZStream
|
|
188 |
zstream := aZStream.
|
|
189 |
! !
|
|
190 |
|
|
191 |
!CompressionStream methodsFor:'low level'!
|
|
192 |
|
|
193 |
zclose
|
|
194 |
"low level close of the zip stream
|
|
195 |
"
|
|
196 |
^ self subclassResponsibility
|
|
197 |
!
|
|
198 |
|
|
199 |
zdeflate
|
|
200 |
"low level - deflate
|
|
201 |
"
|
|
202 |
^ self subclassResponsibility
|
|
203 |
!
|
|
204 |
|
|
205 |
zdeflateInit
|
|
206 |
"low level - deflateInit
|
|
207 |
"
|
|
208 |
^ self subclassResponsibility
|
|
209 |
!
|
|
210 |
|
|
211 |
zget_avail_out
|
|
212 |
"low level - get the number of available out bytes
|
|
213 |
"
|
|
214 |
^ self subclassResponsibility
|
|
215 |
!
|
|
216 |
|
|
217 |
zinflate
|
|
218 |
"low level - inflate
|
|
219 |
"
|
|
220 |
^ self subclassResponsibility
|
|
221 |
!
|
|
222 |
|
|
223 |
zinflateInit
|
|
224 |
"low level - inflateInit
|
|
225 |
"
|
|
226 |
^ self subclassResponsibility
|
|
227 |
!
|
|
228 |
|
|
229 |
zopen
|
|
230 |
"low level - opens the zip stream
|
|
231 |
"
|
|
232 |
^ self subclassResponsibility
|
|
233 |
!
|
|
234 |
|
|
235 |
zset_avail_in:count
|
|
236 |
"set the 'avail_in' and compute the crc
|
|
237 |
"
|
|
238 |
^ self subclassResponsibility
|
|
239 |
! !
|
|
240 |
|
|
241 |
!CompressionStream methodsFor:'private'!
|
|
242 |
|
|
243 |
onStreamPutBytes:count from:data
|
|
244 |
"write compressed data to the (output) stream
|
|
245 |
"
|
|
246 |
onStream ifNil:[ self errorNotOpen ].
|
|
247 |
onStream nextPutBytes:count from:data startingAt:1
|
|
248 |
! !
|
|
249 |
|
|
250 |
!CompressionStream methodsFor:'queries'!
|
|
251 |
|
|
252 |
atEnd
|
|
253 |
"return true if the end of the compressed input stream has been reached
|
|
254 |
"
|
|
255 |
^ hitEOF ~~ false
|
|
256 |
!
|
|
257 |
|
|
258 |
isBinary
|
|
259 |
"return true, if the stream is in binary (as opposed to text-) mode.
|
|
260 |
The default when created is false.
|
|
261 |
"
|
|
262 |
^ binary
|
|
263 |
!
|
|
264 |
|
|
265 |
isOpen
|
|
266 |
"return true, if this stream is open
|
|
267 |
"
|
|
268 |
^ onStream notNil
|
|
269 |
!
|
|
270 |
|
|
271 |
isReadOpen
|
|
272 |
"return true, if this stream can be read from
|
|
273 |
"
|
|
274 |
^ mode == #readonly
|
|
275 |
!
|
|
276 |
|
|
277 |
isWriteOpen
|
|
278 |
"return true, if this stream can be written to
|
|
279 |
"
|
|
280 |
^ mode == #writeonly
|
|
281 |
! !
|
|
282 |
|
|
283 |
!CompressionStream methodsFor:'reading'!
|
|
284 |
|
|
285 |
contents
|
|
286 |
"return the entire contents of the stream
|
|
287 |
"
|
|
288 |
|out b|
|
|
289 |
|
|
290 |
mode == #readonly ifFalse:[ self errorReadOnly ].
|
|
291 |
|
|
292 |
out := (String new:1024) writeStream.
|
|
293 |
|
|
294 |
[ (b := self next) notNil ] whileTrue:[
|
|
295 |
out nextPut:b
|
|
296 |
].
|
|
297 |
^ out contents
|
|
298 |
!
|
|
299 |
|
|
300 |
next
|
|
301 |
"return the next element, a character or byte (textmode)
|
|
302 |
if there is more element, nil is returned
|
|
303 |
"
|
|
304 |
|byte|
|
|
305 |
|
|
306 |
mode == #readonly ifFalse:[ self errorReadOnly ].
|
|
307 |
|
|
308 |
hitEOF == true ifTrue:[^ nil].
|
|
309 |
|
|
310 |
position == readLimit ifTrue:[
|
|
311 |
[ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
|
|
312 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
|
|
313 |
|
|
314 |
(n notNil and:[n > 0]) ifFalse:[
|
|
315 |
self pastEnd
|
|
316 |
].
|
|
317 |
self zset_avail_in:n.
|
|
318 |
].
|
|
319 |
readLimit ifNil:[
|
|
320 |
hitEOF := true.
|
|
321 |
^ nil
|
|
322 |
].
|
|
323 |
position := 0.
|
|
324 |
].
|
|
325 |
position := position + 1.
|
|
326 |
byte := outputBytes at:position.
|
|
327 |
|
|
328 |
binary ifTrue:[^ byte ].
|
|
329 |
^ Character value:byte
|
|
330 |
! !
|
|
331 |
|
|
332 |
!CompressionStream methodsFor:'startup & release'!
|
|
333 |
|
|
334 |
close
|
|
335 |
"close the zip-stream
|
|
336 |
"
|
|
337 |
hitEOF := true.
|
|
338 |
|
|
339 |
zstream ifNotNil:[
|
|
340 |
self flush.
|
|
341 |
self zclose.
|
|
342 |
].
|
|
343 |
!
|
|
344 |
|
|
345 |
openWithMode:aMode on:aStream
|
|
346 |
|
|
347 |
(onStream := aStream) ifNil:[ self errorNotOpen ].
|
|
348 |
|
|
349 |
mode := aMode.
|
|
350 |
outputBytes := ExternalBytes unprotectedNew:8192.
|
|
351 |
inputBytes := ExternalBytes unprotectedNew:8192.
|
|
352 |
readLimit := position := 0.
|
|
353 |
binary := false.
|
|
354 |
|
|
355 |
self zopen.
|
|
356 |
self registerForFinalization.
|
|
357 |
|
|
358 |
hitEOF := false.
|
|
359 |
|
|
360 |
aMode == #readonly ifTrue:[
|
|
361 |
self zinflateInit.
|
|
362 |
] ifFalse:[
|
|
363 |
self zdeflateInit
|
|
364 |
].
|
|
365 |
! !
|
|
366 |
|
|
367 |
!CompressionStream methodsFor:'writing'!
|
|
368 |
|
|
369 |
flush
|
|
370 |
"flush the input and output buffer
|
|
371 |
"
|
|
372 |
|continue|
|
|
373 |
|
|
374 |
self isWriteOpen ifFalse:[ ^ self ].
|
|
375 |
self zset_avail_in:position.
|
|
376 |
|
|
377 |
position := 0.
|
|
378 |
continue := true.
|
|
379 |
|
|
380 |
[continue] whileTrue:[ |count|
|
|
381 |
count := self zget_avail_out.
|
|
382 |
|
|
383 |
count > 0 ifTrue:[
|
|
384 |
self onStreamPutBytes:count from:outputBytes
|
|
385 |
].
|
|
386 |
continue := self zdeflate.
|
|
387 |
].
|
|
388 |
!
|
|
389 |
|
|
390 |
nextPut:something
|
|
391 |
"write the argument, something
|
|
392 |
"
|
|
393 |
|byte|
|
|
394 |
|
|
395 |
byte := nil.
|
|
396 |
%{
|
|
397 |
int _bval;
|
|
398 |
|
|
399 |
if( __INST(mode) == @symbol(writeonly) )
|
|
400 |
{
|
|
401 |
if( __INST(binary) != true )
|
|
402 |
{
|
|
403 |
if( ! __isCharacter(something) )
|
|
404 |
goto bad;
|
|
405 |
|
|
406 |
_bval = __intVal(__characterVal(something)) & 0xFF;
|
|
407 |
}
|
|
408 |
else if( __isSmallInteger(something) )
|
|
409 |
{
|
|
410 |
_bval = __intVal(something);
|
|
411 |
|
|
412 |
if( (_bval < 0) || (_bval > 255) )
|
|
413 |
goto bad;
|
|
414 |
}
|
|
415 |
else
|
|
416 |
goto bad;
|
|
417 |
|
|
418 |
byte = __MKSMALLINT( _bval );
|
|
419 |
}
|
|
420 |
bad: ;
|
|
421 |
%}.
|
|
422 |
byte ifNil:[
|
|
423 |
mode == #readonly ifTrue:[self errorReadOnly].
|
|
424 |
zstream ifNil:[ self errorNotOpen ].
|
|
425 |
self invalidArguments.
|
|
426 |
].
|
|
427 |
position == inputBytes size ifTrue:[ self flush ].
|
|
428 |
position := position + 1.
|
|
429 |
inputBytes at:position put:byte.
|
|
430 |
!
|
|
431 |
|
|
432 |
nextPutByte:aByte
|
|
433 |
"write a byte; works in both binary and text modes.
|
|
434 |
"
|
|
435 |
binary ifTrue:[
|
|
436 |
self nextPut:aByte.
|
|
437 |
] ifFalse:[
|
|
438 |
binary := true.
|
|
439 |
self nextPut:aByte.
|
|
440 |
binary := false.
|
|
441 |
].
|
|
442 |
! !
|
|
443 |
|
|
444 |
CompressionStream initialize!
|