author | Stefan Vogel <sv@exept.de> |
Tue, 03 Jun 2014 12:34:04 +0200 | |
changeset 3292 | 904ccaf98691 |
parent 3284 | 0fb781a3260d |
child 3293 | 772bcf3f973c |
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 |
||
142 |
zerror:anError |
|
143 |
|error| |
|
144 |
||
145 |
zstream isNil ifTrue:[ |
|
2190 | 146 |
error := 'not open'. |
1059 | 147 |
] ifFalse:[ |
2190 | 148 |
anError isNumber ifTrue:[ |
149 |
anError == 1 ifTrue:[ error := 'stream at end' ] |
|
150 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ] |
|
151 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ] |
|
152 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ] |
|
153 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ] |
|
154 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ] |
|
155 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ] |
|
156 |
ifFalse:[ |
|
157 |
error := 'compressing error: ', anError printString |
|
158 |
]]]]]]]. |
|
159 |
] ifFalse:[ |
|
160 |
error := anError printString |
|
161 |
]. |
|
1059 | 162 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
163 |
StreamError raiseErrorString:(self class name , ': ', error). |
1059 | 164 |
! ! |
165 |
||
166 |
!CompressionStream methodsFor:'finalization'! |
|
167 |
||
168 |
executor |
|
2190 | 169 |
"redefined to return a lightweight copy |
1193 | 170 |
- all we need is the memory handle" |
171 |
||
2190 | 172 |
^ self class basicNew finalizeCopy:zstream. |
1059 | 173 |
! |
174 |
||
175 |
finalize |
|
1061 | 176 |
"the compressin-stream was garbage collected; |
1193 | 177 |
close the underlying zip-stream" |
178 |
||
1061 | 179 |
self closeZStream. |
1059 | 180 |
! |
181 |
||
182 |
finalizeCopy:aZStream |
|
1193 | 183 |
"used for finalization to close the underlying zip-stream" |
184 |
||
1059 | 185 |
zstream := aZStream. |
186 |
! ! |
|
187 |
||
188 |
!CompressionStream methodsFor:'low level'! |
|
189 |
||
2190 | 190 |
z_nextAvailableInto:aCollection startingAt:offset maxCount:maxCount |
1079 | 191 |
"read the next available bytes into a collection, a string or byteArray; |
1193 | 192 |
returns the size read" |
193 |
||
1079 | 194 |
|start count avail| |
195 |
||
196 |
avail := readLimit - position. |
|
197 |
avail > 0 ifFalse:[^ 0]. |
|
198 |
||
199 |
count := aCollection size - offset + 1. |
|
2190 | 200 |
count < 0 ifTrue:[ |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
201 |
self zerror:'invalid arguments' |
2190 | 202 |
]. |
203 |
count == 0 ifTrue:[ |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
204 |
^ 0 |
1079 | 205 |
]. |
2190 | 206 |
|
207 |
count := avail min:count. |
|
208 |
maxCount notNil ifTrue:[ count := count min:maxCount ]. |
|
209 |
||
210 |
start := position. |
|
1079 | 211 |
position := position + count. |
212 |
||
2194 | 213 |
%{ |
2192 | 214 |
unsigned char * _dstPt; |
215 |
int _count = __intVal( count ); |
|
216 |
int _offset = __intVal( offset ); |
|
217 |
unsigned char * _srcPt; |
|
218 |
OBJ _srcObj = __INST( outputBytes ); |
|
1079 | 219 |
|
220 |
if( __isBytes(aCollection) ) { |
|
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
221 |
_dstPt = (unsigned char *) (__byteArrayVal(aCollection)); |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
222 |
} else if (__isStringLike(aCollection)) { |
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
223 |
_dstPt = (unsigned char *) (__stringVal( aCollection)); |
2192 | 224 |
} else { |
2336
072bb2f98ec9
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
2293
diff
changeset
|
225 |
goto error; |
2192 | 226 |
} |
1079 | 227 |
|
2192 | 228 |
_dstPt = _dstPt + _offset - 1; |
1079 | 229 |
|
2192 | 230 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj ); |
231 |
_srcPt += __intVal( start ); |
|
1079 | 232 |
|
2192 | 233 |
memcpy(_dstPt, _srcPt, _count); |
1079 | 234 |
|
2192 | 235 |
RETURN(__MKSMALLINT(_count)); |
1079 | 236 |
|
2192 | 237 |
error: ; |
1079 | 238 |
%}. |
239 |
||
240 |
^ self zerror:'invalid argument' |
|
241 |
! |
|
242 |
||
1193 | 243 |
zclose |
244 |
"low level close of the zip stream" |
|
245 |
||
246 |
^ self subclassResponsibility |
|
247 |
! |
|
248 |
||
249 |
zdeflate |
|
250 |
"low level - deflate |
|
251 |
returns false if the deflate operation is finished otherwise true" |
|
252 |
||
253 |
^ self subclassResponsibility |
|
254 |
! |
|
255 |
||
256 |
zdeflateInit |
|
257 |
"low level - deflateInit |
|
258 |
initialize the deflate mode, write header" |
|
259 |
||
260 |
^ self subclassResponsibility |
|
261 |
! |
|
262 |
||
263 |
zget_avail_out |
|
264 |
"low level - get the number of available out bytes" |
|
265 |
||
266 |
^ self subclassResponsibility |
|
267 |
! |
|
268 |
||
269 |
zinflate |
|
270 |
"low level - inflate |
|
271 |
returns nil if at uncompress is finished, or the number of |
|
272 |
available bytes in the output-buffer" |
|
273 |
||
274 |
^ self subclassResponsibility |
|
275 |
! |
|
276 |
||
277 |
zinflateInit |
|
278 |
"low level - inflateInit |
|
279 |
initialize the inflate mode, read and check header" |
|
280 |
||
281 |
^ self subclassResponsibility |
|
282 |
! |
|
283 |
||
1059 | 284 |
zopen |
285 |
"low level - opens the zip stream |
|
1193 | 286 |
create the resources" |
287 |
||
1059 | 288 |
^ self subclassResponsibility |
289 |
! |
|
290 |
||
291 |
zset_avail_in:count |
|
1193 | 292 |
"set the 'avail_in' and compute the crc" |
293 |
||
1059 | 294 |
^ self subclassResponsibility |
295 |
! ! |
|
296 |
||
297 |
!CompressionStream methodsFor:'private'! |
|
298 |
||
1061 | 299 |
closeZStream |
1193 | 300 |
"close the zip-stream" |
301 |
||
1061 | 302 |
onStream := mode := nil. |
303 |
hitEOF := true. |
|
304 |
||
2467 | 305 |
zstream notNil ifTrue:[ |
306 |
self unregisterForFinalization. |
|
307 |
self zclose. |
|
308 |
zstream := nil. |
|
1061 | 309 |
]. |
2467 | 310 |
|
311 |
"Modified: / 08-08-2010 / 14:40:41 / cg" |
|
1061 | 312 |
! |
313 |
||
1081 | 314 |
contentsSpecies |
1193 | 315 |
"return the kind of object to be returned by sub-collection builders" |
316 |
||
3284 | 317 |
binary ifTrue:[^ ByteArray]. |
1193 | 318 |
^ String |
1081 | 319 |
! |
320 |
||
3284 | 321 |
fillBuffer |
322 |
"Fill the inputBytes buffer. |
|
323 |
returns true if data is available for reading; |
|
324 |
false if the stream is at end. |
|
325 |
Updates the readLimit and position." |
|
326 |
||
327 |
hitEOF ifTrue:[ |
|
328 |
^ false. |
|
329 |
]. |
|
330 |
||
331 |
position >= readLimit ifTrue:[ |
|
332 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
|
333 |
|n| |
|
334 |
||
335 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1. |
|
336 |
n == 0 ifTrue:[ |
|
337 |
hitEOF := true. |
|
338 |
^ false |
|
339 |
]. |
|
340 |
self zset_avail_in:n. |
|
341 |
]. |
|
342 |
readLimit isNil ifTrue:[ |
|
343 |
hitEOF := true. |
|
344 |
^ false |
|
345 |
]. |
|
346 |
position := 0. |
|
347 |
]. |
|
348 |
^ true |
|
349 |
! |
|
350 |
||
1059 | 351 |
onStreamPutBytes:count from:data |
1193 | 352 |
"write compressed data to the (output) stream" |
353 |
||
1059 | 354 |
onStream nextPutBytes:count from:data startingAt:1 |
355 |
! ! |
|
356 |
||
357 |
!CompressionStream methodsFor:'queries'! |
|
358 |
||
359 |
atEnd |
|
1193 | 360 |
"return true if the end of the compressed input stream has been reached" |
361 |
||
3284 | 362 |
^ self fillBuffer not |
1062 | 363 |
! |
364 |
||
1059 | 365 |
isBinary |
366 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 367 |
The default when created is false" |
368 |
||
1059 | 369 |
^ binary |
370 |
! |
|
371 |
||
2195 | 372 |
isHeaderAndChecksumSuppressed |
373 |
"answer true if the checksum and header are suppressed; |
|
374 |
the default is true (backward compatibility)" |
|
375 |
||
376 |
suppressHeaderAndChecksum isNil ifTrue:[ |
|
377 |
^ self class defaultSuppressHeaderAndChecksum |
|
378 |
]. |
|
379 |
^ suppressHeaderAndChecksum |
|
380 |
! |
|
381 |
||
1059 | 382 |
isOpen |
1193 | 383 |
"return true, if this stream is open" |
384 |
||
1059 | 385 |
^ onStream notNil |
386 |
! |
|
387 |
||
1193 | 388 |
isReadable |
389 |
"return true, if this stream can be read from" |
|
390 |
||
1059 | 391 |
^ mode == #readonly |
392 |
! |
|
393 |
||
1193 | 394 |
isWritable |
395 |
"return true, if this stream can be written to" |
|
396 |
||
1059 | 397 |
^ mode == #writeonly |
398 |
! ! |
|
399 |
||
400 |
!CompressionStream methodsFor:'reading'! |
|
401 |
||
402 |
contents |
|
1193 | 403 |
"return the entire contents of and close the stream" |
404 |
||
1081 | 405 |
|species stream bfsize buffer count| |
1059 | 406 |
|
3284 | 407 |
mode ~~ #readonly ifTrue:[ |
408 |
self errorWriteOnly |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
409 |
]. |
3284 | 410 |
species := self contentsSpecies. |
411 |
self fillBuffer ifFalse:[ |
|
412 |
"at end" |
|
413 |
^ species new. |
|
414 |
]. |
|
1059 | 415 |
|
3284 | 416 |
bfsize := outputBytes size. |
1081 | 417 |
buffer := species new:bfsize. |
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
418 |
stream := WriteStream on:(species new:bfsize). |
1079 | 419 |
|
3284 | 420 |
[ |
421 |
count := self z_nextAvailableInto:buffer startingAt:1 maxCount:nil. |
|
1079 | 422 |
|
3284 | 423 |
count == bfsize ifTrue:[ |
424 |
stream nextPutAll:buffer. |
|
425 |
] ifFalse:[ |
|
426 |
count > 0 ifTrue:[ |
|
427 |
stream nextPutAll:buffer startingAt:1 to:count. |
|
428 |
] |
|
429 |
]. |
|
430 |
self fillBuffer |
|
431 |
] whileTrue. |
|
1062 | 432 |
self close. |
1193 | 433 |
^ stream contents |
1059 | 434 |
! |
435 |
||
436 |
next |
|
437 |
"return the next element, a character or byte (textmode) |
|
3284 | 438 |
raise an error, if there are no more elements" |
1193 | 439 |
|
1059 | 440 |
|byte| |
441 |
||
3099 | 442 |
(byte := self nextByte) isNil ifTrue:[ |
443 |
"there is no more element; the stream is at end" |
|
444 |
^ nil |
|
1059 | 445 |
]. |
446 |
binary ifTrue:[^ byte ]. |
|
1193 | 447 |
^ Character value:byte |
1082 | 448 |
! |
449 |
||
450 |
next:n |
|
451 |
"return the next count elements of the stream as a collection. |
|
1193 | 452 |
Redefined to return a String or ByteArray and for optimization" |
453 |
||
3284 | 454 |
|data count species| |
1082 | 455 |
|
3284 | 456 |
mode ~~ #readonly ifTrue:[ |
457 |
self errorWriteOnly |
|
458 |
]. |
|
1082 | 459 |
species := self contentsSpecies. |
460 |
||
3284 | 461 |
hitEOF ifTrue:[ |
462 |
"This normally raises an error. If you proceed, you get what has been collected (nothing)." |
|
463 |
self pastEndRead. |
|
464 |
^ species new |
|
1082 | 465 |
]. |
466 |
data := species new:n. |
|
3284 | 467 |
count := self next:n into:data startingAt:1. |
468 |
count = n ifTrue:[ |
|
469 |
^ data. |
|
1082 | 470 |
]. |
3284 | 471 |
"This normally raises an error. If you proceed, you get what has been collected." |
472 |
self pastEndRead. |
|
473 |
^ data copyFrom:1 to:count. |
|
1082 | 474 |
! |
475 |
||
2191 | 476 |
next:n into:aBuffer startingAt:startIndex |
477 |
"read the next n elements of the stream into aBuffer. |
|
478 |
Return the number of bytes read." |
|
479 |
||
2192 | 480 |
|count remaining offset| |
2191 | 481 |
|
3284 | 482 |
mode ~~ #readonly ifTrue:[ |
483 |
self errorWriteOnly |
|
484 |
]. |
|
485 |
self fillBuffer ifFalse:[ |
|
486 |
self pastEndRead isNil ifTrue:[^ 0]. |
|
2191 | 487 |
]. |
488 |
offset := startIndex. |
|
489 |
remaining := n. |
|
490 |
||
3284 | 491 |
[ |
492 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
|
493 |
offset := count + offset. |
|
494 |
remaining := remaining - count. |
|
3292 | 495 |
remaining ~~ 0 and:[self fillBuffer] |
3284 | 496 |
] whileTrue. |
2191 | 497 |
^ n - remaining |
498 |
! |
|
499 |
||
3099 | 500 |
nextByte |
501 |
"return the next element, a byte |
|
3284 | 502 |
raise an error, if there are no more elements" |
503 |
||
504 |
mode ~~ #readonly ifTrue:[ |
|
505 |
self errorWriteOnly |
|
506 |
]. |
|
507 |
self fillBuffer ifFalse:[ |
|
508 |
"there is no more element; the stream is at end" |
|
509 |
^ self pastEndRead. |
|
510 |
]. |
|
511 |
position := position + 1. |
|
512 |
^ outputBytes at:position. |
|
513 |
! |
|
514 |
||
515 |
nextByteOrNil |
|
516 |
"return the next element, a byte |
|
517 |
return nil, if there are no more elements" |
|
518 |
||
519 |
mode ~~ #readonly ifTrue:[ |
|
520 |
self errorWriteOnly |
|
521 |
]. |
|
522 |
self fillBuffer ifFalse:[ |
|
523 |
"there is no more element; the stream is at end" |
|
524 |
^ nil. |
|
525 |
]. |
|
526 |
position := position + 1. |
|
527 |
^ outputBytes at:position. |
|
528 |
! |
|
529 |
||
530 |
nextOrNil |
|
531 |
"return the next element, a character or byte (textmode) |
|
3099 | 532 |
return nil, if there are no more elements" |
533 |
||
534 |
|byte| |
|
535 |
||
3284 | 536 |
(byte := self nextByteOrNil) isNil ifTrue:[ |
3099 | 537 |
"there is no more element; the stream is at end" |
538 |
^ nil |
|
539 |
]. |
|
3284 | 540 |
binary ifTrue:[^ byte]. |
541 |
^ Character value:byte |
|
3099 | 542 |
! |
543 |
||
2190 | 544 |
skip:count |
1082 | 545 |
"skip count objects, return the receiver |
1193 | 546 |
redefined for optimization" |
547 |
||
1082 | 548 |
|n avail| |
549 |
||
3284 | 550 |
mode ~~ #readonly ifTrue:[ |
551 |
self errorWriteOnly |
|
1083 | 552 |
]. |
553 |
||
3284 | 554 |
n := count. |
555 |
n <= 0 ifTrue:[ |
|
556 |
n ~~ 0 ifTrue:[ |
|
557 |
"don't know how to unread ..." |
|
558 |
PositionError raiseRequest |
|
559 |
]. |
|
560 |
^ self |
|
561 |
]. |
|
1082 | 562 |
|
3284 | 563 |
[self fillBuffer] whileTrue:[ |
564 |
avail := readLimit - position. |
|
565 |
avail >= n ifTrue:[ |
|
566 |
position := position + n. |
|
567 |
^ self |
|
568 |
]. |
|
569 |
position := readLimit := 0. "/ reset input |
|
570 |
n := n - avail. |
|
1082 | 571 |
]. |
1059 | 572 |
! ! |
573 |
||
574 |
!CompressionStream methodsFor:'startup & release'! |
|
575 |
||
576 |
close |
|
1193 | 577 |
"close the zip-stream" |
578 |
||
1059 | 579 |
hitEOF := true. |
1193 | 580 |
zstream notNil ifTrue:[ |
1195 | 581 |
self flush. |
582 |
self closeZStream. |
|
1059 | 583 |
]. |
584 |
! |
|
585 |
||
586 |
openWithMode:aMode on:aStream |
|
1061 | 587 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
588 |
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
|
589 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
590 |
" |
2195 | 591 |
|
592 |
^ self |
|
593 |
openWithMode:aMode |
|
594 |
on:aStream |
|
595 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
596 |
! |
|
597 |
||
598 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
599 |
"open the zip-stream on a stream |
|
600 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
601 |
in the ZipStream |
|
602 |
" |
|
603 |
||
604 |
^ self |
|
605 |
streamOpenWithMode:aMode |
|
606 |
on:aStream |
|
607 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
608 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
609 |
|
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
610 |
streamOpenWithMode:aMode on:aStream |
2195 | 611 |
|
612 |
^ self |
|
613 |
streamOpenWithMode:aMode |
|
614 |
on:aStream |
|
615 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
616 |
! |
|
617 |
||
618 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
619 |
"open the compression stream on a stream |
2195 | 620 |
#readonly uncompress the data derived from the read-stream, aStream |
621 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 622 |
" |
1059 | 623 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
624 |
aStream isNil ifTrue:[ |
2195 | 625 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
626 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
627 |
|
2190 | 628 |
onStream := aStream. |
1059 | 629 |
mode := aMode. |
1829 | 630 |
outputBytes := ExternalBytes unprotectedNew:16384. |
631 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 632 |
readLimit := position := 0. |
633 |
binary := false. |
|
2195 | 634 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 635 |
|
636 |
self zopen. |
|
637 |
self registerForFinalization. |
|
638 |
||
639 |
hitEOF := false. |
|
640 |
||
641 |
aMode == #readonly ifTrue:[ |
|
2195 | 642 |
self zinflateInit. |
1059 | 643 |
] ifFalse:[ |
2195 | 644 |
self zdeflateInit |
1059 | 645 |
]. |
646 |
! ! |
|
647 |
||
648 |
!CompressionStream methodsFor:'writing'! |
|
649 |
||
1062 | 650 |
contents:contents |
1193 | 651 |
"write the entire contents to and close the stream" |
652 |
||
3284 | 653 |
self nextPutAll:contents. |
1062 | 654 |
self close. |
655 |
! |
|
656 |
||
1059 | 657 |
flush |
1193 | 658 |
"flush the input and output buffer" |
659 |
||
1829 | 660 |
|continue availOut| |
1059 | 661 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
662 |
self isWritable ifFalse:[^ self]. |
1059 | 663 |
self zset_avail_in:position. |
664 |
||
665 |
position := 0. |
|
666 |
||
1829 | 667 |
[ continue := self zdeflate. |
2190 | 668 |
availOut := self zget_avail_out. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
669 |
|
2190 | 670 |
availOut > 0 ifTrue:[ |
671 |
self onStreamPutBytes:availOut from:outputBytes |
|
672 |
]. |
|
1829 | 673 |
|
1827 | 674 |
] doWhile:[ continue == true ]. |
1059 | 675 |
! |
676 |
||
1193 | 677 |
nextPut:aByteOrCharacter |
678 |
"write the argument, aByteOrCharacter" |
|
1059 | 679 |
|
1193 | 680 |
position == inputBytes size ifTrue:[self flush]. |
681 |
position := position + 1. |
|
682 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 683 |
! |
684 |
||
685 |
nextPutAll:aCollection |
|
686 |
|limit| |
|
687 |
||
688 |
limit := inputBytes size. |
|
689 |
||
690 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 691 |
position == limit ifTrue:[self flush]. |
692 |
position := position + 1. |
|
693 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 694 |
]. |
1193 | 695 |
! ! |
1059 | 696 |
|
1193 | 697 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 698 |
|
1193 | 699 |
version |
3292 | 700 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.34 2014-06-03 10:34:04 stefan Exp $' |
2293 | 701 |
! |
702 |
||
703 |
version_CVS |
|
3292 | 704 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.34 2014-06-03 10:34:04 stefan Exp $' |
1059 | 705 |
! ! |
706 |
||
3099 | 707 |
|
1059 | 708 |
CompressionStream initialize! |