Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
ReadWriteStream
Last updated at 6:22 pm UTC on 2 May 2018
 ReadWriteStream subclasses  
 {RWBinaryOrTextStream . FileStream . Transcripter . CompressedSourceStream . MultiByteBinaryOrTextStream}


Notes 2018 / 2013


Mailing list discussion – NEED for summary...

Chris Muller
Tue, May 1, 2018 at 10:00 PM
Reply-To: The general-purpose Squeak developers list
To: squeak dev

Does anyone know why ReadWriteStream overrides #contents from WriteStream?

WriteStream behaves as I would expect

   |stream| stream := WriteStream on: String new.
   stream nextPutAll: 'chris'; reset; nextPutAll: 'C'; contents     "—>  'C'   as expected"

but ReadWriteStream doesn't...

   |stream| stream := ReadWriteStream on: String new.
   stream nextPutAll: 'chris'; reset; nextPutAll: 'C'; contents     "—> 'Chris'   unexpected!"

I want to reuse a ReadWriteStream, so I want #contents to honor the end position. What's going on here?

Chris Muller
Wed, May 2, 2018 at 2:20 AM
Reply-To: ma.chris.m@gmail.com, The general-purpose Squeak developers list
To: The general-purpose Squeak developers list

It seems I've asked this question before:

http://forum.world.st/surprising-behavior-from-MultiByteBinaryOrTextStream-gt-gt-contents-td4706322.html

This made me remember some of the really great points and discussion
about the Stream design you've made in the past. This, for example:

http://forum.world.st/In-memory-FileSystem-write-streams-not-being-polymorphic-td4721006i20.html

Good to re-read that thread from time to time..


Nicolas Cellier
Wed, May 2, 2018 at 2:14 PM
Reply-To: The general-purpose Squeak developers list
To: Chris Muller , The general-purpose Squeak developers list

Hi Chris,
Yes, you are right, ReadWriteStream looks simple. But it is not.
It inherits from WriteStream which is a Stream that cannot read (self shouldNotImplement).
WriteStream inherits itself from PositionableStream which can neither read nor write (self subclassResponsibility).
PositionableStream is rather read-oriented though, see inst.var. readLimit and its usage which is clearly for delimiting read limit (thus the name).

WriteStream reuse readLimit with a slightly different purpose: indicate the right-most position written so far.
The purpose is to answer the whole contents even of position has been moved backward (rather than truncate to current position).
Hence, the natural invariant should be this one:
the readLimit should be incremented when writing pastEnd (past the right-most position written so far - readLimit).
But it is not... Indeed, primitive: 66 (nextPut:) increments position and ignores the readLimit, and just consider the writeLimit:
    rw := ReadWriteStream on: (String new: 10).
    rw nextPut: $a.
    rw instVarNamed: #readLimit
PastEnd is interpreted as the physical limit - writeLimit - so as to have efficient primitives as long as there is room in the target collection.

That doesn't matter, because the position is shared for reading and writing operations.
Thanks to this property, we can to enforce the invariant differently:
hack every place where we might assign a position backward with a:
    readLimit := readLimit max: position.

The hack is both clever and fragile...
It's a nice hack, because as you observed, there are not so many methods requiring an overwrite...
But it's fragile, because it means necessary chirurgical operations in subclasses to maintain the invariant.
And if ever you want to subclass with a Stream maintaining two separate positions for read and write, boom!
Since this invariant isn't documented anywhere, probably because you just have to read the code :(
our best way to learn it will be pain: probably a feedback loop valued by the biological analogy ;)

And heavy chirurgy is what happens in the subclasses zoo which are further hacked...
The inst. var. position is no more the absolute position in underlying collection, but rather a relative position in some buffer/segment, both in case of StandardFileStream and CompressedSourceStream.
So these classes also modify the meaning of readLimit inst. var. to be the number of bytes readable in the buffer.
And they need yet another way to access the absolute readLimit (endOfFile for CompressedSourceStream, and OS fseek-ftell-based for StandardFileStream).

Now, if you are about to modify one of these classes, and try and track usage of position/readLimit inst. var. you are in brain trouble.
Remember that position and self position might be different things...

