CompressionStream.st
changeset 1062 211b3cb6d628
parent 1061 be824dc5e0e7
child 1079 b6e148cf5df4
equal deleted inserted replaced
1061:be824dc5e0e7 1062:211b3cb6d628
   276     "return true if the end of the compressed input stream has been reached
   276     "return true if the end of the compressed input stream has been reached
   277     "
   277     "
   278     ^ hitEOF ~~ false
   278     ^ hitEOF ~~ false
   279 !
   279 !
   280 
   280 
   281 isBinary
   281 canReadWithoutBlocking
   282     "return true, if the stream is in binary (as opposed to text-) mode.
   282     "returns true if data are available for reading;
   283      The default when created is false.
   283      false if the stream is at end.
   284     "
   284      updates the readLimit and position
   285     ^ binary
   285     "
   286 !
   286     mode == #readonly ifFalse:[
   287 
   287         self errorReadOnly
   288 isOpen
   288     ].
   289     "return true, if this stream is open
   289     hitEOF == true ifTrue:[ ^ false ].
   290     "
   290 
   291     ^ onStream notNil
   291     position >= readLimit ifTrue:[
   292 !
       
   293 
       
   294 isReadOpen
       
   295     "return true, if this stream can be read from
       
   296     "
       
   297     ^ mode == #readonly
       
   298 !
       
   299 
       
   300 isWriteOpen
       
   301     "return true, if this stream can be written to
       
   302     "
       
   303     ^ mode == #writeonly
       
   304 ! !
       
   305 
       
   306 !CompressionStream methodsFor:'reading'!
       
   307 
       
   308 contents
       
   309     "return the entire contents of the stream
       
   310     "
       
   311     |out b|
       
   312 
       
   313     mode == #readonly ifFalse:[ self errorReadOnly ].
       
   314 
       
   315     out := (String new:1024) writeStream.
       
   316 
       
   317     [ (b := self next) notNil ] whileTrue:[
       
   318         out nextPut:b
       
   319     ].
       
   320     ^ out contents
       
   321 !
       
   322 
       
   323 next
       
   324     "return the next element, a character or byte (textmode)
       
   325      if there is  more element, nil is returned
       
   326     "
       
   327     |byte|
       
   328 
       
   329     mode == #readonly ifFalse:[ self errorReadOnly ].
       
   330 
       
   331     hitEOF == true ifTrue:[^ nil].
       
   332 
       
   333     position == readLimit ifTrue:[
       
   334         [ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
   292         [ (readLimit := self zinflate) == 0 ] whileTrue:[ |n|
   335             n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
   293             n := onStream nextBytes:(inputBytes size) into:inputBytes startingAt:1.
   336 
   294 
   337             (n notNil and:[n > 0]) ifFalse:[
   295             (n notNil and:[n > 0]) ifFalse:[
   338                 self pastEnd
   296                 self pastEnd
   339             ].
   297             ].
   340             self zset_avail_in:n.
   298             self zset_avail_in:n.
   341         ].
   299         ].
   342         readLimit ifNil:[
   300         readLimit ifNil:[
   343             hitEOF := true.
   301             hitEOF := true.
   344           ^ nil
   302           ^ false
   345         ].
   303         ].
   346         position := 0.
   304         position := 0.
       
   305     ].
       
   306     ^ true
       
   307 !
       
   308 
       
   309 isBinary
       
   310     "return true, if the stream is in binary (as opposed to text-) mode.
       
   311      The default when created is false.
       
   312     "
       
   313     ^ binary
       
   314 !
       
   315 
       
   316 isOpen
       
   317     "return true, if this stream is open
       
   318     "
       
   319     ^ onStream notNil
       
   320 !
       
   321 
       
   322 isReadOpen
       
   323     "return true, if this stream can be read from
       
   324     "
       
   325     ^ mode == #readonly
       
   326 !
       
   327 
       
   328 isWriteOpen
       
   329     "return true, if this stream can be written to
       
   330     "
       
   331     ^ mode == #writeonly
       
   332 ! !
       
   333 
       
   334 !CompressionStream methodsFor:'reading'!
       
   335 
       
   336 contents
       
   337     "return the entire contents of the stream;
       
   338      after reading the stream is closed.
       
   339     "
       
   340     |stream bfsize|
       
   341 
       
   342     mode == #readonly ifFalse:[ self errorReadOnly ].
       
   343 
       
   344     binary ifTrue:[ stream := ByteArray new:1024 ]
       
   345           ifFalse:[ stream := String    new:1024 ].
       
   346 
       
   347     stream := stream writeStream.
       
   348     bfsize := outputBytes size.
       
   349 
       
   350     [ self canReadWithoutBlocking ] whileTrue:[ |data|
       
   351         readLimit == bfsize ifTrue:[
       
   352             data := outputBytes.
       
   353         ] ifFalse:[
       
   354             data := outputBytes copyFrom:1 to:readLimit.
       
   355         ].
       
   356         binary ifFalse:[
       
   357             data := data asString
       
   358         ].
       
   359         stream nextPutAll:data.
       
   360         position := readLimit := 0.     "reset input data"
       
   361     ].
       
   362     self close.
       
   363   ^ stream contents
       
   364 !
       
   365 
       
   366 next
       
   367     "return the next element, a character or byte (textmode)
       
   368      if there is  more element, nil is returned
       
   369     "
       
   370     |byte|
       
   371 
       
   372     self canReadWithoutBlocking ifFalse:[
       
   373         "there is no more element; the stream is at end"
       
   374         ^ nil
   347     ].
   375     ].
   348     position := position + 1.
   376     position := position + 1.
   349     byte := outputBytes at:position.
   377     byte := outputBytes at:position.
   350 
   378 
   351     binary ifTrue:[^ byte ].
   379     binary ifTrue:[^ byte ].
   389         self zdeflateInit
   417         self zdeflateInit
   390     ].
   418     ].
   391 ! !
   419 ! !
   392 
   420 
   393 !CompressionStream methodsFor:'writing'!
   421 !CompressionStream methodsFor:'writing'!
       
   422 
       
   423 contents:contents
       
   424     "write the entire contents to the stream;
       
   425      after writing the stream is closed.
       
   426     "
       
   427     contents do:[:c| self nextPut:c ].
       
   428     self close.
       
   429 !
   394 
   430 
   395 flush
   431 flush
   396     "flush the input and output buffer
   432     "flush the input and output buffer
   397     "
   433     "
   398     |continue|
   434     |continue|
   444         byte = __MKSMALLINT( _bval );
   480         byte = __MKSMALLINT( _bval );
   445     }
   481     }
   446 bad: ;
   482 bad: ;
   447 %}.
   483 %}.
   448     byte ifNil:[
   484     byte ifNil:[
   449         mode == #readonly ifTrue:[self errorReadOnly].
   485         mode ~~ #writeonly ifTrue:[
   450         zstream ifNil:[ self errorNotOpen ].
   486             zstream ifNil:[self errorNotOpen].
       
   487             self errorWriteOnly.
       
   488         ].
   451         self invalidArguments.
   489         self invalidArguments.
   452     ].
   490     ].
   453     position == inputBytes size ifTrue:[ self flush ].
   491     position == inputBytes size ifTrue:[ self flush ].
   454     position := position + 1.
   492     position := position + 1.
   455     inputBytes at:position put:byte.
   493     inputBytes at:position put:byte.