terminals/Xtreams__FileReadStream.st
author Martin Kobetic
Sun, 17 Nov 2013 00:22:31 -0500
changeset 144 e193a6772be4
parent 109 9587e2df7029
permissions -rw-r--r--
merging

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

"{ NameSpace: Xtreams }"

ExternalReadStream subclass:#FileReadStream
	instanceVariableNames:'filename position'
	classVariableNames:''
	poolDictionaries:''
	category:'Xtreams-Terminals'
!

FileReadStream comment:'Reads from a file. The stream is usually created by sending #reading to a Filename. However the actual terminal is an IOAccessor. The original filename is accessible through the IOAccessor. The stream is binary and is naturally positionable.
{{{
	| file |
	file := ObjectMemory imageName asFilename reading.
	[ file read: 13 ] ensure: [ file close ]
}}}
As a convenience, sending #reading to a Filename of a directory will create a stream of all filenames in that directory.
{{{
	''/tmp'' asFilename reading rest
}}}

Instance Variables
	position        <Integer> current position of the stream

'
!


!FileReadStream methodsFor:'accessing'!

get

	| object |
	object := super get.
	position := position + 1.
	^object
!

read: anInteger into: aSequenceableCollection at: startIndex

	super read: anInteger into: aSequenceableCollection at: startIndex.
	position := position + anInteger.
	^anInteger
! !

!FileReadStream methodsFor:'initialize-release'!

on: anAccessor
	super on: anAccessor.
	position := 0
! !

!FileReadStream methodsFor:'private'!

bytesForRead

	"Ideally we'd ask for the fileSize and subtract that from our position, but calling #fileSize is a slow primitive"
	^cache size
!

incompleteRead: incomplete

	position := position + incomplete count.
	super incompleteRead: incomplete.
!

readBytes:count into:aByteBuffer startingAt:firstIndex

    ^source readBytes:count into:aByteBuffer startingAt:firstIndex
!

setFilename: fn

    filename := fn
! !

!FileReadStream methodsFor:'seeking'!

++ anInteger
	| count |
	anInteger < 0 ifTrue: [ ^self -- anInteger negated ].
	count := self available min: anInteger.
	position := position + count.
	source seekTo: position from: #begin.
	cacheDataSize := cachePosition := 0.
	count < anInteger ifTrue: [(Incomplete count: count) raise].
	^anInteger
!

-- anInteger
	| count |
	anInteger < 0 ifTrue: [ ^self ++ anInteger negated ].
	count := position min: anInteger.
	position := position - count.
	source seekTo: position from: #begin.
	cacheDataSize := cachePosition := 0.
	anInteger = count ifTrue: [^anInteger].
	(Incomplete count: count) raise
!

length
	^filename fileSize
!

position
	^position
!

position: aPosition

	| available |
	aPosition < 0 ifTrue: [ Incomplete zero raise ].
	available := aPosition min: self length.
	source seekTo: available from: #begin.
	position := available.
	cacheDataSize := cachePosition := 0.
	available = aPosition ifTrue: [^aPosition].
	(Incomplete count: available) raise
! !

!FileReadStream methodsFor:'testing'!

isPositionable
	^true
! !

!FileReadStream class methodsFor:'documentation'!

version_HG

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