So you are somehow right, ReadWriteStream is not at the level of awfullness I described, but it carries the seeds for this awfullness to be further developped.
Reusing inst. var. with a different intention across hierarchy is a good recipe for brain storm (a bug factory and a limitation to extensibility).
Most of the time, we don't need interleaved read/writes, except for a database backend or a few other stateful cases.
Instead, we mostly write then read. I already replaced a few instances of ReadWriteStream on this YAGNI principle, and would like this work to be continued, thus my simplistic reaction.
Of course, your case might differ.

Nicolas

Chris Muller
Wed, May 2, 2018 at 7:59 PM
Reply-To: The general-purpose Squeak developers list
To: Nicolas Cellier
Cc: The general-purpose Squeak developers list

Thanks for that fantastic explanation, Nicolas. If I were to try to distill your point, it's that each class in Squeak's legacy Stream hierarchy has its own view on how ITS concrete instances interpret the meaning of the API. And while, thankfully, they're mostly consistent with #next, #peek, and #nextPut:, they're not at all on other messages like #contents. The result of this is that the classes are mostly silo'ed from each other because it's complex to try to start sharing the behaviors. That was my take-away, anyway, please let me know if I missed it.

One thing I'm not sure about is whether you feel commingling read ability with write ability in the same object is a bad thing in an abstract sense? Or, it just didn't get implemented via a well-planned start in Squeak's case (organic growth), and so it's furthering dependencies on something that is hacked together and hard to change.

You mentioned the database exception, and sometimes it's hard for me to think outside of that context, so ReadWriteStream feels like a natural encapsulation of behaviors for accessing and updating file contents. If there's any inherent advantage to splitting the core streaming capabilities (reading, writing and positioning) into separate classes, or whether all those should be the API of all streams (but some may have to delete positioning via #shouldNotImplement). That would leave the class-hierarchy abstractions to focus on the data itself for things like conversion, compression, encryption, etc. instead of differing stream core capabilities...

Regards,
Chris


Notes 2007

In response to the issues raised below:
1.
The code in the book is wrong. It works for the ReadStream
class, but not the ReadWriteStream class.

2.
   s := ReadWriteStream on: #(1 2).
   s inspect.
The readLimit variable is 0

3.
Look at the ReadWriteStream contents method:

 contents
	"Answer with a copy of my collection from 1 to readLimit."

	readLimit _ readLimit max: position.
	^collection copyFrom: 1 to: readLimit

^ collection copyFrom: 1 to: 0 will be #().

[dae 15Dec07]
Are there bugs in the ReadWriteStream class in Squeak 3.5?

I've been reading the "Squeak: A Quick Trip to ObjectLand" book and have come across some problems with getting the examples using ReadWriteStream to work (on page 174-175). I don't get the same results as the book states as one should expect.

For instance, the following is a series of message sends from the book and what it says are the expected returned values:

 s := ReadWriteStream on: #(1 2).
 s next. "expected returned value should be 1"
 s next. "...should be 1"
 s atEnd. "...should be true"
 s reset.
 s next. "...should be 1"
 s position. "...should be 0"

However, this is what I actually got:

 s := ReadWriteStream on: #(1 2).
 s next. ".result is nil while expected result should be 1 according to the book"
 s atEnd. "results in true"
 s position. "Answers 0"
 s reset.
 s next. "result is nil should be 1 according to the book"
 s position. "Again, answers 0 while it should be 1"
 s atEnd. "true again despite reset message send"
 s contents. "#()"

I stepped through the on: message sent to ReadWriteStream and noticed that the read limit is set to 0.

I tried sending the with: message to see what happened.

 s := ReadWriteStream with: #(1 2).
 s next. "result returned is nil"
 s position. "2"
 s atEnd. "true"

Again, the next message returned nil instead of 1. Although, this time the position messaged returned 2.

The results are closer to what the book expects if a reset message is sent and the with: creation method is used instead of on:.

 s := ReadWriteStream with: #(1 2).
 s reset.
 s next. "1 as expected"
 s next. "2 likewise, as expected"
 s atEnd. "true"


I've written an unit test for ReadWriteStream, ReadWriteStream, which exposes these failings. Is it possible if those responsible for ReadWriteStream could please verify the tests assumptions?