何时在 AS3 的 ByteArray 中使用 writeUTF() 和 writeUTFBytes() [英] when to use writeUTF() and writeUTFBytes() in ByteArray of AS3

查看:22
本文介绍了何时在 AS3 的 ByteArray 中使用 writeUTF() 和 writeUTFBytes()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为自己创建文件格式,因此我正在为我的文件创建标题.要将已知长度的字符串写入 ByteArray,我应该使用哪种方法,writeUTF() 或 writeUTFBytes().

I am trying to create a file format for myself, so i was forming the header for my file. To write a known length string into a ByteArray, which method should i use, writeUTF() or writeUTFBytes().

从 Flex 3 语言参考中,它告诉我 writeUTF() 将字符串的长度放在前面并抛出 RangeError 而 writeUTFBytes() 则没有.

From the Flex 3 language ref, it tells me that writeUTF() prepends the length of the string and throws a RangeError whereas writeUTFBytes() does not.

任何建议将不胜感激.

推荐答案

两者的唯一区别在于 writeUTFBytes() 不会在消息前加上字符串的长度(The RangeError是因为 65535 是您可以存储在 16 位中的最大数字)

The only difference between the two is that writeUTFBytes() doesn't prepend the message with the length of the string (The RangeError is because 65535 is the highest number you can store in 16 bits)

您在何处使用另一种取决于您在做什么.例如,我在复制要压缩的 XML 对象时使用 writeUTFBytes().在这种情况下,我不关心字符串的长度,它会在代码中引入一些额外的东西.

Where you'd use one over the other depends on what you're doing. For example, I use writeUTFBytes() when copying a XML object over to be compressed. In this case, I don't care about the length of the string, and it'd introduce something extra to the code.

writeUTF() 如果您正在编写一个流/网络服务器,那么当您在消息前加上消息长度前缀时,您就知道要在另一端传输多少字节之前会很有用消息的结尾.例如,我有 200 字节的消息.我读取了长度(16 位整数),它告诉我消息是 100 个字节.我读了 100 个字节,我知道这是一条完整的消息.之后的一切都是另一条消息.如果消息长度显示消息为 300 字节,那么我知道我必须等待一段时间才能收到完整消息.

writeUTF() can be useful if you're writing a streaming/network server, where as you prefix the message length to the message, you know how many bytes to stream on the other end before the end of the message. e.g., I have 200 bytes worth of message. I read the length (16-bit integer), which tells me the message is 100 bytes. I read in 100 bytes and I know it's a complete message. Everything after is another message. If the message length said the message was 300 bytes, then I'd know I'd have to wait a bit before I have the full message.

这篇关于何时在 AS3 的 ByteArray 中使用 writeUTF() 和 writeUTFBytes()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