Stream Question

David T. Lewis lewis at mail.msen.com
Fri Sep 21 00:43:26 UTC 2001


On Wed, Sep 19, 2001 at 07:06:11AM -0600, Ken G. Brown wrote:

> Is there some sort of first in first out stream available somewhere?
> 
> Thx.
> Ken

I was looking for the same thing recently, but the only FIFO I
could find is SharedQueue. Here is a quick hack, not well tested
but apparently working, which you may be able to adapt to your
purposes.  It's a Stream that uses a SharedQueue instead of a
Collection. It assumes that the objects sent through the FIFO are
characters, and answers a String in response to #next: and #upToEnd.

Dave
 
-------------- next part --------------
'From Squeak3.1alpha [latest update: #''Squeak3.1alpha'' of 7 March 2001 update 3960] on 20 September 2001 at 8:36:46 pm'!
Stream subclass: #InternalPipe
	instanceVariableNames: 'queue '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'OS-Command Shell'!
!InternalPipe commentStamp: '<historical>' prior: 0!
I am a first-in, first-out queue with streaming behavior. I behave similarly to an OSPipe,
but am implemented in the Smalltalk image rather than with external OS pipes.!


!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:36'!
next
	"Answer the next object accessible by the receiver."

	^ queue next
! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/19/2001 21:13'!
next: anInteger 
	"Answer the next anInteger elements of my collection."

	| strm |
	strm _ WriteStream on: ''.
	(1 to: anInteger) do:
		[:index |
		self atEnd
			ifTrue: [^ strm contents]
			ifFalse: [strm nextPut: self next]].
	^ strm contents
! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:37'!
nextPut: anObject 
	"Insert the argument, anObject, as the next object accessible by the 
	receiver. Answer anObject."

	^ queue nextPut: anObject! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:41'!
nextPutAll: aCollection 
	"Append the elements of aCollection to the sequence of objects accessible 
	by the receiver. Answer aCollection."

	aCollection do: [:e | queue nextPut: e]
! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:42'!
peek

	^ queue peek
! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:35'!
queue

	^ queue ifNil: [queue _ SharedQueue new]
! !

!InternalPipe methodsFor: 'accessing' stamp: 'dtl 9/16/2001 22:47'!
upToEnd
	"Answer the remaining elements in the string"

	| strm |
	strm _ WriteStream on: ''.
	[self atEnd] whileFalse: [strm nextPut: queue next].
	^ strm contents! !


!InternalPipe methodsFor: 'initialize-release' stamp: 'dtl 9/16/2001 22:35'!
initialize

	self queue
! !


!InternalPipe methodsFor: 'testing' stamp: 'dtl 9/16/2001 22:46'!
atEnd
	"Answer whether the receiver can access any more objects."

	^ queue size == 0
! !

!InternalPipe methodsFor: 'testing' stamp: 'dtl 9/16/2001 22:49'!
closed

	^ false! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

InternalPipe class
	instanceVariableNames: ''!

!InternalPipe class methodsFor: 'instance creation' stamp: 'dtl 9/16/2001 22:33'!
new

	"InternalPipe new"

	^ super basicNew initialize! !


!InternalPipe class methodsFor: 'examples' stamp: 'dtl 9/19/2001 21:18'!
testPipe

	"InternalPipe testPipe inspect"

	| pipe result |
	pipe _ self new.
	pipe nextPutAll: 'string to send through an InternalPipe'.
	result _ pipe upToEnd.
	pipe close.
	^ result
! !


More information about the Squeak-dev mailing list