简体   繁体   English

java-将字节数组转换为类?

[英]java - Convert a byte array to a class?

In C/C++, you can do the following: 在C / C ++中,您可以执行以下操作:

struct DataStructure
{
  char member1;
  char member2;
};

DataStructure ds;
char bytes[] = {0xFF, 0xFE};

memcpy(&ds, bytes, sizeof(ds));

and you would essentially get the following: 并且您基本上将获得以下内容:

ds.member1 = 0xFF;
ds.member2 = 0xFE;

What is the Java equivalent? Java等效项是什么?

What is the Java equivalent? Java等效项是什么?

There is no Java equivalent. 没有等效的Java。

Java does not allow you to create or modify objects by accessing them at that level. Java不允许您通过在该级别访问对象来创建或修改对象。 You should be using new or setter methods, depending on what you are trying to achieve. 您应该使用new方法或setter方法,具体取决于要实现的目标。

(There are a couple of ways to do this kind of thing, but they are unsafe, non-portable and "not Java" ... and they are not warranted in this situation.) (有两种方法可以执行这种操作,但是它们不安全,不可移植且“非Java”……并且在这种情况下不做保证。)

The memcpy you wrote depends on the internal implementation of the struct and would not necessarily work. 您编写的memcpy取决于该结构的内部实现,不一定有效。 In java, you need to define a constructor that accepts a byte array and set the fields. 在Java中,您需要定义一个接受字节数组的构造函数并设置字段。 No shortcuts like this, as the memory structure of the class is not defined. 没有这样的快捷方式,因为未定义该类的内存结构。

In Java you cannot work with the memory directly (no memcpy ) it is the advantage (disadvantage?) of Java. 在Java中, 您不能直接使用内存 (没有memcpy ),这是Java的优势(劣势?)。 There are some java library methods to copy arrays: System.arraycopy() . 有一些Java库方法可以复制数组: System.arraycopy() In general, to copy some object you need to ship it with clone method. 通常,要复制某些对象,您需要使用克隆方法将其运送。

You might be able to do that in C. But you'd be wandering into aliasing problems and a hunka hunka burning undefined behavior. 您也许可以在C语言中做到这一点。但是,您将徘徊于混叠问题和不明确的行为中。

And because struct padding is up to a compiler, what you might get with your memcpy is just ds.member1 = 0xFF, ds.member2 = whatever junk happened to be on the stack at the time, because member1 was padded to occupy 4 bytes rather than just 1. Or maybe you get junk for both, because you set the top 2 bytes of a 4-byte and they're in the bottom 2 bytes. 而且由于结构填充取决于编译器,因此使用memcpy可能会得到的只是ds.member1 = 0xFF,ds.member2 =当时堆栈上发生的任何垃圾,因为member1被填充以占用4个字节,而不是而不是1。或者您可能都为这两个垃圾,因为您将4字节的高2字节设置为最低2字节。

What you're wandering into is compiler/runtime-specific memory layouts. 您要进入的是编译器/运行时特定的内存布局。 The same is true in Java. 在Java中也是如此。 Java itself won't let you do something so horrendously un-Java, but if you write your own JVM or debug an existing JVM written in C or C++, you could do something like that. Java本身不允许您使用非Java来做如此可怕的事情,但是如果您编写自己的JVM或调试用C或C ++编写的现有JVM,则可以执行类似的操作。 And who knows what would happen; 谁知道会发生什么呢? I'm not Java god enough to know exactly how much the JVM spec pins down JVM implementation, but my guess is, not to the degree necessary to enable interoperability of the in-memory, runtime representations of objects. 我不是Java的神,不足以确切地知道JVM规范对JVM实现有多大的限制,但是我的猜测是,对于实现对象的内存中运行时表示形式的互操作性而言,程度不够。

So you get undefined behavior in every language flavor. 因此,每种语言都会出现不确定的行为。 Tastes just as good in each language, too - like mystery meat. 每种语言的味道也一样-就像神秘的肉一样。

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

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