author | ab |
Thu, 05 Jun 2008 11:15:06 +0200 | |
changeset 2005 | a6ef1083aa32 |
parent 1829 | 4dd5f9f30878 |
child 2190 | 6c3f9241753a |
permissions | -rw-r--r-- |
1308 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
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 |
16 |
outputBytes zstream' |
|
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 |
|
27 |
All Rights Reserved |
|
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:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
43 |
Claus Atzkern |
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:] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
50 |
|
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 |
|
1059 | 65 |
^ self basicNew openWithMode:#readonly on:aStream |
66 |
! |
|
67 |
||
68 |
writeOpenOn:aStream |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
69 |
"open to write data compressed to stream" |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
70 |
|
1059 | 71 |
^ self basicNew openWithMode:#writeonly on:aStream |
72 |
! ! |
|
73 |
||
74 |
!CompressionStream methodsFor:'accessing'! |
|
75 |
||
76 |
binary |
|
77 |
"switch to binary mode - default is text |
|
78 |
" |
|
79 |
binary := true. |
|
80 |
! |
|
81 |
||
82 |
text |
|
1193 | 83 |
"switch to text mode - default is text" |
84 |
||
1059 | 85 |
binary := false. |
86 |
! ! |
|
87 |
||
88 |
!CompressionStream methodsFor:'error handling'! |
|
89 |
||
90 |
errorNotOpen |
|
1193 | 91 |
"report an error, that the stream has not been opened" |
92 |
||
1059 | 93 |
self zerror:'not open'. |
94 |
! |
|
95 |
||
96 |
errorReadOnly |
|
1193 | 97 |
"report an error, that the stream is a readOnly stream" |
98 |
||
1059 | 99 |
self zerror:'is readonly' |
100 |
! |
|
101 |
||
102 |
errorWriteOnly |
|
1193 | 103 |
"report an error, that the stream is a writeOnly stream" |
104 |
||
1059 | 105 |
self zerror:'is writeonly' |
106 |
! |
|
107 |
||
108 |
invalidArgument |
|
1193 | 109 |
"called if a method is invoked with invalid parameters" |
110 |
||
1059 | 111 |
self zerror:'invalid arguments'. |
112 |
! |
|
113 |
||
1481 | 114 |
pastEndRead |
1059 | 115 |
self zerror:'end of stream'. |
1782 | 116 |
^ nil |
117 |
||
118 |
"Modified: / 18-11-2006 / 15:37:08 / cg" |
|
1059 | 119 |
! |
120 |
||
121 |
zerror:anError |
|
122 |
|error| |
|
123 |
||
124 |
zstream isNil ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
125 |
error := 'not open'. |
1059 | 126 |
] ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
127 |
anError isNumber ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
128 |
anError == 1 ifTrue:[ error := 'stream at end' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
129 |
ifFalse:[anError == -1 ifTrue:[ error := 'processing error: ', anError printString ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
130 |
ifFalse:[anError == -2 ifTrue:[ error := 'processing error' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
131 |
ifFalse:[anError == -3 ifTrue:[ error := 'input data are corrupted' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
132 |
ifFalse:[anError == -4 ifTrue:[ error := 'not enough memory' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
133 |
ifFalse:[anError == -5 ifTrue:[ error := 'not enough memory in the output stream' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
134 |
ifFalse:[anError == -6 ifTrue:[ error := 'version error' ] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
135 |
ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
136 |
error := 'compressing error: ', anError printString |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
137 |
]]]]]]]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
138 |
] ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
139 |
error := anError printString |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
140 |
]. |
1059 | 141 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
142 |
StreamError raiseErrorString:(self class name , ': ', error). |
1059 | 143 |
! ! |
144 |
||
145 |
!CompressionStream methodsFor:'finalization'! |
|
146 |
||
147 |
executor |
|
148 |
"redefined to return a lightweight copy |
|
1193 | 149 |
- all we need is the memory handle" |
150 |
||
1059 | 151 |
^ self class basicNew finalizeCopy:zstream. |
152 |
! |
|
153 |
||
154 |
finalize |
|
1061 | 155 |
"the compressin-stream was garbage collected; |
1193 | 156 |
close the underlying zip-stream" |
157 |
||
1061 | 158 |
self closeZStream. |
1059 | 159 |
! |
160 |
||
161 |
finalizeCopy:aZStream |
|
1193 | 162 |
"used for finalization to close the underlying zip-stream" |
163 |
||
1059 | 164 |
zstream := aZStream. |
165 |
! ! |
|
166 |
||
167 |
!CompressionStream methodsFor:'low level'! |
|
168 |
||
1079 | 169 |
z_nextAvailableInto:aCollection startingAt:offset |
170 |
"read the next available bytes into a collection, a string or byteArray; |
|
1193 | 171 |
returns the size read" |
172 |
||
1079 | 173 |
|start count avail| |
174 |
||
175 |
avail := readLimit - position. |
|
176 |
avail > 0 ifFalse:[^ 0]. |
|
177 |
||
178 |
count := aCollection size - offset + 1. |
|
179 |
||
180 |
count > 0 ifFalse:[ |
|
1195 | 181 |
count < 0 ifTrue:[ |
182 |
self zerror:'invalid arguments' |
|
183 |
]. |
|
184 |
^ 0 |
|
1079 | 185 |
]. |
186 |
count := avail min:count. |
|
187 |
start := position. |
|
188 |
position := position + count. |
|
189 |
||
190 |
%{ unsigned char * _dstPt; |
|
191 |
||
192 |
if( __isBytes(aCollection) ) { |
|
1195 | 193 |
_dstPt = (unsigned char *) (__ByteArrayInstPtr(aCollection)->ba_element); |
1079 | 194 |
} else if (__isString(aCollection)) { |
1195 | 195 |
_dstPt = (unsigned char *) (__stringVal( aCollection)); |
1079 | 196 |
} else |
1195 | 197 |
_dstPt = (unsigned char *) 0; |
1079 | 198 |
|
199 |
if( _dstPt ) |
|
200 |
{ |
|
1195 | 201 |
int _loop, _count, _offset; |
202 |
unsigned char * _srcPt; |
|
203 |
OBJ _srcObj = __INST( outputBytes ); |
|
1079 | 204 |
|
1195 | 205 |
_offset = __intVal( offset ); |
206 |
_dstPt = _dstPt + _offset - 1; |
|
1079 | 207 |
|
1195 | 208 |
_srcPt = (unsigned char *) __externalBytesAddress( _srcObj ); |
209 |
_srcPt += __intVal( start ); |
|
210 |
_count = __intVal( count ); |
|
1079 | 211 |
|
1195 | 212 |
for( _loop = 0; _loop < _count; ++_loop ) |
213 |
* _dstPt++ = * _srcPt++; |
|
1079 | 214 |
|
1195 | 215 |
RETURN(__MKSMALLINT(_count)); |
1079 | 216 |
} |
217 |
%}. |
|
218 |
||
219 |
^ self zerror:'invalid argument' |
|
220 |
! |
|
221 |
||
1193 | 222 |
zclose |
223 |
"low level close of the zip stream" |
|
224 |
||
225 |
^ self subclassResponsibility |
|
226 |
! |
|
227 |
||
228 |
zdeflate |
|
229 |
"low level - deflate |
|
230 |
returns false if the deflate operation is finished otherwise true" |
|
231 |
||
232 |
^ self subclassResponsibility |
|
233 |
! |
|
234 |
||
235 |
zdeflateInit |
|
236 |
"low level - deflateInit |
|
237 |
initialize the deflate mode, write header" |
|
238 |
||
239 |
^ self subclassResponsibility |
|
240 |
! |
|
241 |
||
242 |
zget_avail_out |
|
243 |
"low level - get the number of available out bytes" |
|
244 |
||
245 |
^ self subclassResponsibility |
|
246 |
! |
|
247 |
||
248 |
zinflate |
|
249 |
"low level - inflate |
|
250 |
returns nil if at uncompress is finished, or the number of |
|
251 |
available bytes in the output-buffer" |
|
252 |
||
253 |
^ self subclassResponsibility |
|
254 |
! |
|
255 |
||
256 |
zinflateInit |
|
257 |
"low level - inflateInit |
|
258 |
initialize the inflate mode, read and check header" |
|
259 |
||
260 |
^ self subclassResponsibility |
|
261 |
! |
|
262 |
||
1059 | 263 |
zopen |
264 |
"low level - opens the zip stream |
|
1193 | 265 |
create the resources" |
266 |
||
1059 | 267 |
^ self subclassResponsibility |
268 |
! |
|
269 |
||
270 |
zset_avail_in:count |
|
1193 | 271 |
"set the 'avail_in' and compute the crc" |
272 |
||
1059 | 273 |
^ self subclassResponsibility |
274 |
! ! |
|
275 |
||
276 |
!CompressionStream methodsFor:'private'! |
|
277 |
||
1061 | 278 |
closeZStream |
1193 | 279 |
"close the zip-stream" |
280 |
||
1061 | 281 |
onStream := mode := nil. |
282 |
hitEOF := true. |
|
283 |
||
284 |
zstream ifNotNil:[ |
|
1195 | 285 |
self unregisterForFinalization. |
286 |
self zclose. |
|
287 |
zstream := nil. |
|
1061 | 288 |
]. |
289 |
! |
|
290 |
||
1081 | 291 |
contentsSpecies |
1193 | 292 |
"return the kind of object to be returned by sub-collection builders" |
293 |
||
294 |
binary == true ifTrue:[^ ByteArray]. |
|
295 |
^ String |
|
1081 | 296 |
! |
297 |
||
1059 | 298 |
onStreamPutBytes:count from:data |
1193 | 299 |
"write compressed data to the (output) stream" |
300 |
||
1059 | 301 |
onStream nextPutBytes:count from:data startingAt:1 |
302 |
! ! |
|
303 |
||
304 |
!CompressionStream methodsFor:'queries'! |
|
305 |
||
306 |
atEnd |
|
1193 | 307 |
"return true if the end of the compressed input stream has been reached" |
308 |
||
1829 | 309 |
^ self canReadWithoutBlocking not |
1059 | 310 |
! |
311 |
||
1062 | 312 |
canReadWithoutBlocking |
313 |
"returns true if data are available for reading; |
|
314 |
false if the stream is at end. |
|
1193 | 315 |
updates the readLimit and position" |
316 |
||
1829 | 317 |
|n| |
318 |
||
1062 | 319 |
mode == #readonly ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
320 |
self errorWriteOnly |
1062 | 321 |
]. |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
322 |
hitEOF == true ifTrue:[^ false]. |
1062 | 323 |
|
324 |
position >= readLimit ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
325 |
[(readLimit := self zinflate) == 0] whileTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
326 |
n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1. |
1829 | 327 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
328 |
n == 0 ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
329 |
hitEOF := true. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
330 |
^ false |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
331 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
332 |
self zset_avail_in:n. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
333 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
334 |
readLimit isNil ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
335 |
hitEOF := true. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
336 |
^ false |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
337 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
338 |
position := 0. |
1062 | 339 |
]. |
340 |
^ true |
|
341 |
! |
|
342 |
||
1059 | 343 |
isBinary |
344 |
"return true, if the stream is in binary (as opposed to text-) mode. |
|
1193 | 345 |
The default when created is false" |
346 |
||
1059 | 347 |
^ binary |
348 |
! |
|
349 |
||
350 |
isOpen |
|
1193 | 351 |
"return true, if this stream is open" |
352 |
||
1059 | 353 |
^ onStream notNil |
354 |
! |
|
355 |
||
1193 | 356 |
isReadable |
357 |
"return true, if this stream can be read from" |
|
358 |
||
1059 | 359 |
^ mode == #readonly |
360 |
! |
|
361 |
||
1193 | 362 |
isWritable |
363 |
"return true, if this stream can be written to" |
|
364 |
||
1059 | 365 |
^ mode == #writeonly |
366 |
! ! |
|
367 |
||
368 |
!CompressionStream methodsFor:'reading'! |
|
369 |
||
370 |
contents |
|
1193 | 371 |
"return the entire contents of and close the stream" |
372 |
||
1081 | 373 |
|species stream bfsize buffer count| |
1059 | 374 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
375 |
mode == #readonly ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
376 |
self errorWriteOnly |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
377 |
]. |
1062 | 378 |
bfsize := outputBytes size. |
1059 | 379 |
|
1081 | 380 |
species := self contentsSpecies. |
381 |
buffer := species new:bfsize. |
|
1428
c9f7ec7d0c84
testcases extracted into spearate class
Claus Gittinger <cg@exept.de>
parents:
1308
diff
changeset
|
382 |
stream := WriteStream on:(species new:bfsize). |
1079 | 383 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
384 |
[self canReadWithoutBlocking] whileTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
385 |
count := self z_nextAvailableInto:buffer startingAt:1. |
1079 | 386 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
387 |
count == bfsize ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
388 |
stream nextPutAll:buffer. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
389 |
] ifFalse:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
390 |
count > 0 ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
391 |
stream nextPutAll:buffer startingAt:1 to:count. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
392 |
] |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
393 |
]. |
1059 | 394 |
]. |
1062 | 395 |
self close. |
1193 | 396 |
^ stream contents |
1059 | 397 |
! |
398 |
||
399 |
next |
|
400 |
"return the next element, a character or byte (textmode) |
|
1193 | 401 |
return nil, if there are no more elements" |
402 |
||
1059 | 403 |
|byte| |
404 |
||
1062 | 405 |
self canReadWithoutBlocking ifFalse:[ |
1195 | 406 |
"there is no more element; the stream is at end" |
407 |
^ nil |
|
1059 | 408 |
]. |
409 |
position := position + 1. |
|
410 |
byte := outputBytes at:position. |
|
411 |
||
412 |
binary ifTrue:[^ byte ]. |
|
1193 | 413 |
^ Character value:byte |
1082 | 414 |
! |
415 |
||
416 |
next:n |
|
417 |
"return the next count elements of the stream as a collection. |
|
1193 | 418 |
Redefined to return a String or ByteArray and for optimization" |
419 |
||
1082 | 420 |
|data count offset species| |
421 |
||
422 |
species := self contentsSpecies. |
|
423 |
||
424 |
self canReadWithoutBlocking ifFalse:[ |
|
1195 | 425 |
^ species new |
1082 | 426 |
]. |
427 |
data := species new:n. |
|
428 |
offset := 1. |
|
429 |
||
1193 | 430 |
[self canReadWithoutBlocking] whileTrue:[ |
1195 | 431 |
count := self z_nextAvailableInto:data startingAt:offset. |
432 |
offset := count + offset. |
|
433 |
offset > n ifTrue:[^ data] |
|
1082 | 434 |
]. |
435 |
^ data copyFrom:1 to:(offset - 1) |
|
436 |
! |
|
437 |
||
438 |
skip:count |
|
439 |
"skip count objects, return the receiver |
|
1193 | 440 |
redefined for optimization" |
441 |
||
1082 | 442 |
|n avail| |
443 |
||
444 |
n := count. |
|
445 |
||
1193 | 446 |
n <= 0 ifTrue:[ |
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
447 |
n ~~ 0 ifTrue:[ |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
448 |
"dont know how to unread ..." |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
449 |
PositionError raiseRequest |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
450 |
]. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
451 |
^ self |
1083 | 452 |
]. |
453 |
||
1193 | 454 |
[self canReadWithoutBlocking] whileTrue:[ |
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
455 |
avail := readLimit - position. |
1082 | 456 |
|
1446
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
457 |
avail >= n ifTrue:[ |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
458 |
position := position + n. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
459 |
^ self |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
460 |
]. |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
461 |
position := readLimit := 0. "/ reset input |
c59e6816f4b8
Use exception classes instead of class vars
Stefan Vogel <sv@exept.de>
parents:
1428
diff
changeset
|
462 |
n := n - avail. |
1082 | 463 |
]. |
1059 | 464 |
! ! |
465 |
||
466 |
!CompressionStream methodsFor:'startup & release'! |
|
467 |
||
468 |
close |
|
1193 | 469 |
"close the zip-stream" |
470 |
||
1059 | 471 |
hitEOF := true. |
1193 | 472 |
zstream notNil ifTrue:[ |
1195 | 473 |
self flush. |
474 |
self closeZStream. |
|
1059 | 475 |
]. |
476 |
! |
|
477 |
||
478 |
openWithMode:aMode on:aStream |
|
1061 | 479 |
"open the zip-stream on a stream |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
480 |
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
|
481 |
in the ZipStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
482 |
" |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
483 |
^ self streamOpenWithMode:aMode on:aStream. |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
484 |
! |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
485 |
|
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
486 |
streamOpenWithMode:aMode on:aStream |
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
487 |
"open the compression stream on a stream |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
488 |
#readonly uncompress the data derived from the read-stream, aStream |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
489 |
#writeonly compress the data and write to the write-stream, aStream |
1061 | 490 |
" |
1059 | 491 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
492 |
aStream isNil ifTrue:[ |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
493 |
^ self errorNotOpen |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
494 |
]. |
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
495 |
|
1829 | 496 |
onStream := aStream. |
1059 | 497 |
mode := aMode. |
1829 | 498 |
outputBytes := ExternalBytes unprotectedNew:16384. |
499 |
inputBytes := ExternalBytes unprotectedNew:16384. |
|
1059 | 500 |
readLimit := position := 0. |
501 |
binary := false. |
|
502 |
||
503 |
self zopen. |
|
504 |
self registerForFinalization. |
|
505 |
||
506 |
hitEOF := false. |
|
507 |
||
508 |
aMode == #readonly ifTrue:[ |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
509 |
self zinflateInit. |
1059 | 510 |
] ifFalse:[ |
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
511 |
self zdeflateInit |
1059 | 512 |
]. |
513 |
! ! |
|
514 |
||
515 |
!CompressionStream methodsFor:'writing'! |
|
516 |
||
1062 | 517 |
contents:contents |
1193 | 518 |
"write the entire contents to and close the stream" |
519 |
||
520 |
contents do:[:c| self nextPut:c]. |
|
1062 | 521 |
self close. |
522 |
! |
|
523 |
||
1059 | 524 |
flush |
1193 | 525 |
"flush the input and output buffer" |
526 |
||
1829 | 527 |
|continue availOut| |
1059 | 528 |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
529 |
self isWritable ifFalse:[^ self]. |
1059 | 530 |
self zset_avail_in:position. |
531 |
||
532 |
position := 0. |
|
533 |
||
1829 | 534 |
[ continue := self zdeflate. |
535 |
availOut := self zget_avail_out. |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
536 |
|
1829 | 537 |
availOut > 0 ifTrue:[ |
538 |
self onStreamPutBytes:availOut from:outputBytes |
|
1197
c86e858c2893
Documentation. Does not work yet.
Stefan Vogel <sv@exept.de>
parents:
1195
diff
changeset
|
539 |
]. |
1829 | 540 |
|
1827 | 541 |
] doWhile:[ continue == true ]. |
1059 | 542 |
! |
543 |
||
1193 | 544 |
nextPut:aByteOrCharacter |
545 |
"write the argument, aByteOrCharacter" |
|
1059 | 546 |
|
1193 | 547 |
position == inputBytes size ifTrue:[self flush]. |
548 |
position := position + 1. |
|
549 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
1829 | 550 |
! |
551 |
||
552 |
nextPutAll:aCollection |
|
553 |
|limit| |
|
554 |
||
555 |
limit := inputBytes size. |
|
556 |
||
557 |
aCollection do:[:aByteOrCharacter| |
|
558 |
position == limit ifTrue:[self flush]. |
|
559 |
position := position + 1. |
|
560 |
inputBytes at:position put:aByteOrCharacter asInteger. |
|
561 |
]. |
|
562 |
^ aCollection |
|
1193 | 563 |
! ! |
1059 | 564 |
|
1193 | 565 |
!CompressionStream class methodsFor:'documentation'! |
1059 | 566 |
|
1193 | 567 |
version |
2005
a6ef1083aa32
add streamOpenWithMode:on: method to allow to redefine the
ab
parents:
1829
diff
changeset
|
568 |
^ '$Header: /cvs/stx/stx/libbasic2/CompressionStream.st,v 1.22 2008-06-05 09:15:06 ab Exp $' |
1059 | 569 |
! ! |
570 |
||
571 |
CompressionStream initialize! |