tags:

views:

243

answers:

3

I have an int[] in java which i want to convert to a 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.

However the only reason to do this is because of java's type safety rules. An int array is already a byte array. Its just that java doesnt allow casting an int[] to a byte[] and then using it as a byte[].

Is there any way, maybe using jni, to make an int array look like a byte array to java ?

+8  A: 

No. There isn't a capability to implement an object with a native Java array interface.

It sounds to me like you want an object wrapping your int[], and present methods to access it in a byte-array-wise fashion. e.g.

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)

Brian Agnew
I don't think it is possible to define a class that presents a full byte[] interface, including indexing and passing it in places where byte[] is expected. So I think the OP will most likely have to create a copy.
Martin v. Löwis
Martin is correct. This class cannot be used in cases where a byte[] needs to be passed.
pdeva
@pdeva - noted as above. That's why my answer started 'No' :-) I did *further* highlight this issue following Martin's comment. I'm not sure how much clearer I can be, other than just answering 'No'.
Brian Agnew
A: 

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.
  • 2) If you are an expert, see rule 1.
  • 3) if you are an expert, have written the clearest code possible and have timing tests which are failing, then:
  • 3a) write an optimized version and leave the unoptimized code as comments
  • 3b) retest, if your optimized code does not pass the speed tests, revert!
  • 3c) explain exactly why you coded this way in comments. Include your speed measurements and references to requirements.
  • 3d) if rule 3 seems like too much work, see rule 1
Bill K
A Minus? Really? Because you couldn't use C calls to map it (by creating a new pointer to the same memory space and returning it?) or because someone doesn't comprehend the importance of not optimizing?
Bill K
+1  A: 

Depending on your precise requirements, you may be able to use NIO's java.nio.ByteBuffer class. Do your initial allocation as a ByteBuffer, and use it's getInt and putInt methods to access int values. When you need to access the buffer in terms of bytes, you may use the get and put methods. ByteBuffer also has an asIntBuffer method which changes the default get and put behavior to int instead of byte.

If you're using JNI, a directly allocated ByteBuffer (in some instances) permits direct pointer access in your C code.

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

BigZig