substreams/Xtreams__LimitReadSubstream.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 01 Feb 2012 00:56:17 +0000
changeset 103 726bf2ca0b99
parent 67 a87e5ce04545
child 109 9587e2df7029
permissions -rw-r--r--
Removed methods from FileHandle to make it compilable. Having methods there is not a good idea anyway.

"{ 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_SVN
    ^ '$Id$'
! !