ObjectCoder.st
author Claus Gittinger <cg@exept.de>
Sat, 17 Nov 2001 11:10:08 +0100
changeset 6211 69f320c220d5
parent 5617 465796ebb40c
child 6380 457773bd81f9
permissions -rw-r--r--
initial checkin

"
 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' }"

Object 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'!

encodingOf:anObject
    "return the encoding of anObject"

    ^ self new encodingOf:anObject with:nil
!

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

    ^ self new encodingOf:anObject with:aParameter
! !

!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 := WriteStream on:(ByteArray new:20).
    ] 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:'encoding-smalltalk-types'!

encodeAbsoluteTime:anAbsoluteTime with:aParameter
    "encode an absolute time value"

    self encodeObject:anAbsoluteTime with:aParameter


!

encodeBitArray:aBitArray with:aParameter
    "encode a BitArray"

    self encodeObject:aBitArray with:aParameter


!

encodeBoolean:aBoolean with:aParameter
    "encode a Boolean"

    self encodeObject:aBoolean with:aParameter


!

encodeByteArray:aByteArray with:aParameter
    "encode an absolute time value"

    self encodeObject:aByteArray with:aParameter


!

encodeCharacter:aCharacter with:aParameter
    "encode a Character"

    self encodeObject:aCharacter with:aParameter


!

encodeDictionary:aDictionary with:aParameter
    "encode a Dictionary"

    self encodeObject:aDictionary with:aParameter


!

encodeFloat:aFloat with:aParameter
    "encode a Float"

    self encodeObject:aFloat with:aParameter


!

encodeInteger:anInteger with:aParameter
    "encode an Integer"

    self encodeObject:anInteger with:aParameter


!

encodeNilWith:aParameter
    "encode a nil"

    self encodeObject:nil with:aParameter


!

encodeObject:anObject with:aParameter
    "encode an object. Subclasses must implement this method"

    self subclassResponsibility


!

encodeSequencableCollection:aCollection with:aParameter
    "encode a SequencableCollection"

    self encodeObject:aCollection with:aParameter


!

encodeSet:aCollection with:aParameter
    "encode a Set"

    self encodeObject:aCollection with:aParameter


!

encodeString:aString with:aParameter
    "encode a String"

    self encodeObject:aString with:aParameter


!

encodeSymbol:aSymbol with:aParameter
    "encode a Symbol"

    self encodeObject:aSymbol with:aParameter


! !

!ObjectCoder methodsFor:'initialization'!

close
    "close the underlying stream"

    stream notNil ifTrue:[
        stream close.
    ].


!

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.4 2000-09-20 08:32:51 stefan Exp $'
! !