author | Claus Gittinger <cg@exept.de> |
Sun, 05 Jun 2016 09:57:02 +0200 | |
changeset 3881 | 16890fe7ef2e |
parent 3316 | 7d8e613eb9da |
child 4205 | 126f38d59bbe |
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 |
||
3293 | 335 |
[ |
336 |
n := onStream nextAvailableBytes:(inputBytes size) into:inputBytes startingAt:1. |
|
337 |
n == 0 ifTrue:[ |
|
338 |
onStream atEnd ifTrue:[ |
|
339 |
hitEOF := true. |
|
340 |
^ false. |
|
341 |
] ifFalse:[ |
|
342 |
"must be a socket or pipe" |
|
343 |
onStream readWait. |
|
344 |
]. |
|
345 |
]. |
|
346 |
n == 0 |
|
347 |
] whileTrue. |
|
3284 | 348 |
self zset_avail_in:n. |
349 |
]. |
|
350 |
readLimit isNil ifTrue:[ |
|
351 |
hitEOF := true. |
|
352 |
^ false |
|
353 |
]. |
|
354 |
position := 0. |
|
355 |
]. |
|
356 |
^ true |
|
357 |
! |
|
358 |
||
1059 | 359 |
onStreamPutBytes:count from:data |
1193 | 360 |
"write compressed data to the (output) stream" |
361 |
||
1059 | 362 |
onStream nextPutBytes:count from:data startingAt:1 |
363 |
! ! |
|
364 |
||
365 |
!CompressionStream methodsFor:'queries'! |
|
366 |
||
367 |
atEnd |
|
1193 | 368 |
"return true if the end of the compressed input stream has been reached" |
369 |
||
3284 | 370 |
^ self fillBuffer not |
1062 | 371 |
! |
372 |
||
1059 | 373 |
isBinary |
374 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 375 |
The default when created is false" |
376 |
||
1059 | 377 |
^ binary |
378 |
! |
|
379 |
||
2195 | 380 |
isHeaderAndChecksumSuppressed |
381 |
"answer true if the checksum and header are suppressed; |
|
382 |
the default is true (backward compatibility)" |
|
383 |
||
384 |
suppressHeaderAndChecksum isNil ifTrue:[ |
|
385 |
^ self class defaultSuppressHeaderAndChecksum |
|
386 |
]. |
|
387 |
^ suppressHeaderAndChecksum |
|
388 |
! |
|
389 |
||
1059 | 390 |
isOpen |
1193 | 391 |
"return true, if this stream is open" |
392 |
||
1059 | 393 |
^ onStream notNil |
394 |
! |
|
395 |
||
1193 | 396 |
isReadable |
397 |
"return true, if this stream can be read from" |
|
398 |
||
1059 | 399 |
^ mode == #readonly |
400 |
! |
|
401 |
||
1193 | 402 |
isWritable |
403 |
"return true, if this stream can be written to" |
|
404 |
||
1059 | 405 |
^ mode == #writeonly |
406 |
! ! |
|
407 |
||
408 |
!CompressionStream methodsFor:'reading'! |
|
409 |
||
410 |
contents |
|
1193 | 411 |
"return the entire contents of and close the stream" |
412 |
||
1081 | 413 |
|species stream bfsize buffer count| |
1059 | 414 |
|
3284 | 415 |
mode ~~ #readonly ifTrue:[ |
416 |
self errorWriteOnly |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
417 |
]. |
3284 | 418 |
species := self contentsSpecies. |
419 |
self fillBuffer ifFalse:[ |
|
420 |
"at end" |
|
421 |
^ species new. |
|
422 |
]. |
|
1059 | 423 |
|
3284 | 424 |
bfsize := outputBytes size. |
1081 | 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 |
|
3284 | 428 |
[ |
429 |
count := self z_nextAvailableInto:buffer startingAt:1 maxCount:nil. |
|
1079 | 430 |
|
3284 | 431 |
count == bfsize ifTrue:[ |
432 |
stream nextPutAll:buffer. |
|
433 |
] ifFalse:[ |
|
434 |
count > 0 ifTrue:[ |
|
435 |
stream nextPutAll:buffer startingAt:1 to:count. |
|
436 |
] |
|
437 |
]. |
|
438 |
self fillBuffer |
|
439 |
] whileTrue. |
|
1062 | 440 |
self close. |
1193 | 441 |
^ stream contents |
1059 | 442 |
! |
443 |
||
444 |
next |
|
445 |
"return the next element, a character or byte (textmode) |
|
3284 | 446 |
raise an error, if there are no more elements" |
1193 | 447 |
|
1059 | 448 |
|byte| |
449 |
||
3099 | 450 |
(byte := self nextByte) isNil ifTrue:[ |
451 |
"there is no more element; the stream is at end" |
|
452 |
^ nil |
|
1059 | 453 |
]. |
454 |
binary ifTrue:[^ byte ]. |
|
1193 | 455 |
^ Character value:byte |
1082 | 456 |
! |
457 |
||
458 |
next:n |
|
459 |
"return the next count elements of the stream as a collection. |
|
1193 | 460 |
Redefined to return a String or ByteArray and for optimization" |
461 |
||
3284 | 462 |
|data count species| |
1082 | 463 |
|
3284 | 464 |
mode ~~ #readonly ifTrue:[ |
465 |
self errorWriteOnly |
|
466 |
]. |
|
1082 | 467 |
species := self contentsSpecies. |
468 |
||
3284 | 469 |
hitEOF ifTrue:[ |
470 |
"This normally raises an error. If you proceed, you get what has been collected (nothing)." |
|
471 |
self pastEndRead. |
|
472 |
^ species new |
|
1082 | 473 |
]. |
474 |
data := species new:n. |
|
3284 | 475 |
count := self next:n into:data startingAt:1. |
476 |
count = n ifTrue:[ |
|
477 |
^ data. |
|
1082 | 478 |
]. |
3284 | 479 |
"This normally raises an error. If you proceed, you get what has been collected." |
480 |
self pastEndRead. |
|
481 |
^ data copyFrom:1 to:count. |
|
1082 | 482 |
! |
483 |
||
2191 | 484 |
next:n into:aBuffer startingAt:startIndex |
485 |
"read the next n elements of the stream into aBuffer. |
|
486 |
Return the number of bytes read." |
|
487 |
||
2192 | 488 |
|count remaining offset| |
2191 | 489 |
|
3284 | 490 |
mode ~~ #readonly ifTrue:[ |
491 |
self errorWriteOnly |
|
492 |
]. |
|
493 |
self fillBuffer ifFalse:[ |
|
494 |
self pastEndRead isNil ifTrue:[^ 0]. |
|
2191 | 495 |
]. |
496 |
offset := startIndex. |
|
497 |
remaining := n. |
|
498 |
||
3284 | 499 |
[ |
500 |
count := self z_nextAvailableInto:aBuffer startingAt:offset maxCount:remaining. |
|
501 |
offset := count + offset. |
|
502 |
remaining := remaining - count. |
|
3292 | 503 |
remaining ~~ 0 and:[self fillBuffer] |
3284 | 504 |
] whileTrue. |
2191 | 505 |
^ n - remaining |
506 |
! |
|
507 |
||
3099 | 508 |
nextByte |
509 |
"return the next element, a byte |
|
3284 | 510 |
raise an error, if there are no more elements" |
511 |
||
512 |
mode ~~ #readonly ifTrue:[ |
|
513 |
self errorWriteOnly |
|
514 |
]. |
|
515 |
self fillBuffer ifFalse:[ |
|
516 |
"there is no more element; the stream is at end" |
|
517 |
^ self pastEndRead. |
|
518 |
]. |
|
519 |
position := position + 1. |
|
520 |
^ outputBytes at:position. |
|
521 |
! |
|
522 |
||
523 |
nextByteOrNil |
|
524 |
"return the next element, a byte |
|
525 |
return nil, if there are no more elements" |
|
526 |
||
527 |
mode ~~ #readonly ifTrue:[ |
|
528 |
self errorWriteOnly |
|
529 |
]. |
|
530 |
self fillBuffer ifFalse:[ |
|
531 |
"there is no more element; the stream is at end" |
|
532 |
^ nil. |
|
533 |
]. |
|
534 |
position := position + 1. |
|
535 |
^ outputBytes at:position. |
|
536 |
! |
|
537 |
||
3316 | 538 |
nextBytes:numBytes into:aByteCollection startingAt:initialIndex |
539 |
"can do it faster here than in super class" |
|
540 |
||
541 |
^ self next:numBytes into:aByteCollection startingAt:initialIndex. |
|
542 |
! |
|
543 |
||
3284 | 544 |
nextOrNil |
545 |
"return the next element, a character or byte (textmode) |
|
3099 | 546 |
return nil, if there are no more elements" |
547 |
||
548 |
|byte| |
|
549 |
||
3284 | 550 |
(byte := self nextByteOrNil) isNil ifTrue:[ |
3099 | 551 |
"there is no more element; the stream is at end" |
552 |
^ nil |
|
553 |
]. |
|
3284 | 554 |
binary ifTrue:[^ byte]. |
555 |
^ Character value:byte |
|
3099 | 556 |
! |
557 |
||
2190 | 558 |
skip:count |
1082 | 559 |
"skip count objects, return the receiver |
1193 | 560 |
redefined for optimization" |
561 |
||
1082 | 562 |
|n avail| |
563 |
||
3284 | 564 |
mode ~~ #readonly ifTrue:[ |
565 |
self errorWriteOnly |
|
1083 | 566 |
]. |
567 |
||
3284 | 568 |
n := count. |
569 |
n <= 0 ifTrue:[ |
|
570 |
n ~~ 0 ifTrue:[ |
|
571 |
"don't know how to unread ..." |
|
572 |
PositionError raiseRequest |
|
573 |
]. |
|
574 |
^ self |
|
575 |
]. |
|
1082 | 576 |
|
3284 | 577 |
[self fillBuffer] whileTrue:[ |
578 |
avail := readLimit - position. |
|
579 |
avail >= n ifTrue:[ |
|
580 |
position := position + n. |
|
581 |
^ self |
|
582 |
]. |
|
583 |
position := readLimit := 0. "/ reset input |
|
584 |
n := n - avail. |
|
1082 | 585 |
]. |
1059 | 586 |
! ! |
587 |
||
588 |
!CompressionStream methodsFor:'startup & release'! |
|
589 |
||
590 |
close |
|
1193 | 591 |
"close the zip-stream" |
592 |
||
1059 | 593 |
hitEOF := true. |
1193 | 594 |
zstream notNil ifTrue:[ |
1195 | 595 |
self flush. |
596 |
self closeZStream. |
|
1059 | 597 |
]. |
598 |
! |
|
599 |
||
600 |
openWithMode:aMode on:aStream |
|
1061 | 601 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
602 |
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
|
603 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
604 |
" |
2195 | 605 |
|
606 |
^ self |
|
607 |
openWithMode:aMode |
|
608 |
on:aStream |
|
609 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
610 |
! |
|
611 |
||
612 |
openWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
613 |
"open the zip-stream on a stream |
|
614 |
can be reimplemented to do some additional stuff (e.g. gzip header) like |
|
615 |
in the ZipStream |
|
616 |
" |
|
617 |
||
618 |
^ self |
|
619 |
streamOpenWithMode:aMode |
|
620 |
on:aStream |
|
621 |
suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
622 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
623 |
|
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
624 |
streamOpenWithMode:aMode on:aStream |
2195 | 625 |
|
626 |
^ self |
|
627 |
streamOpenWithMode:aMode |
|
628 |
on:aStream |
|
629 |
suppressHeaderAndChecksum:(self class defaultSuppressHeaderAndChecksum) |
|
630 |
! |
|
631 |
||
632 |
streamOpenWithMode:aMode on:aStream suppressHeaderAndChecksum:aBoolean |
|
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
633 |
"open the compression stream on a stream |
2195 | 634 |
#readonly uncompress the data derived from the read-stream, aStream |
635 |
#writeonly compress the data and write to the write-stream, aStream |
|
1061 | 636 |
" |
1059 | 637 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
638 |
aStream isNil ifTrue:[ |
2195 | 639 |
^ self errorNotOpen |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
640 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
641 |
|
2190 | 642 |
onStream := aStream. |
1059 | 643 |
mode := aMode. |
1829 | 644 |
outputBytes := ExternalBytes unprotectedNew:16384. |
645 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 646 |
readLimit := position := 0. |
647 |
binary := false. |
|
2195 | 648 |
suppressHeaderAndChecksum := aBoolean. |
1059 | 649 |
|
650 |
self zopen. |
|
651 |
self registerForFinalization. |
|
652 |
||
653 |
hitEOF := false. |
|
654 |
||
655 |
aMode == #readonly ifTrue:[ |
|
2195 | 656 |
self zinflateInit. |
1059 | 657 |
] ifFalse:[ |
2195 | 658 |
self zdeflateInit |
1059 | 659 |
]. |
660 |
! ! |
|
661 |
||
662 |
!CompressionStream methodsFor:'writing'! |
|
663 |
||
1062 | 664 |
contents:contents |
1193 | 665 |
"write the entire contents to and close the stream" |
666 |
||
3284 | 667 |
self nextPutAll:contents. |
1062 | 668 |
self close. |
669 |
! |
|
670 |
||
1059 | 671 |
flush |
1193 | 672 |
"flush the input and output buffer" |
673 |
||
1829 | 674 |
|continue availOut| |
1059 | 675 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
676 |
self isWritable ifFalse:[^ self]. |
1059 | 677 |
self zset_avail_in:position. |
678 |
||
679 |
position := 0. |
|
680 |
||
1829 | 681 |
[ continue := self zdeflate. |
2190 | 682 |
availOut := self zget_avail_out. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
683 |
|
2190 | 684 |
availOut > 0 ifTrue:[ |
685 |
self onStreamPutBytes:availOut from:outputBytes |
|
686 |
]. |
|
1829 | 687 |
|
1827 | 688 |
] doWhile:[ continue == true ]. |
1059 | 689 |
! |
690 |
||
1193 | 691 |
nextPut:aByteOrCharacter |
692 |
"write the argument, aByteOrCharacter" |
|
1059 | 693 |
|
1193 | 694 |
position == inputBytes size ifTrue:[self flush]. |
695 |
position := position + 1. |
|
696 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 697 |
! |
698 |
||
699 |
nextPutAll:aCollection |
|
700 |
|limit| |
|
701 |
||
702 |
limit := inputBytes size. |
|
703 |
||
704 |
aCollection do:[:aByteOrCharacter| |
|
2293 | 705 |
position == limit ifTrue:[self flush]. |
706 |
position := position + 1. |
|
707 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 708 |
]. |
1193 | 709 |
! ! |
1059 | 710 |
|
1193 | 711 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 712 |
|
1193 | 713 |
version |
3316 | 714 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.36 2014-07-03 13:37:32 stefan Exp $' |
2293 | 715 |
! |
716 |
||
717 |
version_CVS |
|
3316 | 718 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.36 2014-07-03 13:37:32 stefan Exp $' |
1059 | 719 |
! ! |
720 |
||
3099 | 721 |
|
1059 | 722 |
CompressionStream initialize! |