Discard the next count items in the stream.
testing
atEndReturn true if the end was previously found or answer a guess based on the size of the contents of the pipe.
dataAvaliable
Return true if there are any data ready to be read from the pipe.
PipeReadStream OS-Streams
isEmpty
Return true if the pipe contains no data.
Rationale
This class allows the reading of data from an operating system pipe. Using a PlpeReadStream, a Smalltalk process can obtain data from a related process.
Discussion
The abstraction of a pipe stream is used outside of Smalltalk, for example, several C functions use a stream for reading, including fscanf, gets, and getc. Most users are familiar with the standard input stream, stdin.
Streams make accessing data easier, because they "know" the position where reading or writing can occur. It isn't necessary for you to keep track of how many characters have been read so that you can specify which character to read next. A stream knows when it has reached the end of its data and when it is empty.
Protocol
To create an instance of PipeReadStream, you should use an
instance creation
message inherited from the superclass, PipeStream - for example, send the message openOn: to PipeReadStream.Initialize-release
methods initialize appropriate instance variables and add this Pipe Read Stream to the class variable OpenPipeStreams.Accessing
methods set the mode instance variable and, if the mode was incorrect, copy the data into the correct type of buffer (ByteArray or String). Data in the stream can be read - the entire file, only the contents of the buffer, a single byte, or a specified number of bytes. Data from the stream can be read and returned in a Collection. The inherited message next Put: is intercepted because this is a stream for reading.Enumerating
methods evaluate a block for each byte/character or each line in the stream.Positioning
allows you to skip over (discard) a specified number of bytes/characters in the stream.Testing
methods answer whether the end of the stream's data has been reached, whether there are data available for reading, and whether the pipe is empty.PipeReadStream OS-Streams
Examples
The following method is found under SystemDictionary
initialize-release.
startUp
"Do all that is necessary to begin execution of a Smalltalk image."
I
stdinI
ScheduledControllers restore.
OS notNil iITrue:
[(aS status: 0) fileSize = 0 ifFalse:
[stdin ~ PipeReadStream openOnFd:
(os
duplicateFd: 0 with: 5).Transcript refresh; cr; cr; show: 'Filing in from: standard input'; cr.
stdin fileln; close].
Sub task markAndSignalAll; install].
Sensor cursorPoint: Display viewport topLeft
In addition to starting Smalltalk, the method above provides the ability for you to pass a file or "dolt" to Smalltalk from the command line when you invoke an image.
It makes a system call to determine if the pipe for standard input (file descriptor
=
0) has sizeo.
If there is anything in that pipe, an instance of PipeReadStream is created on standard input, but using a different file descriptor - the duplicateFd:message links the new stream with standard input. Next the "filing in" message is displayed in the System Transcript. Smalltalk then attempts to "file in" whatever is present in standard input. If the standard input is not in "file in" format, a syntax error will result. Typical fileln files have names ending in
.st
or.ws.
Here are two examples of command lines which pass arguments to Smalltalk:echo "Transcript show: 'Your message here.'.!" I <smalltalk or imageName>
cd <smalltalk fileln directory>
smalltalk < Clock.st
In the first example you would type small talk or the name of your image in the position indicated by the angle brackets « ». The string you type as the argument to show: will be displayed in the System Transcript when the image comes up. In the second example, you first cd to the directory which contains the fileln files.
Without moving to that directory, you could accomplish the same thing by giving the full path of
Clock.st
on line two. The clock code will be filed in when the image comes up. The first example uses a shell pipe operator (I );
the second example uses a shell 110 redirection operator (" <" for input).Related Classes Subclasses:
OSFllter
PipeReadStream OS-Streams
You will find it helpful to look at the following classes for a further understanding of this class:
• the Pipe Stream hierarchy,
• Pipe, and
• methods nextLine and nextCNumber in PositionableStream.
PipeStream
ExternalStream subclass: #PipeStream instanceVariableNames:
class Variable Names : poolDictionaries:
category:
Summary
'fileDescriptor mode' 'OpenPipeStreams ' 'OS-Streams'
OS-Streams
PipeStreams are stream-based interfaces to operating system pipes. Pipe Stream is an abstract class implementing protocol common to PipeReadStream and PipeWriteStream, such as opening, closing, and accessing methods.
Instance Variables
fileDescriptor <Integer>
The file descriptor by which the underlying pipe is accessed.
mode <Symbol>
Either #binary or #text, depending on what type of data is expected to be used on the underlying pipe.
Class Variables
OpenPlpeStreams <OrderedCollection>
All open PipeStreams are listed here.
Instance Methods
initialize-release
InltializeOnFd: anFd
Initialize the pipe stream with the supplied open file descriptor, which must refer to a Pipe.
accessing
binarySet the mode of this PipeStream to binary.
bufferClass
Return the class that is used as a buffer for this PipeStream, depending on the data type which has been selected.
PipeStream OS-Streams
contents
Not appropriate for a PipeStream.
fileDescriptor
Answer the descriptor to be accessed for streaming over data.
fileDescrlptor: fd
size
text
Store the argument as the descriptor to be accessed for streaming over data.
Return the size of the PipeStream contents as reported by the operating system.
Set the mode of this PipeStream to text.
copying
copyNot appropriate for a PipeStream.