It turns out we had half a dozen implementation, of varying degree of
efficiency. Now all a stream has to override is #next:putAll:startingAt: (if they wish to). Paolo 2007-08-23 Paolo Bonzini <[hidden email]> * kernel/ByteStream.st: Remove #nextPutAll:. * kernel/FileDescr.st: Remove #nextPutAll: and #nextPutAllFlush:. * kernel/FileStream.st: Remove #nextPutAll: and #nextPutAllFlush:. * kernel/Stream.st: Use #nextHunk in #nextPutAll:, add #nextPutAllFlush:. * kernel/Transcript.st: Implement #next:putAll:startingAt:. * kernel/WriteStream.st: Likewise. * zlib/ZLibReadStream.st: Delay blocking until first access. * zlib/ZLibWriteStream.st: Remove #nextPutAll:. --- orig/kernel/ByteStream.st +++ mod/kernel/ByteStream.st @@ -161,32 +161,6 @@ nextPutByte: anInteger ^super nextPut: (int bitAnd: 255) ! -nextPutAll: aCollection - "Write all the objects in aCollection to the receiver" - | collEnd relative lastCopied | - aCollection isSequenceable - ifFalse: [ ^super nextPutAll: aCollection ]. - - aCollection isEmpty ifTrue: [ ^self ]. - - collEnd := ptr + aCollection size - 1. - relative := ptr - 1. - - [ - lastCopied := collEnd min: collection size. - collection - replaceFrom: ptr - to: lastCopied - with: aCollection - startingAt: ptr - relative. - - (ptr := lastCopied + 1) > collEnd - ] whileFalse: [ - ptr > endPtr ifTrue: [ endPtr := ptr ]. - self growCollection - ]. -! - nextPutByteArray: aByteArray "Store aByteArray on the byte array" ^self nextPutAll: aByteArray --- orig/kernel/FileDescr.st +++ mod/kernel/FileDescr.st @@ -532,27 +532,6 @@ next: n putAll: aCollection startingAt: "Put the characters in the supplied range of aCollection in the file" ^self write: aCollection from: position to: position + n - 1! -nextPutAll: aCollection - "Put all the characters in aCollection in the file" - | stream | - aCollection isSequenceable ifFalse: [ - [ stream := aCollection readStream ] - on: MessageNotUnderstood - do: [ :ex | ex return: aCollection asString readStream ]. - - [ stream atEnd ] whileFalse: [ self write: stream nextHunk ]. - ^self ]. - - self write: aCollection asString -! - -nextPutAllFlush: aCollection - "Put all the characters in aCollection in the file. For compatibility - with FileStream (FileDescriptor is not buffered, thus this method is - equivalent to nextPutAll:" - self write: aCollection asString -! - nextByteArray: anInteger "Return the next 'anInteger' bytes from the stream, as a ByteArray." ^(self next: anInteger) asByteArray --- orig/kernel/FileStream.st +++ mod/kernel/FileStream.st @@ -351,45 +351,6 @@ copyFrom: from to: to !FileStream methodsFor: 'overriding inherited methods'! -nextPutAllFlush: aCollection - "Put all the characters in aCollection in the file, then flush the - file buffers" - | n coll written | - coll := aCollection asString. - n := coll size. - written := collection size - ptr + 1 min: n. - self next: written bufferAll: coll startingAt: 1; flush. - written = n ifFalse: [ self write: coll from: written + 1 to: n ]. -! - -nextPutAll: aCollection - "Put all the characters in aCollection in the file" - | n coll written | - "Just do 'coll := aCollection asString', but avoid expensive operations - in the common case where aCollection is already a String." - coll := aCollection isSequenceable - ifTrue: [ aCollection ] - ifFalse: [ - [ aCollection asString ] - on: MessageNotUnderstood - do: [ :ex | - "If we are in a stream, try to facilitate buffering." - [ aCollection atEnd ] whileFalse: [ - coll := aCollection nextHunk. - self next: coll size putAll: coll startingAt: 1 ]. - ^self ] ]. - - n := coll size. - written := collection size - ptr + 1 min: n. - self next: written bufferAll: coll startingAt: 1. - written = n ifTrue: [ ^self ]. - - self flush. - n - written < collection size - ifTrue: [ self next: n - written bufferAll: coll startingAt: written + 1 ] - ifFalse: [ self write: coll from: written + 1 to: n ]. -! - next: n putAll: aCollection startingAt: pos | written | written := collection size - ptr + 1 min: n. --- orig/kernel/Stream.st +++ mod/kernel/Stream.st @@ -201,11 +201,28 @@ next: n putAll: aCollection startingAt: ^aCollection ! +nextPutAllFlush: aCollection + "Put all the elements of aCollection in the stream, then flush the + buffers if supported by the stream." + self nextPutAll: aCollection; flush +! + nextPutAll: aCollection "Write all the objects in aCollection to the receiver" - aCollection isSequenceable - ifTrue: [ self next: aCollection size putAll: aCollection startingAt: 1 ] - ifFalse: [ aCollection do: [ :element | self nextPut: element ] ]. + | coll | + aCollection isSequenceable ifTrue: [ + ^self next: aCollection size putAll: aCollection startingAt: 1 ]. + + "Try to detect a Stream." + [ aCollection atEnd ] + on: MessageNotUnderstood + do: [ :ex | ^self nextPutAll: (self species withAll: aCollection) ]. + + "If we are in a stream, try to facilitate buffering." + [ aCollection atEnd ] whileFalse: [ + coll := aCollection nextHunk. + self next: coll size putAll: coll startingAt: 1 ]. + ^aCollection ! --- orig/kernel/Transcript.st +++ mod/kernel/Transcript.st @@ -103,10 +103,10 @@ next: anInteger put: anObject self nextPutAll: (String new: anInteger withAll: anObject) ! -nextPutAll: aString +next: n putAll: aString startingAt: pos "Write aString to the Transcript" semaphore critical: [ - self primNextPutAll: aString. + self primNextPutAll: (aString copyFrom: pos to: pos + n - 1). Processor idle ] ! --- orig/kernel/WriteStream.st +++ mod/kernel/WriteStream.st @@ -99,6 +99,27 @@ nextPut: anObject ^anObject ! +next: n putAll: aCollection startingAt: pos + "Put n characters or bytes of aCollection, starting at the pos-th, + in the collection buffer." + + | end written amount | + ptr = collection size ifTrue: [ self growCollection ]. + written := 0. + [ + end := collection size min: ptr + (n - written - 1). + end >= ptr ifTrue: [ + collection + replaceFrom: ptr + to: end + with: aCollection + startingAt: pos + written. + written := written + (end - ptr + 1). + ptr := end + 1 ]. + written < n + ] whileTrue: [ self growCollection ]. + ptr > endPtr ifTrue: [ endPtr := ptr - 1 ]! + contents "Returns a collection of the same type that the stream accesses, up to and including the final element." --- orig/packages/zlib/ZLibReadStream.st +++ mod/packages/zlib/ZLibReadStream.st @@ -145,9 +145,9 @@ position !ZlibReadStream methodsFor: 'private'! resetBuffer + ptr := 0. delta := 0. - endPtr := 0. - self fillBuffer! + endPtr := 0! initialize: aStream super initialize: aStream. --- orig/packages/zlib/ZLibWriteStream.st +++ mod/packages/zlib/ZLibWriteStream.st @@ -147,28 +147,6 @@ nextPut: aByte inBytes at: ptr put: aByte. ptr := ptr + 1! -nextPutAll: aCollection - "Put all the characters or bytes in aCollection in the deflation buffer." - - | n coll written | - "Just do 'coll := aCollection asString', but avoid expensive operations - in the common case where aCollection is already a String." - ptr = inBytes size ifTrue: [ self flushBuffer ]. - - coll := aCollection isSequenceable - ifTrue: [ aCollection ] - ifFalse: [ - [ aCollection asString ] - on: MessageNotUnderstood - do: [ :ex | - "If we are in a stream, try to facilitate buffering." - [ aCollection atEnd ] whileFalse: [ - coll := aCollection nextHunk. - self next: coll size putAll: coll startingAt: 1 ]. - ^self ] ]. - - self next: coll size putAll: coll startingAt: 1! - next: n putAll: aCollection startingAt: pos "Put n characters or bytes of aCollection, starting at the pos-th, in the deflation buffer." _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
On Thu, Aug 23, 2007 at 12:40:52PM +0200, Paolo Bonzini wrote:
> It turns out we had half a dozen implementation, of varying degree of > efficiency. Now all a stream has to override is > #next:putAll:startingAt: (if they wish to). I love it when code becomes simpler and more mighty :-) [.snipped lots of patch lines with - in front.] Robin _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
Free forum by Nabble | Edit this page |