FULL PRODUCT VERSION :
$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu3)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
the ByteBuffer returned from ByteBuffer.asReadOnlyBuffer does not retain its order() attribute.
workaround: calling order(ByteOrder) on the resulting ByteBuffer
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see the source code for the test case.
basically, the two buffers should have produced the same int value.
IntBuffer buf1 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
IntBuffer buf2 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asIntBuffer();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
expected:
255
255
255
ACTUAL -
255
-16777216
255
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
/** Shows that a buffer doesn't retain its Endian property when copied into a read-only buffer
*
* @author Arnon Klein
*
*/
public class ReadonlyBufferBug {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f = File.createTempFile("readonly", "bug");
f.deleteOnExit();
DataOutputStream d = new DataOutputStream(new FileOutputStream(f));
d.write(new byte[] {-1,0,0,0});
d.close();
FileChannel channel = new RandomAccessFile(f.getAbsolutePath(), "rw").getChannel();
IntBuffer buf1 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
IntBuffer buf2 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asIntBuffer();
IntBuffer buf3 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
System.out.println(buf1.get());
System.out.println(buf2.get());
System.out.println(buf3.get());
}
}
---------- END SOURCE ----------
$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu3)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Linux 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
A DESCRIPTION OF THE PROBLEM :
the ByteBuffer returned from ByteBuffer.asReadOnlyBuffer does not retain its order() attribute.
workaround: calling order(ByteOrder) on the resulting ByteBuffer
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see the source code for the test case.
basically, the two buffers should have produced the same int value.
IntBuffer buf1 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
IntBuffer buf2 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asIntBuffer();
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
expected:
255
255
255
ACTUAL -
255
-16777216
255
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
/** Shows that a buffer doesn't retain its Endian property when copied into a read-only buffer
*
* @author Arnon Klein
*
*/
public class ReadonlyBufferBug {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File f = File.createTempFile("readonly", "bug");
f.deleteOnExit();
DataOutputStream d = new DataOutputStream(new FileOutputStream(f));
d.write(new byte[] {-1,0,0,0});
d.close();
FileChannel channel = new RandomAccessFile(f.getAbsolutePath(), "rw").getChannel();
IntBuffer buf1 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
IntBuffer buf2 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).order(ByteOrder.LITTLE_ENDIAN).asReadOnlyBuffer().asIntBuffer();
IntBuffer buf3 = channel.map(FileChannel.MapMode.READ_ONLY, 0, 4).asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
System.out.println(buf1.get());
System.out.println(buf2.get());
System.out.println(buf3.get());
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8065570 (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
-
- Closed
-