class: PositionableStream
added:
#copyToEndInto:bufferSize:
#nextAvailable:into:startingAt:
#nextBytes:into:startingAt:
comment/format in: #upToAll:
--- a/PositionableStream.st Thu Mar 12 20:21:48 2015 +0100
+++ b/PositionableStream.st Thu Mar 12 20:24:30 2015 +0100
@@ -253,6 +253,35 @@
"Modified: / 04-06-2007 / 17:21:55 / cg"
! !
+!PositionableStream methodsFor:'misc functions'!
+
+copyToEndInto:aWriteStream bufferSize:bufferSize
+ "read from the receiver, and write all data up to the end to another stream.
+ Return the number of bytes which have been transferred.
+ Redefined here to avoid intermediate buffers/garbage.
+ bufferSize does not matter here."
+
+ |cnt|
+
+ collection notNil ifTrue:[
+ aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit.
+ cnt := readLimit - position.
+ position := readLimit.
+ ^ cnt.
+ ].
+ ^ super copyToEndInto:aWriteStream bufferSize:bufferSize.
+
+ "
+ |rs ws cnt|
+
+ ws := #() writeStream.
+ rs := #( 1 2 3 4 a nil true) readWriteStream.
+ rs next.
+ cnt := rs copyToEndInto:ws bufferSize:0.
+ Transcript show:cnt; show:' '; showCR:ws contents.
+ "
+! !
+
!PositionableStream methodsFor:'positioning'!
backStep
@@ -649,6 +678,79 @@
"
!
+nextAvailable:count into:aCollection startingAt:initialIndex
+ "return the next count objects from the stream. If the end is
+ reached before, only that many objects are copyied into the
+ collection.
+ Returns the number of objects that have been actually read."
+
+ | max |
+
+ collection notNil ifTrue:[
+ max := (readLimit - position) min: count.
+ aCollection
+ replaceFrom: initialIndex
+ to: initialIndex+max-1
+ with: collection
+ startingAt: position+1.
+ position := position + max.
+ ^ max.
+ ].
+
+ ^ super nextAvailable:count into:aCollection startingAt:initialIndex.
+
+ "
+ |s n buffer|
+
+ buffer := ByteArray new:10.
+
+ s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
+ s next:3.
+ n := s nextBytes:9 into:buffer startingAt:1.
+ Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
+ "
+!
+
+nextBytes:numBytes into:aCollection startingAt:initialIndex
+ "return the next numBytes from the stream. If the end is
+ reached before, only that many bytes are copyied into the
+ collection.
+ Returns the number of bytes that have been actually read.
+ The receiver must support reading of binary bytes.
+
+ Notice: this method is provided here for protocol completeness
+ with externalStreams - it is normally not used with other
+ streams."
+
+ |max|
+
+ (collection isByteCollection
+ and:[aCollection isByteCollection]) ifTrue:[
+ "do it the fast way"
+ max := (readLimit - position) min: numBytes.
+ aCollection
+ replaceBytesFrom:initialIndex
+ to:(initialIndex + max - 1)
+ with:collection
+ startingAt:position+1.
+ position := position + max.
+ ^ max
+ ].
+ "do it the hard way"
+ ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex
+
+ "
+ |s n buffer|
+
+ buffer := ByteArray new:10.
+
+ s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
+ s next:3.
+ n := s nextBytes:9 into:buffer startingAt:1.
+ Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
+ "
+!
+
upToAll:aCollection
"read until a subcollection consisisting of the elements in aCollection is encountered.
Return everything read excluding the elements in aCollection.
@@ -666,7 +768,7 @@
which position after the found item. We implement the method
this way for the sake of ST80-compatibility."
- "/ in the future, this wil have the Squeak semantics, which is upToAllExcluding:
+ "/ in the future, this will have the Squeak semantics, which is upToAllExcluding:
^ self upToAll_positionBefore:aCollection
@@ -780,11 +882,11 @@
!PositionableStream class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.171 2015-03-12 19:24:30 stefan Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.170 2015-03-12 17:14:00 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/PositionableStream.st,v 1.171 2015-03-12 19:24:30 stefan Exp $'
! !