JDK-4715166 : (bf) ByteBuffer.duplicate() does not preserve byte ordering
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-07-16
  • Updated: 2017-07-20
  • Resolved: 2017-07-20
Description
Name: nt126004			Date: 07/15/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
The byte ordering of a ByteBuffer created by the duplicate
method is always BIG_ENDIAN even if the original buffer was
LITTLE_ENDIAN. This is very surprising behaviour. The slice
() method also shows this behaviour.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the attached test code!
2.
3.

EXPECTED VERSUS ACTUAL BEHAVIOR :
I would expect the duplicate of a buffer with LittleEndian
byte ordering to also have litte endian ordering.
The actual output from my test is

original BIG_ENDIAN, duplicate BIG_ENDIAN
original LITTLE_ENDIAN, duplicate BIG_ENDIAN
original BIG_ENDIAN, duplicate BIG_ENDIAN
original LITTLE_ENDIAN, duplicate BIG_ENDIAN

The first pair relate to a heap allocated buffer, while the
second pair to a direct allocated buffer.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.nio.*;

class TestByteOrder
{
	public static void main(String[] args)
	{
		ByteBuffer a = ByteBuffer.allocate(1024);
		System.out.println("original "+a.order()+",duplicate "+a.duplicate().order());
		a.order(ByteOrder.LITTLE_ENDIAN);
		System.out.println("original "+a.order()+",duplicate "+a.duplicate().order());
		a = ByteBuffer.allocateDirect(1024);
		System.out.println("original "+a.order()+",duplicate "+a.duplicate().order());
		a.order(ByteOrder.LITTLE_ENDIAN);
		System.out.println("original "+a.order()+",duplicate "+a.duplicate().order());
	}
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Either always use BIG_ENDIAN byte ordering or set the
desired ordering after each duplicate or slice call.
(Review ID: 158553) 
======================================================================

Comments
The ByteBuffer javadoc was changed some time ago to specify that the byte order is big endian so I think we can close this issue.
20-07-2017

EVALUATION The spec for ByteBuffer.order(): /** * Retrieves this buffer's byte order. * * <p> The byte order is used when reading or writing multibyte values, and * when creating buffers that are views of this byte buffer. The order of * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN * BIG_ENDIAN}. * * @return This buffer's byte order */ Thus, the current behaviour is not a bug. We should probably add a duplicate(boolean) method which allows the programmer to indicate whether the byte order at the time of invocation should be preserved. We may also need to slightly modify the existing spec for the existing duplicate() and order() methods.
03-01-2007