terminals/Xtreams__FileReadStream.st
author mkobetic
Mon, 21 Nov 2011 06:44:00 +0000
changeset 25 02e7c3b6f63c
parent 9 6c90659cf105
child 39 80fdc4602b14
permissions -rw-r--r--
added XtreamsPool to fix DefaultBufferSize; set proper category names

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

"{ NameSpace: Xtreams }"

ExternalReadStream subclass:#FileReadStream
	instanceVariableNames:'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.
! !

!FileReadStream methodsFor:'seeking'!

++ anInteger
	| count |
	anInteger < 0 ifTrue: [ ^self -- anInteger negated ].
	count := self available min: anInteger.
	position := position + count.
	source seekTo: position.
	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.
	cacheDataSize := cachePosition := 0.
	anInteger = count ifTrue: [^anInteger].
	(Incomplete count: count) raise
!

length
	^source fileSize
!

position
	^position
!

position: aPosition

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

!FileReadStream methodsFor:'testing'!

isPositionable
	^true
! !

!FileReadStream class methodsFor:'documentation'!

version_SVN
    ^ '$Id$'
! !