NIOを使用するだけです。これは、この特定の目的のために設計されています。ByteBufferとIntBufferは、必要なことをすばやく、効率的に、そしてエレガントに実行します。ビッグ/リトルエンディアン変換、高性能IO用の「直接」バッファーを処理し、データ型をバイトバッファーに混在させることもできます。
整数をバイトに変換します。
ByteBuffer bbuffer = ByteBuffer.allocate(4*theIntArray.length);
IntBuffer ibuffer = bbuffer.asIntBuffer(); //wrapper--doesn't allocate more memory
ibuffer.put(theIntArray); //add your int's here; can use
//array if you want
byte[] rawBytes = bbuffer.array(); //returns array backed by bbuffer--
//i.e. *doesn't* allocate more memory
バイトを整数に変換します。
ByteBuffer bbuffer = ByteBuffer.wrap(rawBytes);
IntBuffer ibuffer = bbuffer.asIntBuffer();
while(ibuffer.hasRemaining())
System.out.println(ibuffer.get()); //also has bulk operators