terminals/Xtreams__BufferWriteStream.st
author Martin Kobetic
Sun, 17 Nov 2013 00:23:18 -0500
changeset 147 bd6be28aa924
parent 109 9587e2df7029
permissions -rw-r--r--
merging

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

"{ NameSpace: Xtreams }"

WriteStream subclass:#BufferWriteStream
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Xtreams-Terminals'
!

BufferWriteStream comment:'Write stream on a Buffer. Usually used in tandem with a read stream to access buffer contents conveniently.
{{{
	buffer := ElasticBuffer on: String new.
	bufferIn := buffer writing.
	bufferOut := buffer reading.
	100000 timesRepeat: [ bufferIn write: ''Hello World''. bufferOut read: 11 ].
	buffer cacheSize
}}}
'
!


!BufferWriteStream methodsFor:'accessing'!

insert: anInteger from: aSequenceableCollection at: startIndex
	^destination insert: anInteger from: aSequenceableCollection at: startIndex
!

put: anObject
	^destination put: anObject
!

write: anInteger from: aSequenceableCollection at: startIndex
	^destination write: anInteger from: aSequenceableCollection at: startIndex
! !

!BufferWriteStream methodsFor:'initialize-release'!

close
	destination close
!

contentsSpecies
	^destination contentsSpecies
!

flush
! !

!BufferWriteStream methodsFor:'seeking'!

++ anInteger
	| count |
	anInteger < 0 ifTrue: [ ^self -- anInteger negated ].
	count := destination writeSkip: anInteger.
	count < anInteger ifTrue: [(Incomplete count: count) raise].
	^anInteger
!

-- anInteger
	| count |
	anInteger < 0 ifTrue: [ ^self ++ anInteger negated ].
	count := (destination writeSkip: anInteger negated) negated.
	count = anInteger ifTrue: [^anInteger].
	(Incomplete count: count) raise
!

available
	^destination writeSize
!

length
	^destination activeSize
!

position
	^destination writePosition
!

position: aPosition
	| available |
	aPosition < 0 ifTrue: [ Incomplete zero raise ].
	available := aPosition min: destination activeSize.
	destination writePosition: available.
	available = aPosition ifTrue: [^aPosition ].
	(Incomplete count: available) raise
!

skip: anInteger
	| count |
	count := destination writeSkip: anInteger.
	count ~= anInteger ifTrue: [(Incomplete count: count) raise]
! !

!BufferWriteStream methodsFor:'testing'!

isPositionable
	^true
! !

!BufferWriteStream class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !