ObjectCoder.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Nov 2002 11:54:33 +0100
changeset 6896 ebfdcf4e35e5
parent 6705 490a44b0890d
child 7257 b9f0fb923c72
permissions -rw-r--r--
utilities

"
 COPYRIGHT (c) 2000 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' }"

AbstractObjectCoder subclass:#ObjectCoder
	instanceVariableNames:'stream'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Storage'
!

!ObjectCoder class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2000 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.
"


!

documentation
"
    This is an abstract class. Subclasses implement encoding and decoding
    of Objects onto resp. from a stream. Possible coders are ASN.1/BER,
    CORBA/CDR, BOSS, RMI ...

    Classes which implement basic types (Boolean, Integer, Float, ...)
    implement #encodeOn:with:, which dispatches onto an ObjectCoder.

    [author:]
         Stefan Vogel

    [see also:]
        OSI::ASN1_Coder

    [instance variables:]
        stream  <Stream>        the stream we read/write the encodings from/to

    [class variables:]
"
! !

!ObjectCoder class methodsFor:'instance creation'!

on:aStream
    "return an encoder/decoder for a stream"

    ^ self new stream:aStream


! !

!ObjectCoder class methodsFor:'encoding'!

decodingOf:anObject
    "return the decoding of anObject"

    ^ (self on:anObject readStream) upToEnd

    "
     Base64Coder encodingOf:#[1 2 16rFe 16rFF]
     Base64Coder decodingOf:'AQL+/w=='    
    "
!

encode:anObject on:aStream

    |coder|

    coder := self new stream:aStream.
    anObject encodeOn:coder with:nil.
!

encode:anObject on:aStream with:info

    |coder|

    coder := self new stream:aStream.
    anObject encodeOn:coder with:info.
!

encodingOf:anObject
    "return the encoding of anObject"

    ^ self new encodingOf:anObject with:nil

    "
     Base64Coder encodingOf:#[1 2 16rFe 16rFF]
    "
!

encodingOf:anObject with:aParameter
    "return the encoding of anObject"

    ^ self new encodingOf:anObject with:aParameter
! !

!ObjectCoder methodsFor:'accessing'!

contents

    self flush.
    ^ stream contents
! !

!ObjectCoder methodsFor:'decoding'!

next
    "read, decode and return the next object"

    ^ self subclassResponsibility


! !

!ObjectCoder methodsFor:'encoding'!

encodingOf:anObject
    "answer the encoded argument anObject"

    ^ self encodingOf:anObject with:nil


!

encodingOf:anObject with:aParameter
    "answer the encoded argument anObject"

    stream isNil ifTrue:[
        stream := self emptyWriteStream.
    ] ifFalse:[
        stream reset.
    ].

    anObject encodeOn:self with:aParameter.
    ^ stream contents.
!

nextPut:anObject
    "encode anObject onto my stream"

    anObject encodeOn:self with:nil.

!

nextPutAll:aCollectionOfObjects
    "encode all objects from the argument"

    aCollectionOfObjects do:[:o |
        self nextPut:o
    ]

! !

!ObjectCoder methodsFor:'initialization'!

close
    "close the underlying stream"

    stream notNil ifTrue:[
        stream close.
    ].


!

emptyWriteStream
    "answer an empty stream for writing the encoded object"

    ^ self subclassResponsibility
!

flush
    "flush possibly internally buffered data.
     Nothing is done by default. Subclasses may redefine this"

    ^ self
!

reset
    "reset the coder"

    stream notNil ifTrue:[
        stream reset.
    ].


! !

!ObjectCoder methodsFor:'private - accessing'!

stream:aStream
    stream := aStream.
    stream signalAtEnd:true.



! !

!ObjectCoder class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/ObjectCoder.st,v 1.10 2002-11-26 10:54:33 cg Exp $'
! !