是否可以使用 ByteBuffer 类将强类型数据转换为字节?
是否可以使用 ByteBuffer 类将强类型数据转换为字节?如果不是,其主要目的是什么?如果是,我正在查看其文档 却什么也找不到。例如,put
方法需要一个字节而不是 int。
Is it possible to convert strongly-typed data to bytes using the ByteBuffer
class? If not, what is its main purpose? If yes, I am looking at its documentation and can find nothing. The put
methods require a byte and not an int fro example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能知道,在计算机编程中,大多数数据都以字节格式存储在较低级别上。
ByteBuffer =方便地从字节表示形式读取/写入“所有”数据类型:
ByteBuffer 是一个非常方便的类,可将字节从“如 Int”类型转换为字节表示形式。但反过来也是如此。您可以使用读取方法从字节表示中读取“类型”。
关于您的 put 问题:有一个接受字节的通用 put 方法。而且还有很多方便的方法来放置和读取大多数标准数据类型,还有 int (如您所问):
http://download.oracle.com/javase/1,5.0/docs/api/java/nio/ByteBuffer.html#putInt(int)
实际用例:< /strong>
老实说,到目前为止,对我来说 ByteBuffer 最常用的是 Cassandra NoSQL DB,它们将所有数据存储为字节(数组),而 ByteBuffer 是一个方便的类,可以帮助您读取和写入这些数据。 正如
高端用法:
您所看到的,该类位于 NIO 包中,我认为 ByteBuffer 的起源是非常非常有效地将数据写入磁盘,例如它不会将其写入内存。之前而是直接将磁盘上的“块”区域映射到java中的缓冲区,因此它避免了将数据读取和写入磁盘的任何中间步骤。
as you might know, in computer programming most of the Data is somehow stored on the lower levels in Byte Format.
ByteBuffer =Convenient Read/Write of "all" Data Types to/from a Byte-Representation:
ByteBuffer is a very convenient Class to convert Bytes from types "like Int" to a Byte representation. But also the other way round. You can read "types" from a byte representation with the read methods.
Regarding your put Question: There is a generic put method that accepts a byte. But also a lot of convenience methods to put and read most of the standard datatypes, also int (as you asked):
http://download.oracle.com/javase/1,5.0/docs/api/java/nio/ByteBuffer.html#putInt(int)
Practical Use Case:
To be honest, the most heaviest use of ByteBuffer for me has been so far with Cassandra NoSQL DB. They store all the Data as byte(arrays) and ByteBuffer is a convenient Class to help to yout read and write those data.
High-End Usage:
As you can see the class is in NIO Package. I think the origin of ByteBuffer was to very very efficiently write Data for example to disk. It does not write it in memory before but directly maps a region of "blocks" on disk to the buffer in java. So it avoid any intermediate steps for reading and writing data to disk. But this is very highend usage...
呃,你的意思是......
编辑: 对于
int
你可以做......Erm, you mean like ...
EDIT: For an
int
you can do...好吧,如果您确实想要的话,如果对象实现了可序列化并将序列化的字节存储在 ByteBuffer 中,您就可以序列化它们。
ByteBuffer 的主要目的似乎是缓冲字节,以便加快异步传输速度。
您似乎正在考虑获取对象所在的 RAM 块并将该块放入 ByteBuffer 中。这行不通。我的意思是,你可以做到,但祝你好运,让对象恢复到可用状态。您必须对其进行处理,例如将指针转换为有意义的新地址(指存储对象实际字段或其他内容的位置)。这就是序列化,这就是
serializable
的作用。Well, you can serialize your objects if they implement
serializable
and store the serialized bytes in aByteBuffer
if you really want to.The main purpose of ByteBuffer appears to be buffering bytes, so that asynchronous transfers can be sped up.
It seems to be that you're thinking of getting the chunk of RAM where the object resides and placing that chunk into a ByteBuffer. This won't work. I mean, you could do it, but good luck getting the object back into a usable state. You have to do work on it, like turn the pointers into meaningful new addresses (referring to the places where you stored the actual fields of the object or whatnot). This is serializing, and this is what
serializable
does.