sleep.bridges.io

Class IOObject

public class IOObject extends Object

The IOObject is the parent class for all IO Source objects that are compatible with Sleep's I/O API.

When an IOObject is created, calling openRead() with the appropriate input stream will initialize this IOObject to work with IO functions that read data. Calling openWrite() has the same effect for print functions. It is the responsibility of the IOObject child class to invoke openRead and openWrite. This is usually done in the constructor.

The pipeline for reading data looks like this:

... <- DataInputStream <- BufferedInputStream <- Original Input Stream

The pipeline for writing data is:

... -> DataOutputStream -> Original Output Stream
Field Summary
protected byte[]buffer
protected InputStreamin
protected OutputStreamout
protected BufferedInputStreamreader
protected DataInputStreamreaderb
protected InputStreamReaderreaderu
protected Threadthread
protected Scalartoken
protected DataOutputStreamwriterb
protected OutputStreamWriterwriteru
Method Summary
voidclose()
Closes all of the reader's / writer's opened by this IOObject.
byte[]getBuffer(int size)
static IOObjectgetConsole(ScriptEnvironment environment)
returns an IOObject that represents stdin/stdout to Sleep's I/O API.
BufferedInputStreamgetInputBuffer()
Returns the ascii data reader
InputStreamgetInputStream()
Returns the latest hooking point into the input stream
OutputStreamgetOutputStream()
Returns the latest hooking point into the output stream
DataInputStreamgetReader()
Returns the binary data reader
ObjectgetSource()
return the actual source of this IO for scripters to query using HOES
ThreadgetThread()
returns the thread associated with this IOObject
ScalargetToken()
returns a scalar token associated with this IOObject.
DataOutputStreamgetWriter()
Returns the binary data writer
booleanisEOF()
Returns true if the reader is closed
voidopenRead(InputStream _in)
Initializes a binary reader (a DataInputStream) and a text reader (a BufferedReader) against this input stream.
voidopenWrite(OutputStream _out)
Initializes a binary writer (a DataOutputStream) and a text writer (a PrintWriter) against this input stream.
voidprint(String text)
Prints out a line of text with no newline character appended
voidprintLine(String text)
Prints out a line of text with a newline character appended
StringreadCharacter()
Reads in a character of text and returns as a string.
StringreadLine()
Reads in a line of text
voidsendEOF()
Closes down the output streams effectively sending an end of file message to the reading end.
voidsetEncoding(String name)
set the charset to be used for all unicode aware reads/writes from/to this stream
voidsetThread(Thread t)
set the thread used for this IOObject (currently used to allow a script to wait() on the threads completion)
voidsetToken(Scalar t)
sets the scalar token associated with this IOObject.
Scalarwait(ScriptEnvironment env, long timeout)

Field Detail

buffer

protected byte[] buffer

in

protected InputStream in

out

protected OutputStream out

reader

protected BufferedInputStream reader

readerb

protected DataInputStream readerb

readeru

protected InputStreamReader readeru

thread

protected Thread thread

token

protected Scalar token

writerb

protected DataOutputStream writerb

writeru

protected OutputStreamWriter writeru

Method Detail

close

public void close()
Closes all of the reader's / writer's opened by this IOObject. If the IO Source object opens any streams, this method should be overriden to close those streams when requested. Calling super.close() is highly recommended as well.

getBuffer

public byte[] getBuffer(int size)

getConsole

public static IOObject getConsole(ScriptEnvironment environment)
returns an IOObject that represents stdin/stdout to Sleep's I/O API. To set a script's console object install an IOObject into a script environment under the variable name %console%

getInputBuffer

public BufferedInputStream getInputBuffer()
Returns the ascii data reader

getInputStream

public InputStream getInputStream()
Returns the latest hooking point into the input stream

getOutputStream

public OutputStream getOutputStream()
Returns the latest hooking point into the output stream

getReader

public DataInputStream getReader()
Returns the binary data reader

getSource

public Object getSource()
return the actual source of this IO for scripters to query using HOES

getThread

public Thread getThread()
returns the thread associated with this IOObject

getToken

public Scalar getToken()
returns a scalar token associated with this IOObject. Will return the empty scalar if the token is null. The token is essentially the stored return value of an executing thread.

getWriter

public DataOutputStream getWriter()
Returns the binary data writer

isEOF

public boolean isEOF()
Returns true if the reader is closed

openRead

public void openRead(InputStream _in)
Initializes a binary reader (a DataInputStream) and a text reader (a BufferedReader) against this input stream. Calling this effectively makes this IOObject useable with Sleep's IO read* functions.

openWrite

public void openWrite(OutputStream _out)
Initializes a binary writer (a DataOutputStream) and a text writer (a PrintWriter) against this input stream. Calling this effectively makes this IOObject useable with Sleep's IO print* functions.

print

public void print(String text)
Prints out a line of text with no newline character appended

printLine

public void printLine(String text)
Prints out a line of text with a newline character appended

readCharacter

public String readCharacter()
Reads in a character of text and returns as a string.

readLine

public String readLine()
Reads in a line of text

sendEOF

public void sendEOF()
Closes down the output streams effectively sending an end of file message to the reading end.

setEncoding

public void setEncoding(String name)
set the charset to be used for all unicode aware reads/writes from/to this stream

setThread

public void setThread(Thread t)
set the thread used for this IOObject (currently used to allow a script to wait() on the threads completion)

setToken

public void setToken(Scalar t)
sets the scalar token associated with this IOObject. Any ScriptInstance object calls setToken on it's parent IOObject. This method is called when the script is finished running and has a return value waiting. This value can be retrieved in Sleep with the &wait function.

wait

public Scalar wait(ScriptEnvironment env, long timeout)