File>>atEnd question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

File>>atEnd question

John Small
The File>>atEnd method works nicely if you are reading
one element at a time, i.e.


    File>>atEnd
        "Answer a Boolean indicating whether we are at the file's end
        or signal a FileException if the operation fails."
        ^self position = (self size - 1)

But doesn't work if you are reading a block of elements, e.g.

    inputFile read: buffer count: buffer size.

What if  File>>atEnd were defined instead as:

    atEnd
        ^self position >= (self size - 1)

would that break any other logic?

John


Reply | Threaded
Open this post in threaded view
|

Re: File>>atEnd question

Ian Bartholomew-17
John,

I very rarely use File as FileStream is, IMHO, much more user friendly but
looking at the code it would seem that

 atEnd
    ^self position = self size

might be more logical as the largest number that #position can possibly
answer is equal to the size of the file.

> The File>>atEnd method works nicely if you are reading
> one element at a time, i.e.

It doesn't appear to for me?. Using the current implementation of #atEnd
(ignoring the quality of the coding in this example)

oc1 := OrderedCollection new.
f := File open: 'c:\x.txt'.
b := ByteArray new: 1.
[f atEnd] whileFalse: [
    f read: b count: 1.
    oc1 add: (b at: 1)].
f close.
oc1 size

answers 1 below the expected value. The equivalent using FileStream

oc2 := OrderedCollection new.
f := FileStream read: 'c:\x.txt' text: false.
[f atEnd] whileFalse: [oc2 add: f next].
f close.
oc2 size

answers the expect value.

Regards
    Ian


Reply | Threaded
Open this post in threaded view
|

Re: File>>atEnd question

John Small
"Ian Bartholomew" <[hidden email]> wrote in message
news:8eJW8.16392$VP6.1075403@stones...
> John,
>
> I very rarely use File as FileStream is, IMHO, much more user friendly but
> looking at the code it would seem that
>
>  atEnd
>     ^self position = self size
>

Thanks Ian.   The only reason I'm using File instead of FileStream
is because I'm building a filter to deal with hundred megabyte files
and I'm reading in chunks for speed.

John


Reply | Threaded
Open this post in threaded view
|

Re: File>>atEnd question

Chris Uppal-3
John Small wrote:

> The only reason I'm using File instead of FileStream
> is because I'm building a filter to deal with hundred megabyte files
> and I'm reading in chunks for speed.

If you haven't already, then you might want to check that it is actually
faster than pulling similar-sized chunks out of a FileStream.  It obviously
depends on your app. but the last time I thought I had occasion to use File
directly, it turned out not to be making any useful difference.

    -- chris