Stream Support

Basic Operation

open(String fileName [, String mode ] ) or
(File file [, String mode ])

open() creates FileInputStream or FileOutputStream, depending on the 2nd parameter mode, from the specified file name. If the 2nd parameter is not specified "r" is implicitly passed.

"r"
FileInputStream(fileName)
"w"
FileOutputStream(fileName)
"a"
FileOutputStream(fileName, true)
read(InputStream input [, OutputStream output ])
read(Reader reader [, Writerwriter ] )

read() reads from input (or reader) and writes to outputStream (or writer). When the second parameter is omitted the output stream of the executing context is used.

import("java.io.*")

function copyFile (src, dst){
  fin = open(src, "r")
  fout = open(dst, "w")
  read(fin, fout)
  fin.close()
  fout.close()
}

URL

openURL(String urlString)

openURL() open a stream of a URL document.

setProperty("proxyHost", "machine") // proxy
setProperty("proxyPort", "8080")
stream = openURL("http://java.sun.com/index.html")
read(stream)
readURL(String urlString [, OutputStream out])
readURL("jar:http//javacenter.sun.co.jp/pnuts/applet.jar!/init.pnut")

Reading Resources

getResource(String name)
e.g.
rsrc = getResource("/init.pnut")
read(rsrc.openStream())

Read this for more detail on resources.

Reader, Writer

reader(InputStream input)
writer(OutputStream output)

reader() creates a BufferedReader from the specified InputStream object.

writer() creates a BufferedWriter from the specified OutputStream object.

e.g.
rd = reader(open("large.txt"))
while ((line = rd.readLine()) != null){
  println(line)
}

Buffer

openByteArray()
openCharArray()

openByteArray() creates a ByteArrayOutputStream.

openCharArray() creates a CharArrayWriter.

e.g.
buf = openByteArray()
read(open("foo.txt"), buf)
buf.toByteArray()

read(open("bar.txt"), buf)
buf.writeTo(fd = open("baz.txt", "w"))
fd.close()

getByteArray(InputStream inputStream)
getCharArray(Reader reader)

getByteArray() reads data from inputStream and returns a byte array.

getCharArray() reads data from reader and returns a char array.

Pipe I/O

pipe(PnutsFunction func, InputStream in ) or
(PnutsFunction func, OutputStream out )

func is a function with two arguments; inputStream and outputStream. When in is specified, it is passed as the first parameter of func on a separate thread. For the second paramter of func, internally used PipedOuputStream is passed. Then pipe() returns a PipedInputStream object that is connected to the PipedOutputStream.

When out is specified, it is passed as the second parameter of func on a separate thread. For the first parameter of func, internally used PipedInputStream is passed. pipe() returns a PipedOutputStream that is connected to the PipedInputStream.

e.g.
pin = pipe(base64encode, open("test.dat"))

read(pin, pout = pipe(gzip, open("test.gz", "w")))
pout.close()

Serialization

writeObject(Serializable object , String filename) or
(Serializable object , File file) or
(Serializable object , OutputStream output)

writeObject() serializes object and save in a file or the specified output stream.

readObject(String fileName) or
(File file) or
(InputStream input)

readObject() reads serialized data from a file or an input stream.


Back