简体   繁体   English

将int []转换为byte []而不创建新对象

[英]converting an int[] to byte[] without creating new objects

I have an int[] in java which i want to convert to a byte[]. 我在java中有一个int [],我想转换为byte []。

Now the usual way to do this would be to create a new byte[] 4 times the size of the int array and copy all the ints byte by byte into the new byte array. 现在通常的做法是创建一个新的byte [] 4倍大小的int数组,并将所有的int字节逐字节复制到新的字节数组中。

However the only reason to do this is because of java's type safety rules. 但是,这样做的唯一原因是因为java的类型安全规则。 An int array is already a byte array. int数组已经是一个字节数组。 Its just that java doesnt allow casting an int[] to a byte[] and then using it as a byte[]. 它只是java不允许将int []转换为byte []然后将其用作byte []。

Is there any way, maybe using jni, to make an int array look like a byte array to java ? 有没有办法,也许使用jni,使一个int数组看起来像java的字节数组?

No. There isn't a capability to implement an object with a native Java array interface. 不可以。没有能力使用本机Java阵列接口实现对象。

It sounds to me like you want an object wrapping your int[], and present methods to access it in a byte-array-wise fashion. 听起来我想要一个包装int []的对象,并提供以字节数组方式访问它的方法。 eg 例如

public class ByteArrayWrapper {
   private int[] array;

   public int getLength() {
      return array.length * 4;
   }

   public byte get(final int index) {
      // index into the array here, find the int, and then the appropriate byte
      // via mod/div/shift type operations....
     int val = array[index / 4];
     return (byte)(val >> (8 * (index % 4)));
   }
}

(the above is not tested/compiled etc. and is dependent upon your byte-ordering requirements. It's purely illustrative ) (以上未经过测试/编译等,取决于您的字节排序要求。这纯粹是说明性的

Depending on your precise requirements, you may be able to use NIO's java.nio.ByteBuffer class. 根据您的精确要求,您可以使用NIO的java.nio.ByteBuffer类。 Do your initial allocation as a ByteBuffer, and use it's getInt and putInt methods to access int values. 将您的初始分配作为ByteBuffer进行,并使用它的getIntputInt方法来访问int值。 When you need to access the buffer in terms of bytes, you may use the get and put methods. 当您需要以字节为单位访问缓冲区时,可以使用getput方法。 ByteBuffer also has an asIntBuffer method which changes the default get and put behavior to int instead of byte. ByteBuffer还有一个asIntBuffer方法,它将默认的get和put行为更改为int而不是byte。

If you're using JNI, a directly allocated ByteBuffer (in some instances) permits direct pointer access in your C code. 如果您正在使用JNI,则直接分配的ByteBuffer(在某些情况下)允许在C代码中直接指针访问。

http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html http://java.sun.com/javase/6/docs/api/java/nio/ByteBuffer.html

For example, 例如,

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

// …

int[] intArray = { 1, 2, 3, 4 };

ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);        
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(intArray);

byte[] byteArray = byteBuffer.array();

If you really really had to, you could use external calls into C to do this, but I'm pretty sure it can't be done within the language. 如果你真的不得不这样做,你可以使用外部调用来做到这一点,但我很确定它不能在语言中完成。

I'm also really curious as to what the existing code looks like and how much extra speed you are expecting. 我也很好奇现有的代码是什么样的,以及你期望的额外速度。

You know the rules of optimization, right? 你知道优化规则吗?

  • 1) Don't optimize. 1)不要优化。
  • 2) If you are an expert, see rule 1. 2)如果您是专家,请参阅规则1。
  • 3) if you are an expert, have written the clearest code possible and have timing tests which are failing, then: 3)如果您是专家,尽可能编写最清晰的代码并进行失败的时序测试,那么:
  • 3a) write an optimized version and leave the unoptimized code as comments 3a)编写优化版本并将未优化的代码留作注释
  • 3b) retest, if your optimized code does not pass the speed tests, revert! 3b)重新测试,如果你的优化代码没有通过速度测试,还原!
  • 3c) explain exactly why you coded this way in comments. 3c)解释为什么你在评论中用这种方式编码。 Include your speed measurements and references to requirements. 包括您的速度测量和参考要求。
  • 3d) if rule 3 seems like too much work, see rule 1 3d)如果规则3似乎太多工作,请参阅规则1

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

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