United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4715166 : (bf) ByteBuffer.duplicate() does not preserve byte ordering

Details
Type:
Enhancement
Submit Date:
2002-07-16
Status:
Open
Updated Date:
2009-04-17
Project Name:
JDK
Resolved Date:
Component:
core-libs
OS:
windows_2000
Sub-Component:
java.nio
CPU:
x86
Priority:
P4
Resolution:
Unresolved
Affected Versions:
1.4.0
Targeted Versions:

Related Reports

Sub Tasks

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
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.
                                     
2007-01-03



Hardware and Software, Engineered to Work Together