| 套件 | flash.utils |
| 介面 | public interface IDataOutput |
| 實作者 | ByteArray, FileStream, Socket |
| 語言版本: | ActionScript 3.0 |
| 執行階段版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
根據預設,所有的 IDataInput 與 IDataOutput 作業都是 "bigEndian" (序列中的最高位元組儲存在最低或第一個儲存位址),而且是非封鎖的。
正負號擴充只在讀取資料時有影響,在寫入資料時則不重要。 因此,在使用 IDataInput.readUnsignedByte() 與 IDataInput.readUnsignedShort() 時,並不需要分隔寫入方法。 換句話說:
- 使用
IDataOutput.writeByte()時,請搭配使用IDataInput.readUnsignedByte()與IDataInput.readByte()。 - 使用
IDataOutput.writeShort()時,請搭配使用IDataInput.readUnsignedShort()與IDataInput.readShort()。
相關 API 元素
公用屬性
| 屬性 | 定義自 | ||
|---|---|---|---|
| endian : String
資料的位元組順序,會是 Endian 類別的 BIG_ENDIAN 或 LITTLE_ENDIAN 常數。 | IDataOutput | ||
| objectEncoding : uint
使用 writeObject() 方法來寫入或讀取二進位資料時,用來判斷使用的是 AMF3 或 AMF0 格式。 | IDataOutput | ||
公用方法
| 方法 | 定義自 | ||
|---|---|---|---|
會寫入 Boolean 值。 | IDataOutput | ||
會寫入位元組。 | IDataOutput | ||
以 length 指定的長度,從指定的位元組陣列 bytes,將位元組序列從 offset 所指定的位元組 (使用從零開始的索引) 開始寫入檔案串流、位元組串流或位元組陣列。 | IDataOutput | ||
會寫入 IEEE 754 雙精度 (64 位元) 浮點數。 | IDataOutput | ||
會寫入 IEEE 754 單精度 (32 位元) 浮點數。 | IDataOutput | ||
會寫入 32 位元的具有正負號整數。 | IDataOutput | ||
使用指定的字元集,將多位元組字串寫入檔案串流、位元組串流或位元組陣列。 | IDataOutput | ||
使用 AMF 序列化格式,將物件寫入檔案串流、位元組串流或位元組陣列。 | IDataOutput | ||
會寫入 16 位元整數。 | IDataOutput | ||
會寫入 32 位元的無正負號整數。 | IDataOutput | ||
將 UTF-8 字串寫入檔案串流、位元組串流或位元組陣列。 | IDataOutput | ||
會寫入 UTF-8 字串。 | IDataOutput | ||
屬性詳細資訊
endian | 屬性 |
objectEncoding | 屬性 |
方法詳細資訊
writeBoolean | () | 方法 |
writeByte | () | 方法 |
writeBytes | () | 方法 |
public function writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0):void| 語言版本: | ActionScript 3.0 |
| 執行階段版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
以 length 指定的長度,從指定的位元組陣列 bytes,將位元組序列從 offset 所指定的位元組 (使用從零開始的索引) 開始寫入檔案串流、位元組串流或位元組陣列。
如果省略 length 參數,便會使用 0 的預設長度,並從 offset 位置開始寫入整個緩衝區。如果也省略 offset 參數,便會寫入整個緩衝區。
如果 offset 或 length 參數超出範圍,這兩者便會固定至 bytes 陣列的開頭與結尾。
參數
bytes:ByteArray — 要寫入的位元組陣列。
| |
offset:uint (default = 0) — 從零開始的索引,指定要在陣列中開始寫入的位置。
| |
length:uint (default = 0) — 無正負號的整數,指定要在緩衝區中寫入的長度。
|
writeDouble | () | 方法 |
writeFloat | () | 方法 |
writeInt | () | 方法 |
writeMultiByte | () | 方法 |
writeObject | () | 方法 |
writeShort | () | 方法 |
writeUnsignedInt | () | 方法 |
writeUTF | () | 方法 |
public function writeUTF(value:String):void| 語言版本: | ActionScript 3.0 |
| 執行階段版本: | AIR 1.0, Flash Player 9, Flash Lite 4 |
將 UTF-8 字串寫入檔案串流、位元組串流或位元組陣列。會先寫入 UTF-8 字串的長度 (以位元組為單位,16 位元整數),後面緊接著表示字串之字元的位元組。
參數
value:String — 要寫入的字串值。
|
擲回值
RangeError — 如果長度大於 65535 的話。
|
writeUTFBytes | () | 方法 |
範例 如何使用本範例
DataOutputExample.as
下列範例會使用
DataOutputExample 類別,將 Boolean 和 pi 的雙精度浮點表示法寫入到位元組陣列。 這是透過下列步驟完成:
- 宣告新的 ByteArray 物件實體
byteArr。 - 寫入 Boolean
false的位元組同等值以及 pi 的同等雙精度浮點數學值。 - 讀回 Boolean 值及雙精度浮點數。
請注意程式碼區段會如何加入到結尾來檢查是否有檔案結尾錯誤,以確保不會讀取超過位元組串流結尾的資料。
package {
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.errors.EOFError;
public class DataOutputExample extends Sprite {
public function DataOutputExample() {
var byteArr:ByteArray = new ByteArray();
byteArr.writeBoolean(false);
byteArr.writeDouble(Math.PI);
byteArr.position = 0;
try {
trace(byteArr.readBoolean()); // false
}
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.
}
}
}
}
Tue Jun 12 2018, 03:47 PM Z