terminals/Xtreams__FileReadStream.st
author mkobetic
Sun, 15 Jan 2012 22:50:34 +0000
changeset 52 a1363827b596
parent 43 b9a077d6ce14
child 78 a9dd8b69b39f
permissions -rw-r--r--
packaging

'From Smalltalk/X, Version:6.2.1 on 15-01-2012 at 05:49:32 PM'                  !

"{ 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.
!

readBytes:count into:aByteBuffer startingAt:firstIndex

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

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