|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Object | +--FileStream
A FileStream allows reading or writing specific parts of a File.
The FileStream
class exposes several ways of reading from and writing to Files.
Examples include reading and writing bytes, strings and lines.
When the FileStream
is created a pointer is usually set at the beginning of the file.
As the read methods are called, the pointer moves through the file. Subsequent
calls to the read methods read from that point and onward. When the end of
the file is reached, the eof
property is set to true
.
This class supports Base64 data for use in cases like XMLHttpRequest
. This
object does not support writing binary data, so the Base64 methods can be
used to write binary data encoded as strings.
By default, the FileStream
object uses an UTF-8 encoding when writing. You
can change this by setting the encoding
property of the FileStream
or by
supplying a charset
argument to the various methods that write characters.
Field Summary | |
int |
bytesAvailable
Number of of bytes available from the current position to the end of the FileStream . |
String |
encoding
The encoding of this FileStream . |
boolean |
eof
Whether or not the end of the FileStream has been reached. |
String |
newLine
Newline character used for this particular FileStream . |
int |
position
The current byte index position of this FileStream . |
String |
systemNewLine
The system default character for separating lines in a file. |
Constructor Summary | |
FileStream()
This class has no public constructor. |
Method Summary | |
void
|
close( )
Close the FileStream for reading or writing.
|
String
|
read( <int> length, <String> charset )
Read a number of characters from the FileStream. |
String
|
readBase64( <int> length )
Read bytes from the FileStream and encode it as Base64
|
ByteArray
|
readBytes( <int> length )
Read a number of bytes from the FileStream
|
String
|
readLine( <String> charset )
Read a line of characters from the FileStream
|
void
|
write( <String> string, <String> charset )
Write a string of characters to the FileStream
|
void
|
writeBase64( <String> string )
Decode a Base64 encoded string and write the data to the FileStream .
|
void
|
writeBytes( <ByteArray> bytes, <int> length )
Write a set of bytes to the FileStream
|
void
|
writeFile( <File> file )
Write a File to the FileStream .
|
void
|
writeImage( <HTMLImageElement> image )
Write an image to the FileStream .
|
void
|
writeLine( <String> string, <String> charset )
Write a line of characters to the FileStream |
Field Detail |
int bytesAvailable
FileStream
.
The value of this property is effectively fileSize
- position
.
String encoding
FileStream
.
This property defaults to UTF-8. Change it to override the default encoding
used when writing to the FileStream
. This can be overridden on a case-by-case
basis by supplying the charset
argument to the various methods
which write characters.
boolean eof
FileStream
has been reached.
If the FileStream
is unreadable, this property is true
.
String newLine
FileStream
.
This is the same as systemNewLine when the FileStream
is created.
This can be set to override the default character used for splitting lines when calling
readLine() or writeLine().
int position
FileStream
.
You may set the position programmatically. If it is set to < 0, the position will be 0.
If it is set to > fileSize
, the position will be fileSize
.
String systemNewLine
Constructor Detail |
FileStream()
Method Detail |
void close( )
FileStream
for reading or writing.String read( <int> length, <String> charset )
This function will read length
number
of characters from the stream.
If there are less than length
characters left
in the file, only the remaining characters in the file are
read, and the eof
property is set to true
.
If eof
is true
when this method is called,
null will be returned.
The resulting String is encoded with the charset in the
FileStream.encoding
property unless the optional charset
argument is given.
length
- Number of characters to read.
charset
- The character set to use when reading.
String readBase64( <int> length )
FileStream
and encode it as Base64
This method will read length
number of
bytes from the FileStream
and return the data as a Base64
encoded String.
This is typically used to encode data from binary files
in order to transfer them for example over XMLHttpRequest
.
As the method will read a number of bytes as specified
in the length argument, and then encode it, a call to
stream.readBase64(100)
will not necessarily end
up as a String with a length of 100.
If there are less than length
bytes left
in the file, only the remaining bytes in the file are
read, and the eof
property is set to true
.
If eof
is true
when this method is called,
null will be returned.
length
- Number of bytes to read.
FileStream
as a Base64 encoded String, or null if there are no data to read.
ByteArray readBytes( <int> length )
FileStream
This function will read length
number
of bytes from the FileStream
.
If there are less than length
bytes left
in the file, only the remaining bytes in the file are
read, and the eof
property is set to true
.
If eof
is true
when this method is called,
null will be returned.
length
- The number of bytes to read.
String readLine( <String> charset )
FileStream
This functions will read all characters up to and including the next
newline in the FileStream
as defined by the newLine property.
If there are no newlines left in the stream, the resulting string will
not have a newline character and the eof
property is set
to true
.
If eof
is true
when this method is called,
null is returned.
The resulting String is encoded with the charset in the
FileStream.encoding
property unless the optional charset
argument is given.
charset
- The character set to use when reading. Optional.
void write( <String> string, <String> charset )
FileStream
This method will write the given String of characters to the FileStream
, using
the given charset
or the charset in the FileStream.encoding
property.
The charset
argument is currently ignored.
string
- The String of characters to write.
charset
- The charset to use when writing. Optional.
void writeBase64( <String> string )
FileStream
.
This method takes a String encoded as Base64, decodes it and writes
the resulting data to the FileStream
. It is typically used for
binary data encoded as Base64 when its transferred for example
over XMLHttpRequest
.string
- Base64 encoded String to write.
void writeBytes( <ByteArray> bytes, <int> length )
FileStream
This method will write the length
first bytes
from the given ByteArray
to the stream.bytes
- The bytes to write.
length
- The number of bytes to write.
void writeFile( <File> file )
FileStream
.
This will write the entire contents of the given File
to the FileStream
.file
- The File to write.
void writeImage( <HTMLImageElement> image )
FileStream
.
This function will take either an HTMLImageElement
or an HTMLCanvasElement
and write the binary data to the FileStream
. In the case of HTMLCanvasElement
,
the image is first encoded as a PNG image.image
- The HTMLImageElement
or HTMLCanvasElement
to write.
void writeLine( <String> string, <String> charset )
This method will write the given String with an appended newline character taken from
the newLine property to the FileStream
, using the given charset or the charset
in the FileStream.encoding
property.
The charset
argument is currently ignored.
string
- The string of characters to write.
charset
- The charset
to use when writing. Optional.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |