| Package | flash.utils |
| Class | public class ByteArray |
| Inheritance | ByteArray Object |
| Implements | IDataInput, IDataOutput |
Note: The ByteArray class is for advanced ActionScript developers who need to access data on the byte level.
In-memory data is a packed array (the most compact representation for the data type)
of bytes, but an instance of the ByteArray
class can be manipulated with the standard ActionScript [] (array access) operators.
It also can be read and written to as an in-memory file, using
methods similar to those in the URLStream and Socket classes.
In addition, zlib compression and decompression are supported, as well as Action Message Format (AMF) object serialization.
Possible uses of the ByteArray class include the following:
See also
| Property | Defined by | ||
|---|---|---|---|
| bytesAvailable : uint
[read-only]
The number of bytes of data available for reading
from the current position in the byte array to the
end of the array.
| ByteArray | ||
![]() | constructor : Object
A reference to the class object or constructor function for a given object instance.
| Object | |
| defaultObjectEncoding : uint
[static]
Denotes the default object encoding for the ByteArray class to use for a new ByteArray instance.
| ByteArray | ||
| endian : String
Changes or reads the byte order for the data; either "bigEndian" or "littleEndian".
| ByteArray | ||
| length : uint
The length of the ByteArray object, in bytes.
| ByteArray | ||
| objectEncoding : uint
Used to determine whether the ActionScript 3.0, ActionScript 2.0, or ActionScript 1.0 format should be
used when writing to, or reading from, a ByteArray instance.
| ByteArray | ||
| position : uint
Moves, or returns the current position, in bytes, of the file
pointer into the ByteArray object.
| ByteArray | ||
![]() | prototype : Object
[static]
A reference to the prototype object of a class or function object.
| Object | |
| Method | Defined by | ||
|---|---|---|---|
|
Creates a ByteArray instance representing a packed array of bytes, so that you can use the methods and properties in this class to optimize your data storage and stream.
| ByteArray | ||
|
Compresses the byte array using zlib compression.
| ByteArray | ||
![]() |
Indicates whether an object has a specified property defined.
| Object | |
![]() |
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter.
| Object | |
![]() |
Indicates whether the specified property exists and is enumerable.
| Object | |
|
Reads a Boolean value from the byte stream.
| ByteArray | ||
|
Reads a signed byte from the byte stream.
| ByteArray | ||
|
Reads the number of data bytes, specified by the
length parameter, from the byte stream. | ByteArray | ||
|
Reads an IEEE 754 double-precision (64-bit) floating-point number from the byte stream.
| ByteArray | ||
|
Reads an IEEE 754 single-precision (32-bit) floating-point number from the byte stream.
| ByteArray | ||
|
Reads a signed 32-bit integer from the byte stream.
| ByteArray | ||
|
Reads a multibyte string of specified length from the byte stream using the
specified character set.
| ByteArray | ||
|
readObject():*
Reads an object from the byte array, encoded in AMF
serialized format.
| ByteArray | ||
|
Reads a signed 16-bit integer from the byte stream.
| ByteArray | ||
|
Reads an unsigned byte from the byte stream.
| ByteArray | ||
|
Reads an unsigned 32-bit integer from the byte stream.
| ByteArray | ||
|
Reads an unsigned 16-bit integer from the byte stream.
| ByteArray | ||
|
Reads a UTF-8 string from the byte stream.
| ByteArray | ||
|
Reads a sequence of UTF-8 bytes specified by the
length
parameter from the byte stream and returns a string. | ByteArray | ||
![]() |
Sets the availability of a dynamic property for loop operations.
| Object | |
|
Converts the byte array to a string.
| ByteArray | ||
|
Decompresses the byte array.
| ByteArray | ||
![]() |
Returns the primitive value of the specified object.
| Object | |
|
Writes a Boolean value.
| ByteArray | ||
|
Writes a byte to the byte stream.
| ByteArray | ||
|
Writes a sequence of
length bytes from the
specified byte array, bytes,
starting offset (zero-based index) bytes
into the byte stream. | ByteArray | ||
|
Writes an IEEE 754 double-precision (64-bit) floating-point number to the byte stream.
| ByteArray | ||
|
Writes an IEEE 754 single-precision (32-bit) floating-point number to the byte stream.
| ByteArray | ||
|
Writes a 32-bit signed integer to the byte stream.
| ByteArray | ||
|
Writes a multibyte string to the byte stream using the specified character set.
| ByteArray | ||
|
Writes an object into the byte array in AMF
serialized format.
| ByteArray | ||
|
Writes a 16-bit integer to the byte stream.
| ByteArray | ||
|
Writes a 32-bit unsigned integer to the byte stream.
| ByteArray | ||
|
Writes a UTF-8 string to the byte stream.
| ByteArray | ||
|
Writes a UTF-8 string to the byte stream.
| ByteArray | ||
| bytesAvailable | property |
bytesAvailable:uint [read-only]The number of bytes of data available for reading from the current position in the byte array to the end of the array.
Use the bytesAvailable property in conjunction
with the read methods each time you access a ByteArray object
to ensure that you are reading valid data.
public function get bytesAvailable():uint
| defaultObjectEncoding | property |
defaultObjectEncoding:uint [read-write]
Denotes the default object encoding for the ByteArray class to use for a new ByteArray instance.
When you create a new ByteArray instance, the encoding on that instance starts
with the value of defaultObjectEncoding.
The defaultObjectEncoding property is initialized to ObjectEncoding.AMF3.
When an object is written to or read from binary data, the objectEncoding value
is used to determine whether the ActionScript 3.0, ActionScript2.0, or ActionScript 1.0 format should be used. The value is a
constant from the ObjectEncoding class.
public static function get defaultObjectEncoding():uint
public function set defaultObjectEncoding(value:uint):void
See also
| endian | property |
endian:String [read-write]Changes or reads the byte order for the data; either "bigEndian" or "littleEndian". The values are constants from the Endian class.
Implementation public function get endian():String
public function set endian(value:String):void
See also
| length | property |
length:uint [read-write]The length of the ByteArray object, in bytes.
If the length is set to a value that is larger than the current length, Flash Player fills the right side with zeros.
If the length is set to a value that is smaller than the current length, the array is truncated.
Implementation public function get length():uint
public function set length(value:uint):void
| objectEncoding | property |
objectEncoding:uint [read-write]Used to determine whether the ActionScript 3.0, ActionScript 2.0, or ActionScript 1.0 format should be used when writing to, or reading from, a ByteArray instance. The value is a constant from the ObjectEncoding class.
Implementation public function get objectEncoding():uint
public function set objectEncoding(value:uint):void
See also
| position | property |
position:uint [read-write]Moves, or returns the current position, in bytes, of the file pointer into the ByteArray object. This is the point at which the next call to a read method starts reading or a write method starts writing.
Implementation public function get position():uint
public function set position(value:uint):void
| ByteArray | () | constructor |
public function ByteArray()Creates a ByteArray instance representing a packed array of bytes, so that you can use the methods and properties in this class to optimize your data storage and stream.
| compress | () | method |
public function compress():voidCompresses the byte array using zlib compression. The entire byte array is compressed.
See also
| readBoolean | () | method |
public function readBoolean():Boolean
Reads a Boolean value from the byte stream. A single byte is read,
and true is returned if the byte is nonzero,
false otherwise.
Boolean —
Returns true if the byte is nonzero, false otherwise.
|
EOFError — There is not sufficient data available
to read.
|
| readByte | () | method |
public function readByte():intReads a signed byte from the byte stream.
The returned value is in the range -128 to 127.
Returnsint —
An integer between -128 and 127.
|
EOFError — There is not sufficient data available
to read.
|
| readBytes | () | method |
public function readBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void
Reads the number of data bytes, specified by the length parameter, from the byte stream.
The bytes are read into the ByteArray object specified by the bytes parameter, starting
at the position specified by offset.
bytes:ByteArray — The ByteArray object to read
data into.
|
|
offset:uint (default = 0) — The offset into bytes at which the data
read should begin.
|
|
length:uint (default = 0) — The number of bytes to read. The default value
of 0 causes all available data to be read.
|
EOFError — There is not sufficient data available
to read.
|
| readDouble | () | method |
public function readDouble():NumberReads an IEEE 754 double-precision (64-bit) floating-point number from the byte stream.
ReturnsNumber —
A double-precision (64-bit) floating-point number.
|
EOFError — There is not sufficient data available
to read.
|
| readFloat | () | method |
public function readFloat():NumberReads an IEEE 754 single-precision (32-bit) floating-point number from the byte stream.
ReturnsNumber —
A single-precision (32-bit) floating-point number.
|
EOFError — There is not sufficient data available
to read.
|
| readInt | () | method |
public function readInt():intReads a signed 32-bit integer from the byte stream.
The returned value is in the range -2147483648 to 2147483647.
Returnsint —
A 32-bit signed integer between -2147483648 and 2147483647.
|
EOFError — There is not sufficient data available
to read.
|
| readMultiByte | () | method |
public function readMultiByte(length:uint, charSet:String):StringReads a multibyte string of specified length from the byte stream using the specified character set.
Parameterslength:uint — The number of bytes from the byte stream to read.
|
|
charSet:String — The string denoting the character set to use to interpret the bytes.
Possible character set strings include "shift-jis", "cn-gb",
"iso-8859-1", and others.
For a complete list, see Supported Character Sets.
|
String —
UTF-8 encoded string.
|
EOFError — There is not sufficient data available
to read.
|
| readObject | () | method |
public function readObject():*Reads an object from the byte array, encoded in AMF serialized format.
Returns* — The deserialized object.
|
EOFError — There is not sufficient data available
to read.
|
| readShort | () | method |
public function readShort():intReads a signed 16-bit integer from the byte stream.
The returned value is in the range -32768 to 32767.
Returnsint —
A 16-bit signed integer between -32768 and 32767.
|
EOFError — There is not sufficient data available
to read.
|
| readUnsignedByte | () | method |
public function readUnsignedByte():uintReads an unsigned byte from the byte stream.
The returned value is in the range 0 to 255.
Returnsuint —
A 32-bit unsigned integer between 0 and 255.
|
EOFError — There is not sufficient data available
to read.
|
| readUnsignedInt | () | method |
public function readUnsignedInt():uintReads an unsigned 32-bit integer from the byte stream.
The returned value is in the range 0 to 4294967295.
Returnsuint —
A 32-bit unsigned integer between 0 and 4294967295.
|
EOFError — There is not sufficient data available
to read.
|
| readUnsignedShort | () | method |
public function readUnsignedShort():uintReads an unsigned 16-bit integer from the byte stream.
The returned value is in the range 0 to 65535.
Returnsuint —
A 16-bit unsigned integer between 0 and 65535.
|
EOFError — There is not sufficient data available
to read.
|
| readUTF | () | method |
public function readUTF():StringReads a UTF-8 string from the byte stream. The string is assumed to be prefixed with an unsigned short indicating the length in bytes.
ReturnsString —
UTF-8 encoded string.
|
EOFError — There is not sufficient data available
to read.
|
See also
| readUTFBytes | () | method |
public function readUTFBytes(length:uint):String
Reads a sequence of UTF-8 bytes specified by the length
parameter from the byte stream and returns a string.
length:uint — An unsigned short indicating the length of the UTF-8 bytes.
|
String —
A string composed of the UTF-8 bytes of the specified length.
|
EOFError — There is not sufficient data available
to read.
|
| toString | () | method |
public function toString():String
Converts the byte array to a string.
If the data in the array begins with a Unicode byte order mark, Flash will honor that mark
when converting to a string. If System.useCodePage is set to true, the player will treat
the data in the array as being in the current system code page when converting.
String —
The string representation of the byte array.
|
| uncompress | () | method |
public function uncompress():void
Decompresses the byte array. The byte array
must have been previously compressed with the
compress() method.
See also
| writeBoolean | () | method |
public function writeBoolean(value:Boolean):void
Writes a Boolean value. A single byte is written according to the value parameter,
either 1 if true or 0 if false.
value:Boolean — A Boolean value determining which byte is written. If the parameter is true,
Flash Player writes a 1; if false, Flash Player writes a 0.
|
| writeByte | () | method |
public function writeByte(value:int):voidWrites a byte to the byte stream.
The low 8 bits of the parameter are used. The high 24 bits are ignored.
Parametersvalue:int — A 32-bit integer. The low 8 bits are written to the byte stream.
|
| writeBytes | () | method |
public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void
Writes a sequence of length bytes from the
specified byte array, bytes,
starting offset (zero-based index) bytes
into the byte stream.
If the length parameter is omitted, the default
length of 0 is used; Flash Player writes the entire buffer starting at
offset.
If the offset parameter is also omitted, the entire buffer is
written.
If offset or length
is out of range, they are clamped to the beginning and end
of the bytes array.
bytes:ByteArray — The ByteArray object.
|
|
offset:uint (default = 0) — A zero-based index indicating the position into the array to begin writing.
|
|
length:uint (default = 0) — An unsigned integer indicating how far into the buffer to write.
|
| writeDouble | () | method |
public function writeDouble(value:Number):voidWrites an IEEE 754 double-precision (64-bit) floating-point number to the byte stream.
Parametersvalue:Number — A double-precision (64-bit) floating-point number.
|
| writeFloat | () | method |
public function writeFloat(value:Number):voidWrites an IEEE 754 single-precision (32-bit) floating-point number to the byte stream.
Parametersvalue:Number — A single-precision (32-bit) floating-point number.
|
| writeInt | () | method |
public function writeInt(value:int):voidWrites a 32-bit signed integer to the byte stream.
Parametersvalue:int — An integer to write to the byte stream.
|
| writeMultiByte | () | method |
public function writeMultiByte(value:String, charSet:String):voidWrites a multibyte string to the byte stream using the specified character set.
Parametersvalue:String — The string value to be written.
|
|
charSet:String — The string denoting the character set to use. Possible character set strings
include "shift-jis", "cn-gb", "iso-8859-1", and others.
For a complete list, see Supported Character Sets.
|
| writeObject | () | method |
public function writeObject(object:*):voidWrites an object into the byte array in AMF serialized format.
Parametersobject:* — The object to serialize.
|
| writeShort | () | method |
public function writeShort(value:int):voidWrites a 16-bit integer to the byte stream. The low 16 bits of the parameter are used. The high 16 bits are ignored.
Parametersvalue:int — 32-bit integer, whose low 16 bits are written to the byte stream.
|
| writeUnsignedInt | () | method |
public function writeUnsignedInt(value:uint):voidWrites a 32-bit unsigned integer to the byte stream.
Parametersvalue:uint — An unsigned integer to write to the byte stream.
|
| writeUTF | () | method |
public function writeUTF(value:String):voidWrites a UTF-8 string to the byte stream. The length of the UTF-8 string in bytes is written first, as a 16-bit integer, followed by the bytes representing the characters of the string.
Parametersvalue:String — The string value to be written.
|
RangeError — If the length is larger than
65535.
|
| writeUTFBytes | () | method |
public function writeUTFBytes(value:String):void
Writes a UTF-8 string to the byte stream. Similar to the writeUTF() method,
but writeUTFBytes() does not prefix the string with a 16-bit length word.
value:String — The string value to be written.
|
ByteArrayExample to write a Boolean
and the double-precision floating-point representation of pi to a byte array. This is accomplished
using the following steps:
byteArr.false and then check the length and
read it back.Note: when trace() is called on a byte, it prints the decimal equivalent
of the bytes stored in the byte array.
Notice how a code segment is added at the end to check for end of file errors to ensure that the byte stream is not read past its end.
package {
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.errors.EOFError;
public class ByteArrayExample extends Sprite {
public function ByteArrayExample() {
var byteArr:ByteArray = new ByteArray();
byteArr.writeBoolean(false);
trace(byteArr.length); // 1
trace(byteArr[0]); // 0
byteArr.writeDouble(Math.PI);
trace(byteArr.length); // 9
trace(byteArr[0]); // 0
trace(byteArr[1]); // 64
trace(byteArr[2]); // 9
trace(byteArr[3]); // 33
trace(byteArr[4]); // 251
trace(byteArr[5]); // 84
trace(byteArr[6]); // 68
trace(byteArr[7]); // 45
trace(byteArr[8]); // 24
byteArr.position = 0;
try {
trace(byteArr.readBoolean() == false); // true
}
catch(e:EOFError) {
trace(e); // EOFError: Error #2030: End of file was encountered.
}
try {
trace(byteArr.readDouble()); // 3.141592653589793
}
catch(e:EOFError) {
trace(e); // EOFError: Error #2030: End of file was encountered.
}
try {
trace(byteArr.readDouble());
}
catch(e:EOFError) {
trace(e); // EOFError: Error #2030: End of file was encountered.
}
}
}
}
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flex/201/langref/flash/utils/ByteArray.html