-
Enhancement
-
Resolution: Fixed
-
P4
-
None
-
b74
-
x86_64
-
windows_7
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8136179 | emb-9 | Brian Burkhalter | P4 | Resolved | Fixed | team |
A DESCRIPTION OF THE REQUEST :
The slice() method on bytebuffer is meant to retain existing state. However, it loses the byteOrder and the sliced bytebuffer always comes back as BIG_ENDIAN. Here's an example:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864
}
JUSTIFICATION :
The API is unclear, callers are expected to re-set the byte order on every slice. I believe this makes it almost a bug rather than a feature request.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
byteBuffer.slice() retains the byteOrder, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 4
}
ACTUAL -
byteBuffer.slice() does not retain the byteOrder, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864
}
---------- BEGIN SOURCE ----------
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864 - should return 4.
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The caller needs to re-set the byteorder manually, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
bar.order(foo.order()); //Re-set byteorder to foo
System.out.println(bar.getInt(0)); //returns 4
}
The slice() method on bytebuffer is meant to retain existing state. However, it loses the byteOrder and the sliced bytebuffer always comes back as BIG_ENDIAN. Here's an example:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864
}
JUSTIFICATION :
The API is unclear, callers are expected to re-set the byte order on every slice. I believe this makes it almost a bug rather than a feature request.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
byteBuffer.slice() retains the byteOrder, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 4
}
ACTUAL -
byteBuffer.slice() does not retain the byteOrder, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864
}
---------- BEGIN SOURCE ----------
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
System.out.println(bar.getInt(0)); //returns 67108864 - should return 4.
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
The caller needs to re-set the byteorder manually, like:
public static void main(String[] args) {
ByteBuffer foo = ByteBuffer.wrap(new byte[]{4, 0, 0, 0});
foo.order(LITTLE_ENDIAN);
System.out.println(foo.getInt(0)); //returns 4
ByteBuffer bar = foo.slice();
bar.order(foo.order()); //Re-set byteorder to foo
System.out.println(bar.getInt(0)); //returns 4
}
- backported by
-
JDK-8136179 (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
-
- Resolved
-
- duplicates
-
JDK-7178109 ByteBuffer does not retain its Endian order when getting a read-only copy
-
- Closed
-
- relates to
-
JDK-8132266 (bf spec) No appropriate CCC requests for the spec changes introduced by JDK-8065570
-
- Closed
-