author | Claus Gittinger <cg@exept.de> |
Sat, 22 Feb 2014 17:04:58 +0100 | |
changeset 3182 | 9511845dede1 |
parent 3099 | 6ec8d8a4dae4 |
child 3284 | 0fb781a3260d |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
2190 | 3 |
All Rights Reserved |
1308 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1195 | 12 |
"{ Package: 'stx:libbasic2' }" |
1059 | 13 |
|
14 |
Stream subclass:#CompressionStream |
|
1193 | 15 |
instanceVariableNames:'onStream hitEOF binary position readLimit mode inputBytes |
2194 | 16 |
outputBytes zstream suppressHeaderAndChecksum' |
1193 | 17 |
classVariableNames:'BlockSize' |
18 |
poolDictionaries:'' |
|
19 |
category:'System-Compress' |
|
1059 | 20 |
! |
21 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
22 |
!CompressionStream class methodsFor:'documentation'! |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
23 |
|
1308 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
2190 | 27 |
All Rights Reserved |
1308 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
38 |
documentation |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
39 |
" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
40 |
Abstract superclass of streams that compress or deconpress data |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
41 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
42 |
[author:] |
3182 | 43 |
Claus Atzkern |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
44 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
45 |
[instance variables:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
46 |
|
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
47 |
[class variables:] |
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 |
[see also:] |
3182 | 50 |
ZipStream |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
51 |
" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
52 |
! ! |
1193 | 53 |
|
1059 | 54 |
!CompressionStream class methodsFor:'initialization'! |
55 |
||
56 |
initialize |
|
57 |
BlockSize := 6. |
|
58 |
! ! |
|
59 |
||
60 |
!CompressionStream class methodsFor:'instance creation'! |
|
61 |
||
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
62 |
readOpenOn:aStream |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
63 |
"open to read data from an compressed stream" |
1193 | 64 |
|
2195 | 65 |
^ self |
66 |
readOpenOn:aStream |
|
67 |
suppressHeaderAndChecksum:(self defaultSuppressHeaderAndChecksum) |
|
68 |
! |
|
69 |
||
70 |
readOpenOn:aStream suppressHeaderAndChecksum:aBoolean |
|
71 |
"open to read data from an compressed stream" |
|
72 |
||
73 |
^ self basicNew |
|
74 |
openWithMode:#readonly |
|
75 |
on:aStream |
|
76 |
suppressHeaderAndChecksum:aBoolean |
|
1059 | 77 |
! |
78 |
||
79 |
writeOpenOn:aStream |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
80 |
"open to write data compressed to stream" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
81 |
|
2195 | 82 |
^ self |
83 |
writeOpenOn:aStream |
|
84 |
suppressHeaderAndChecksum:(self defaultSuppressHeaderAndChecksum) |
|
85 |
! |
|
86 |
||
87 |
writeOpenOn:aStream suppressHeaderAndChecksum:aBoolean |
|
88 |
"open to write data compressed to stream" |
|
89 |
||
90 |
^ self basicNew |
|
91 |
openWithMode:#writeonly |
|
92 |
on:aStream |
|
93 |
suppressHeaderAndChecksum:aBoolean |
|
94 |
! ! |
|
95 |
||
96 |
!CompressionStream class methodsFor:'defaults'! |
|
97 |
||
98 |
defaultSuppressHeaderAndChecksum |
|
99 |
^ true |
|
1059 | 100 |
! ! |
101 |
||
102 |
!CompressionStream methodsFor:'accessing'! |
|
103 |
||
104 |
binary |
|
105 |
"switch to binary mode - default is text |
|
106 |
" |
|
107 |
binary := true. |
|
108 |
! |
|
109 |
||
110 |
text |
|
1193 | 111 |
"switch to text mode - default is text" |
112 |
||
1059 | 113 |
binary := false. |
114 |
! ! |
|
115 |
||
116 |
!CompressionStream methodsFor:'error handling'! |
|
117 |
||
118 |
errorNotOpen |
|
1193 | 119 |
"report an error, that the stream has not been opened" |
120 |
||
1059 | 121 |
self zerror:'not open'. |
122 |
! |
|
123 |
||
124 |
errorReadOnly |
|
1193 | 125 |
"report an error, that the stream is a readOnly stream" |
126 |
||
1059 | 127 |
self zerror:'is readonly' |
128 |
! |
|
129 |
||
130 |
errorWriteOnly |
|
1193 | 131 |
"report an error, that the stream is a writeOnly stream" |
132 |
||
1059 | 133 |
self zerror:'is writeonly' |
134 |
! |
|
135 |
||
136 |
invalidArgument |
|
1193 | 137 |
"called if a method is invoked with invalid parameters" |
138 |
||
1059 | 139 |
self zerror:'invalid arguments'. |
140 |
! |
|
141 |
||
1481 | 142 |
pastEndRead |
1059 | 143 |
self zerror:'end of stream'. |
1782 | 144 |
^ nil |
145 |
||
146 |
"Modified: / 18-11-2006 / 15:37:08 / cg" |
|
1059 | 147 |
! |
148 |
||
149 |
zerror:anError |
|
150 |
|error| |
|
151 |
||
152 |
zstream isNil ifTrue:[ |
|
2190 | 153 |
error := 'not open'. |
1059 | 154 |
] ifFalse:[ |
2190 | 155 |
anError isNumber ifTrue:[ |
156 |
anError == 1 ifTrue:[ error := 'stream at end' ] |
|
157 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ] |
|
158 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ] |
|
159 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ] |
|
160 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ] |
|
161 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ] |
|
162 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ] |
|
163 |
ifFalse:[ |
|
164 |
error := 'compressing error: ', anError printString |
|
165 |
]]]]]]]. |
|
166 |
] ifFalse:[ |
|
167 |
error := anError printString |
|
168 |
]. |
|
1059 | 169 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
170 |
StreamError raiseErrorString:(self class name , ': ', error). |
1059 | 171 |
! ! |
172 |
||
173 |
!CompressionStream methodsFor:'finalization'! |
|
174 |
||
175 |
executor |
|
2190 | 176 |
"redefined to return a lightweight copy |
1193 | 177 |
- all we need is the memory handle" |
178 |
||
2190 | 179 |
^ self class basicNew finalizeCopy:zstream. |
1059 | 180 |
! |
181 |
||
182 |
finalize |
|
1061 | 183 |
"the compressin-stream was garbage collected; |
1193 | 184 |
close the underlying zip-stream" |
185 |
||
1061 | 186 |
self closeZStream. |
1059 | 187 |
! |
188 |
||
189 |
finalizeCopy:aZStream |
|
1193 | 190 |
"used for finalization to close the underlying zip-stream" |
191 |
||
1059 | 192 |
zstream := aZStream. |
193 |
! ! |
|
194 |
||
195 |
!CompressionStream methodsFor:'low level'! |
|
196 |
||
2191 | 197 |
z_nextAvailableInto:aCollection startingAt:offset |
198 |
^ self z_nextAvailableInto:aCollection startingAt:offset maxCount:nil |
|
199 |
! |
|
200 |
||
2190 | 201 |
z_nextAvailableInto:aCollection startingAt:offset maxCount:maxCount |
1079 | 202 |
"read the next available bytes into a collection, a string or byteArray; |
1193 | 203 |
returns the size read" |
204 |
||
1079 | 205 |
|start count avail| |
206 |
||
207 |
avail := readLimit - position. |
|
208 |
avail > 0 ifFalse:[^ 0]. |
|
209 |
||
210 |
count := aCollection size - offset + 1. |
|
2190 | 211 |
count < 0 ifTrue:[ |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
212 |
self zerror:'invalid arguments' |
2190 | 213 |
]. |
214 |
count == 0 ifTrue:[ |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
215 |
^ 0 |
1079 | 216 |
]. |
2190 | 217 |
|
218 |
count := avail min:count. |
|
219 |
maxCount notNil ifTrue:[ count := count min:maxCount ]. |
|
220 |
||
221 |
start := position. |
|
1079 | 222 |
position := position + count. |
223 |
||
2194 | 224 |
%{ |
2192 | 225 |
unsigned char * _dstPt; |
226 |
int _count = __intVal( count ); |
|
227 |
int _offset = __intVal( offset ); |
|
228 |
unsigned char * _srcPt; |
|
229 |
OBJ _srcObj = __INST( outputBytes ); |
|
1079 | 230 |
|
231 |
if( __isBytes(aCollection) ) { |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
232 |
_dstPt = (unsigned char *) (__byteArrayVal(aCollection)); |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
233 |
} else if (__isStringLike(aCollection)) { |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
234 |
_dstPt = (unsigned char *) (__stringVal( aCollection)); |
2192 | 235 |
} else { |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
236 |
goto error; |
2192 | 237 |
} |
1079 | 238 |
|
2192 | 239 |
_dstPt = _dstPt + _offset - 1; |
1079 | 240 |
|
2192 | 241 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj ); |
242 |
_srcPt += __intVal( start ); |
|
1079 | 243 |
|
2192 | 244 |
memcpy(_dstPt, _srcPt, _count); |
1079 | 245 |
|
2192 | 246 |
RETURN(__MKSMALLINT(_count)); |
1079 | 247 |
|
2192 | 248 |
error: ; |
1079 | 249 |
%}. |
250 |
||
251 |
^ self zerror:'invalid argument' |
|
252 |
! |
|
253 |
||
1193 | 254 |
zclose |
255 |
"low level close of the zip stream" |
|
256 |
||
257 |
^ self subclassResponsibility |
|
258 |
! |
|
259 |
||
260 |
zdeflate |
|
261 |
"low level - deflate |
|
262 |
returns false if the deflate operation is finished otherwise true" |
|
263 |
||
264 |
^ self subclassResponsibility |
|
265 |
! |
|
266 |
||
267 |
zdeflateInit |
|
268 |
"low level - deflateInit |
|
269 |
initialize the deflate mode, write header" |
|
270 |
||
271 |
^ self subclassResponsibility |
|
272 |
! |
|
273 |
||
274 |
zget_avail_out |
|
275 |
"low level - get the number of available out bytes" |
|
276 |
||
277 |
^ self subclassResponsibility |
|
278 |
! |
|
279 |
||
280 |
zinflate |
|
281 |
"low level - inflate |
|
282 |
returns nil if at uncompress is finished, or the number of |
|
283 |
available bytes in the output-buffer" |
|
284 |
||
285 |
^ self subclassResponsibility |
|
286 |
! |
|
287 |
||
288 |
zinflateInit |
|
289 |
"low level - inflateInit |
|
290 |
initialize the inflate mode, read and check header" |
|
291 |
||
292 |
^ self subclassResponsibility |
|
293 |
! |
|
294 |
||
1059 | 295 |
zopen |
296 |
"low level - opens the zip stream |
|
1193 | 297 |
create the resources" |
298 |
||
1059 | 299 |
^ self subclassResponsibility |
300 |
! |
|
301 |
||
302 |
zset_avail_in:count |
|
1193 | 303 |
"set the 'avail_in' and compute the crc" |
304 |
||
1059 | 305 |
^ self subclassResponsibility |
306 |
! ! |
|
307 |
||
308 |
!CompressionStream methodsFor:'private'! |
|
309 |
||
1061 | 310 |
closeZStream |
1193 | 311 |
"close the zip-stream" |
312 |
||
1061 | 313 |
onStream := mode := nil. |
314 |
hitEOF := true. |
|
315 |
||
2467 | 316 |
zstream notNil ifTrue:[ |
317 |
self unregisterForFinalization. |
|
318 |
self zclose. |
|
319 |
zstream := nil. |
|
1061 | 320 |
]. |
2467 | 321 |
|
322 |
"Modified: / 08-08-2010 / 14:40:41 / cg" |
|
1061 | 323 |
! |
324 |
||
1081 | 325 |
contentsSpecies |
1193 | 326 |
"return the kind of object to be returned by sub-collection builders" |
327 |
||
328 |
binary == true ifTrue:[^ ByteArray]. |
|
329 |
^ String |
|
1081 | 330 |
! |
331 |
||
1059 | 332 |
onStreamPutBytes:count from:data |
1193 | 333 |
"write compressed data to the (output) stream" |
334 |
||
1059 | 335 |
onStream nextPutBytes:count from:data startingAt:1 |
336 |
! ! |
|
337 |
||
338 |
!CompressionStream methodsFor:'queries'! |
|
339 |
||
340 |
atEnd |
|
1193 | 341 |
"return true if the end of the compressed input stream has been reached" |
342 |
||
1829 | 343 |
^ self canReadWithoutBlocking not |
1059 | 344 |
! |
345 |
||
1062 | 346 |
canReadWithoutBlocking |
2192 | 347 |
"returns true if data is available for reading; |
1062 | 348 |
false if the stream is at end. |
2192 | 349 |
Updates the readLimit and position" |
1193 | 350 |
|
1829 | 351 |
|n| |
352 |
||
1062 | 353 |
mode == #readonly ifFalse:[ |
2194 | 354 |
self errorWriteOnly |
1062 | 355 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
356 |
hitEOF == true ifTrue:[^ false]. |
1062 | 357 |
|
358 |
position >= readLimit ifTrue:[ |
|
2194 | 359 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
360 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1. |
|
1829 | 361 |
|
2194 | 362 |
n == 0 ifTrue:[ |
363 |
hitEOF := true. |
|
364 |
^ false |
|
365 |
]. |
|
366 |
self zset_avail_in:n. |
|
367 |
]. |
|
368 |
readLimit isNil ifTrue:[ |
|
369 |
hitEOF := true. |
|
370 |
^ false |
|
371 |
]. |
|
372 |
position := 0. |
|
1062 | 373 |
]. |
374 |
^ true |
|
375 |
! |
|
376 |
||
1059 | 377 |
isBinary |
378 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 379 |
The default when created is false" |
380 |
||
1059 | 381 |
^ binary |
382 |
! |
|
383 |
||
2195 | 384 |
isHeaderAndChecksumSuppressed |
385 |
"answer true if the checksum and header are suppressed; |
|
386 |
the default is true (backward compatibility)" |
|
387 |
||
388 |
suppressHeaderAndChecksum isNil ifTrue:[ |
|
389 |
^ self class defaultSuppressHeaderAndChecksum |
|
390 |
]. |
|
391 |
^ suppressHeaderAndChecksum |
|
392 |
! |
|
393 |
||
1059 | 394 |
isOpen |
1193 | 395 |
"return true, if this stream is open" |
396 |
||
1059 | 397 |
^ onStream notNil |
398 |
! |
|
399 |
||
1193 | 400 |
isReadable |
401 |
"return true, if this stream can be read from" |
|
402 |
||
1059 | 403 |
^ mode == #readonly |
404 |
! |
|
405 |
||
1193 | 406 |
isWritable |
407 |
"return true, if this stream can be written to" |
|
408 |
||
1059 | 409 |
^ mode == #writeonly |
410 |
! ! |
|
411 |
||
412 |
!CompressionStream methodsFor:'reading'! |
|
413 |
||
414 |
contents |
|
1193 | 415 |
"return the entire contents of and close the stream" |
416 |
||
1081 | 417 |
|species stream bfsize buffer count| |
1059 | 418 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
419 |
mode == #readonly ifFalse:[ |
2190 | 420 |
self errorWriteOnly |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
421 |
]. |
1062 | 422 |
bfsize := outputBytes size. |
1059 | 423 |
|
1081 | 424 |
species := self contentsSpecies. |
425 |
buffer := species new:bfsize. |
|
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
426 |
stream := WriteStream on:(species new:bfsize). |
1079 | 427 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
428 |
[self canReadWithoutBlocking] whileTrue:[ |
2190 | 429 |
count := self z_nextAvailableInto:buffer startingAt:1. |
1079 | 430 |
|
2190 | 431 |
count == bfsize ifTrue:[ |
432 |
stream nextPutAll:buffer. |
|
433 |
] ifFalse:[ |
|
434 |
count > 0 ifTrue:[ |
|
435 |
stream nextPutAll:buffer startingAt:1 to:count. |
|
436 |
] |
|
437 |
]. |
|
1059 | 438 |
]. |
1062 | 439 |
self close. |
1193 | 440 |
^ stream contents |
1059 | 441 |
! |
442 |
||
443 |
next |
|
444 |
"return the next element, a character or byte (textmode) |
|
1193 | 445 |
return nil, if there are no more elements" |
446 |
||
1059 | 447 |
|byte| |
448 |
||
3099 | 449 |
(byte := self nextByte) isNil ifTrue:[ |
450 |
"there is no more element; the stream is at end" |
|
451 |
^ nil |
|
1059 | 452 |
]. |
453 |
binary ifTrue:[^ byte ]. |
|
1193 | 454 |
^ Character value:byte |
1082 | 455 |
! |
456 |
||
457 |
next:n |
|
458 |
"return the next count elements of the stream as a collection. |
|
1193 | 459 |
Redefined to return a String or ByteArray and for optimization" |
460 |
||
1082 | 461 |
|data count offset species| |
462 |
||
463 |
species := self contentsSpecies. |
|
464 |
||
465 |
self canReadWithoutBlocking ifFalse:[ |
|
1195 | 466 |
^ species new |
1082 | 467 |
]. |
468 |
data := species new:n. |
|
469 |
offset := 1. |
|
470 |
||
1193 | 471 |
[self canReadWithoutBlocking] whileTrue:[ |
1195 | 472 |
count := self z_nextAvailableInto:data startingAt:offset. |
473 |
offset := count + offset. |
|
474 |
offset > n ifTrue:[^ data] |
|
1082 | 475 |
]. |
476 |
^ data copyFrom:1 to:(offset - 1) |
|
477 |
! |
|
478 |
||
2191 | 479 |
next:n into:aBuffer startingAt:startIndex |
480 |
"read the next n elements of the stream into aBuffer. |
|
481 |
Return the number of bytes read." |
|
482 |
||
2192 | 483 |
|count remaining offset| |
2191 | 484 |
|
485 |
self canReadWithoutBlocking ifFalse:[ |
|
2194 | 486 |
^ 0 |
2191 | 487 |
]. |
488 |
offset := startIndex. |
|
489 |
remaining := n. |
|
490 |
||
491 |
[self canReadWithoutBlocking] whileTrue:[ |
|
2194 | 492 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
493 |
offset := count + offset. |
|
494 |
remaining := remaining - count. |
|
495 |
remaining == 0 ifTrue:[^ n] |
|
2191 | 496 |
]. |
497 |
^ n - remaining |
|
498 |
! |
|
499 |
||
3099 | 500 |
nextByte |
501 |
"return the next element, a byte |
|
502 |
return nil, if there are no more elements" |
|
503 |
||
504 |
|byte| |
|
505 |
||
506 |
self canReadWithoutBlocking ifFalse:[ |
|
507 |
"there is no more element; the stream is at end" |
|
508 |
^ nil |
|
509 |
]. |
|
510 |
position := position + 1. |
|
511 |
byte := outputBytes at:position. |
|
512 |
||
513 |
^ byte. |
|
514 |
! |
|
515 |
||
2190 | 516 |
skip:count |
1082 | 517 |
"skip count objects, return the receiver |
1193 | 518 |
redefined for optimization" |
519 |
||
1082 | 520 |
|n avail| |
521 |
||
522 |
n := count. |
|
523 |
||
1193 | 524 |
n <= 0 ifTrue:[ |
2190 | 525 |
n ~~ 0 ifTrue:[ |
526 |
"dont know how to unread ..." |
|
527 |
PositionError raiseRequest |
|
528 |
]. |
|
529 |
^ self |
|
1083 | 530 |
]. |
531 |
||
1193 | 532 |
[self canReadWithoutBlocking] whileTrue:[ |
2190 | 533 |
avail := readLimit - position. |
1082 | 534 |
|
2190 | 535 |
avail >= n ifTrue:[ |
536 |
position := position + n. |
|
537 |
^ self |
|
538 |
]. |
|
539 |
position := readLimit := 0. "/ reset input |
|
540 |
n := n - avail. |
|
1082 | 541 |
]. |
1059 | 542 |
! ! |
543 |
||
544 |
!CompressionStream methodsFor:'startup & release'! |
|
545 |
||
546 |
close |
|
1193 | 547 |
"close the zip-stream" |
548 |
||
1059 | 549 |
hitEOF := true. |
1193 | 550 |
zstream notNil ifTrue:[ |
1195 | 551 |
self flush. |
552 |
self closeZStream. |
|
1059 | 553 |
]. |
554 |
! |
|
555 |
||
556 |
openWithMode:aMode on:aStream |
|
1061 | 557 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
558 |
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
|
559 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
560 |
" |
2195 | 561 |
|
562 |
^ self |
|
563 |
openWithMode:aMode |
|
564 |
on:aStream |
|
565 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
566 |
! |
|
567 |
||
568 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
569 |
"open the zip-stream on a stream |
|
570 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
571 |
in the ZipStream |
|
572 |
" |
|
573 |
||
574 |
^ self |
|
575 |
streamOpenWithMode:aMode |
|
576 |
on:aStream |
|
577 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
578 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
579 |
|
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
580 |
streamOpenWithMode:aMode on:aStream |
2195 | 581 |
|
582 |
^ self |
|
583 |
streamOpenWithMode:aMode |
|
584 |
on:aStream |
|
585 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
586 |
! |
|
587 |
||
588 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
589 |
"open the compression stream on a stream |
2195 | 590 |
#readonly uncompress the data derived from the read-stream, aStream |
591 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 592 |
" |
1059 | 593 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
594 |
aStream isNil ifTrue:[ |
2195 | 595 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
596 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
597 |
|
2190 | 598 |
onStream := aStream. |
1059 | 599 |
mode := aMode. |
1829 | 600 |
outputBytes := ExternalBytes unprotectedNew:16384. |
601 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 602 |
readLimit := position := 0. |
603 |
binary := false. |
|
2195 | 604 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 605 |
|
606 |
self zopen. |
|
607 |
self registerForFinalization. |
|
608 |
||
609 |
hitEOF := false. |
|
610 |
||
611 |
aMode == #readonly ifTrue:[ |
|
2195 | 612 |
self zinflateInit. |
1059 | 613 |
] ifFalse:[ |
2195 | 614 |
self zdeflateInit |
1059 | 615 |
]. |
616 |
! ! |
|
617 |
||
618 |
!CompressionStream methodsFor:'writing'! |
|
619 |
||
1062 | 620 |
contents:contents |
1193 | 621 |
"write the entire contents to and close the stream" |
622 |
||
623 |
contents do:[:c| self nextPut:c]. |
|
1062 | 624 |
self close. |
625 |
! |
|
626 |
||
1059 | 627 |
flush |
1193 | 628 |
"flush the input and output buffer" |
629 |
||
1829 | 630 |
|continue availOut| |
1059 | 631 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
632 |
self isWritable ifFalse:[^ self]. |
1059 | 633 |
self zset_avail_in:position. |
634 |
||
635 |
position := 0. |
|
636 |
||
1829 | 637 |
[ continue := self zdeflate. |
2190 | 638 |
availOut := self zget_avail_out. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
639 |
|
2190 | 640 |
availOut > 0 ifTrue:[ |
641 |
self onStreamPutBytes:availOut from:outputBytes |
|
642 |
]. |
|
1829 | 643 |
|
1827 | 644 |
] doWhile:[ continue == true ]. |
1059 | 645 |
! |
646 |
||
1193 | 647 |
nextPut:aByteOrCharacter |
648 |
"write the argument, aByteOrCharacter" |
|
1059 | 649 |
|
1193 | 650 |
position == inputBytes size ifTrue:[self flush]. |
651 |
position := position + 1. |
|
652 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 653 |
! |
654 |
||
655 |
nextPutAll:aCollection |
|
656 |
|limit| |
|
657 |
||
658 |
limit := inputBytes size. |
|
659 |
||
660 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 661 |
position == limit ifTrue:[self flush]. |
662 |
position := position + 1. |
|
663 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 664 |
]. |
1193 | 665 |
! ! |
1059 | 666 |
|
1193 | 667 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 668 |
|
1193 | 669 |
version |
3182 | 670 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.32 2014-02-22 16:04:58 cg Exp $' |
2293 | 671 |
! |
672 |
||
673 |
version_CVS |
|
3182 | 674 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.32 2014-02-22 16:04:58 cg Exp $' |
1059 | 675 |
! ! |
676 |
||
3099 | 677 |
|
1059 | 678 |
CompressionStream initialize! |