简体   繁体   English

是否可以使用ByteBuffer类将强类型数据转换为字节?

[英]Is it possible to convert strongly-typed data to bytes using the ByteBuffer class?

Is it possible to convert strongly-typed data to bytes using the ByteBuffer class? 是否可以使用ByteBuffer类将强类型数据转换为字节? 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. put方法需要一个字节,而不是一个整数示例。

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 =方便地将“所有”数据类型读取/写入字节表示形式:

ByteBuffer is a very convenient Class to convert Bytes from types "like Int" to a Byte representation. ByteBuffer是一个非常方便的类,用于将Bytes从“ Int”类型转换为Byte表示形式。 But also the other way round. 但反过来说。 You can read "types" from a byte representation with the read methods. 您可以使用read方法从字节表示形式读取“类型”。

Regarding your put Question: There is a generic put method that accepts a byte. 关于您的put问题:有一个通用的put方法可以接受一个字节。 But also a lot of convenience methods to put and read most of the standard datatypes, also int (as you asked): 但是,还有很多方便的方法可以放入和读取大多数标准数据类型,也可以读取和读取int(如您所问):

http://download.oracle.com/javase/1,5.0/docs/api/java/nio/ByteBuffer.html#putInt(int ) http://download.oracle.com/javase/1,5.0/docs/api/java/nio/ByteBuffer.html#putInt(int

ByteBuffer byteBuffer =  ByteBuffer.allocate(4);
byteBuffer.putInt(4);
byteBuffer.get() => gets the byte representation of the Int you put in 

Practical Use Case: 实际用例:

To be honest, the most heaviest use of ByteBuffer for me has been so far with Cassandra NoSQL DB. 坦白地说,到目前为止,对我而言,最大的ByteBuffer使用是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. 它们将所有数据存储为字节(数组),而ByteBuffer是方便的类,可帮助您读取和写入这些数据。

High-End Usage: 高端用途:

As you can see the class is in NIO Package. 如您所见,该类在NIO包中。 I think the origin of ByteBuffer was to very very efficiently write Data for example to disk. 我认为ByteBuffer的起源是非常非常有效地将数据写入磁盘。 It does not write it in memory before but directly maps a region of "blocks" on disk to the buffer in java. 它之前不会将其写入内存,而是直接将磁盘上的“块”区域映射到Java中的缓冲区。 So it avoid any intermediate steps for reading and writing data to disk. 因此,它避免了将数据读取和写入磁盘的任何中间步骤。 But this is very highend usage... 但这是非常高端的用法...

Erm, you mean like ... 嗯,你的意思是像...

String foo = "My String";
ByteBuffer myBuffer = ByteBuffer.wrap(foo.getBytes());

EDIT: For an int you can do... 编辑:对于int您可以...

int i = 1234;
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(i);

Well, you can serialize your objects if they implement serializable and store the serialized bytes in a ByteBuffer if you really want to. 好吧,如果对象实现可序列化,则可以对其进行序列serializable ,如果确实需要,可以将序列化的字节存储在ByteBuffer中。

The main purpose of ByteBuffer appears to be buffering bytes, so that asynchronous transfers can be sped up. ByteBuffer的主要用途似乎是缓冲字节,因此可以加快异步传输的速度。

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. 似乎您正在考虑获取对象所驻留的RAM块,并将该块放入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. 这正在序列化,这就是可serializable作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM