substreams/Xtreams__LimitReadSubstream.st
author joe
Fri, 15 Mar 2013 19:54:41 -0400
changeset 113 c0df9d2ad5d3
parent 109 9587e2df7029
permissions -rw-r--r--
* make InifiniteReadingWritingTests abstract

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

"{ NameSpace: Xtreams }"

PositionReadSubstream subclass:#LimitReadSubstream
	instanceVariableNames:'limit'
	classVariableNames:''
	poolDictionaries:''
	category:'Xtreams-Substreams'
!

LimitReadSubstream comment:'Limits the number of elements that can be read from the source stream.

Instance Variables
	limit   <Integer> maximum number of elements that can be read from this stream
	position        <Integer> number of elements already read from the stream

'
!


!LimitReadSubstream class methodsFor:'instance creation'!

on: aSource limit: anInteger
	^self new on: aSource limit: anInteger
! !

!LimitReadSubstream methodsFor:'accessing'!

get
	position = limit ifTrue: [Incomplete zero raise].
	^super get
!

read: anInteger into: aSequenceableCollection at: startIndex
	| count |
	count := anInteger min: self available.
	super read: count into: aSequenceableCollection at: startIndex.
	count < anInteger ifTrue: [(Incomplete on: aSequenceableCollection count: count at: startIndex) raise].
	^anInteger
! !

!LimitReadSubstream methodsFor:'initialize-release'!

on: aSource limit: anInteger
	self on: aSource.
	limit := anInteger
!

subseekend
	self isPositionable ifFalse: [^self].
	self -= 0
! !

!LimitReadSubstream methodsFor:'seeking'!

-= anInteger
	| available |
	anInteger < 0 ifTrue: [Incomplete zero raise].
	available := anInteger min: limit.
	source ++ (limit - position - available).
	position := limit - available.
	available = anInteger ifTrue: [^anInteger].
	^(Incomplete count: available) raise
!

length
	^limit
! !

!LimitReadSubstream class methodsFor:'documentation'!

version_HG

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