transforms/Xtreams__ObjectReadStream.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 01 Feb 2012 00:41:14 +0000
changeset 99 677c81c943e4
parent 72 d16c7d84d4a8
child 101 07512723d137
permissions -rw-r--r--
build files regenerated

"{ Package: 'stx:goodies/xtreams/transforms' }"

"{ NameSpace: Xtreams }"

ReadStream subclass:#ObjectReadStream
	instanceVariableNames:'marshaler objects float double nothing uint16 uint32 uint64 int64
		int8 int16 int32'
	classVariableNames:''
	poolDictionaries:''
	category:'Xtreams-Transforms'
!

ObjectReadStream comment:'Unmarshals objects from binary source, previously marshaled using the same ObjectMarshaler. The bytes must start with marshaler version ID.

Instance Variables
	marshaler	<ObjectMarshaler> performs marshaling of object bodies
	objects	<Array> retains unmarshaled objects for resolution of backward references (to preserve identity)
	longs	<InterpretedReadStream> (internal) for reading of longs
	longlongs	<InterpretedReadStream> (internal) for reading of longlongs
	floats	<InterpretedReadStream> (internal) for reading of floats
	doubles	<InterpretedReadStream> (internal) for reading of doubles
	nothing	<Object> (internal) an object used to occupy empty slots in objects (since nil is potentially valid content)

'
!


!ObjectReadStream class methodsFor:'instance creation'!

on: aDestination
	^self on: aDestination marshaler: ObjectMarshaler new
!

on: aDestination marshaler: aMarshaler
	^self new on: aDestination marshaler: aMarshaler
! !

!ObjectReadStream methodsFor:'accessing'!

get
	| object |
	object := marshaler unmarshal: self.
	self complete.
	^object
!

read: anInteger into: aSequenceableCollection at: startIndex
	| count |
	count := 0.
	[anInteger timesRepeat:
		[aSequenceableCollection at: startIndex + count put: (marshaler unmarshal: self).
		self complete.
		count := count + 1]]
			on: Incomplete do: [:exception |
				self complete.
				(Incomplete on: aSequenceableCollection count: count at: startIndex) raise].
	^anInteger
! !

!ObjectReadStream methodsFor:'initialize-release'!

contentsSpecies
	^Array
!

on: aSource marshaler: aMarshaler
	super on: aSource.
	marshaler := aMarshaler.
	objects := Array with: false with: Transcript.
	nothing := Object new.

	int8 := aSource interpreting: #signedChar.
	(marshaler configureUnmarshal: aSource)
		ifTrue: [
			int16 := aSource interpreting: #signedShort_be.
			int32 := aSource interpreting: #signedLong_be.
			int64 := aSource interpreting: #signedLonglong_be.
			uint16 := aSource interpreting: #unsignedShort_be.
			uint32 := aSource interpreting: #unsignedLong_be.
			uint64 := aSource interpreting: #unsignedLonglong_be]
		ifFalse: [
			int16 := aSource interpreting: #signedShort_le.
			int32 := aSource interpreting: #signedLong_le.
			int64 := aSource interpreting: #signedLonglong_le.
			uint16 := aSource interpreting: #unsignedShort_le.
			uint32 := aSource interpreting: #unsignedLong_le.
			uint64 := aSource interpreting: #unsignedLonglong_le].
	float := aSource interpreting: #float.
	double := aSource interpreting: #double
! !

!ObjectReadStream methodsFor:'primitives'!

double
	^double
!

float
	^float
!

int16
	^int16
!

int32
	^int32
!

int64
	^int64
!

int8
	^int8
!

uint16
	^uint16
!

uint32
	^uint32
!

uint64
	^uint64
!

uint8
	^source
! !

!ObjectReadStream methodsFor:'private'!

complete
	1 to: objects size by: 2 do: [:index |
		objects at: index put: false.
		objects at: index + 1 put: nothing].
	objects at: 2 put: Transcript
!

grow: index
	| replacement |
	(replacement := Array new: index)
		replaceFrom: 1 to: objects size with: objects startingAt: 1.
	objects size + 1 to: index by: 2 do: [:i |
		replacement
			at: i put: false;
			at: i + 1 put: nothing].
	objects := replacement
!

nothing
	^nothing
!

objects
	^objects
! !

!ObjectReadStream class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !