EncodedStream.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 12 Jul 2010 17:20:36 +0100
branchjv
changeset 17780 b6e42c92eba0
parent 17772 6e0b3c06c364
child 17807 06cc6c49e291
permissions -rw-r--r--
Merged with /trunk

"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

PeekableStream subclass:#EncodedStream
	instanceVariableNames:'encoder stream'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Text-Encodings'
!

!EncodedStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2004 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!EncodedStream class methodsFor:'instance creation'!

stream:streamArg encoder:encoder
    ^ (self basicNew) stream:streamArg; encoder:encoder
! !

!EncodedStream class methodsFor:'utilities'!

decodedStreamFor:aStream
    |encoding decoder decodedStream|

    "/ setup for no-encoding; allows for a later switch to a real encoder,
    "/ whenever an encoding pragma is encountered later.
    decoder := CharacterEncoder nullEncoderInstance.

    aStream isPositionable ifTrue:[
        encoding := CharacterEncoder guessEncodingOfStream:aStream.
        (encoding notNil
        and:[ encoding ~= #'iso8859-1' 
        and:[ encoding ~= #'ascii' ]]) ifTrue:[
            decoder := CharacterEncoder encoderFor:encoding.
        ].
    ].
    decodedStream := EncodedStream stream:aStream encoder:decoder.
    decodedStream skipEncodingChunk.
    ^ decodedStream
! !

!EncodedStream methodsFor:'accessing'!

contentsSpecies

    "Not sure if this is ok"

    ^ stream contentsSpecies.

"/    ^UnicodeString

    "Created: / 14-06-2005 / 17:11:01 / janfrog"
!

encoder
    ^ encoder
!

encoder:something
    encoder := something.
!

pathName
    "if our base stream hat a pathname, delegate..."

    stream isNil ifTrue:[
        ^ nil.
    ].
    ^ stream pathName.
!

readStream
    ^ self
!

stream
    ^ stream
!

stream:something
    stream := something.
! !

!EncodedStream methodsFor:'stream protocol'!

atEnd
    ^ stream atEnd
!

close
    stream close
!

contents

    ^String streamContents:
        [:s|
        [ stream atEnd ] whileFalse:
            [s nextPut: stream next]
        ]

    "Created: / 25-02-2010 / 23:34:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

cr
    self nextPutAll:(Character cr asString)
!

emphasis:anObject

    stream emphasis:anObject

    "Created: / 15-06-2005 / 11:16:33 / janfrog"
!

isOpen
    ^ stream notNil and:[stream isOpen]
!

next

    ^encoder readNextCharacterFrom:stream

    "Created: / 14-06-2005 / 17:01:39 / janfrog"
!

next:charactersToRead

    ^encoder readNext:charactersToRead charactersFrom:stream

    "Created: / 16-06-2005 / 11:43:43 / masca"
!

nextChunk
    |chunk|

    chunk := stream nextChunk.
    chunk isNil ifTrue:[^ chunk].
    ^ encoder decodeString:chunk
!

nextChunkPut:chunk
    stream nextChunkPut:(encoder encodeString:chunk)
!

nextPut:aCharacter
    self nextPutAll:(aCharacter asString).
!

nextPutAll:aCollection
    stream nextPutAll:(encoder encodeString:aCollection).
!

peek

    ^stream peek

    "Created: / 20-06-2005 / 10:13:03 / masca"
    "Modified: / 20-06-2005 / 13:06:14 / masca"
!

peekFor:aCharacter
    ^ stream peekFor:aCharacter
!

position
    ^ stream position
!

position0Based
    "to be obsoleted - use position"

    ^ stream position0Based
!

position0Based:newPosition
    "to be obsoleted - use position"

    stream position0Based:newPosition
!

position1Based
    "to be obsoleted - use position"

    ^ stream position1Based
!

position1Based:newPosition
    "to be obsoleted - use position"

    stream position1Based:newPosition
!

position:newPosition
    stream position:newPosition
!

reset
    stream reset

    "Created: / 25-02-2010 / 23:37:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

setToEnd
    stream setToEnd
!

skip: anInteger

    "/ Should skip on character basis, not on bytes. This works for XML reader
    ^stream skip: anInteger

    "Created: / 20-06-2005 / 13:06:06 / masca"
!

skipSeparators
    ^ stream skipSeparators
! !

!EncodedStream methodsFor:'utilities'!

skipEncodingChunk
    |pos chunk tree|

    pos := self position.
    chunk := self nextChunk.
    tree := (Parser parseExpression:chunk).
    "/ if this is a valid chunk (i.e. not a comment or encoding-directive),
    "/ then position back, so it will be processed as usual.
    tree notNil ifTrue:[
        self position:pos
    ]
! !

!EncodedStream class methodsFor:'documentation'!

version
    ^ '$Id: EncodedStream.st 10544 2010-07-12 16:20:36Z vranyj1 $'
!

version_CVS
    ^ 'Header: /cvs/stx/stx/libbasic/EncodedStream.st,v 1.18 2010/02/04 16:11:51 stefan Exp '
!

version_SVN
    ^ '$Id: EncodedStream.st 10544 2010-07-12 16:20:36Z vranyj1 $'
! !