This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: java.nio.ByteBuffer problem.


On Friday, Sep 19, 2003, at 09:24 Pacific/Auckland, Rutger Ovidius wrote:

Is this supported with gcj? All I get are 0's.

gcc version 3.3.1 (mingw special 20030804-1)

I tried this example with the current CVS libgcj. It gets more than 0's, but the result is incorrect as the current ByteBuffer implementation only supports ByteOrder.BIG_ENDIAN (ie Java byte order) - note the comments in ByteBufferImpl.java which claim it only supports LITTLE_ENDIAN are incorrect.


It wouldn't be too hard to go through and add cases to the existing code to handle little endian as well, but I'm thinking there would be a lot to be gained by optimizing much of the buffer stuff, such as ByteBuffer.putInt() etc, with CNI methods. Anyone else want to comment on this? Michael?

Regards

Bryce.



import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {

public static void main (String [] args) {

	ByteBuffer bb = ByteBuffer.allocate(4);
	bb.order( ByteOrder.LITTLE_ENDIAN );
	bb.putInt(1111);  // any #..

	System.out.println(toHexString(bb.array()));
}

public static String toHexString ( byte[] b )
{
	StringBuffer sb = new StringBuffer( b.length * 2 );
	for ( int i=0 ; i<b.length ; i++ )
	{
	sb.append( hexChar [ ( b[ i] & 0xf0 ) >>> 4 ] ) ;
	sb.append( hexChar [ b[ i] & 0x0f ] ) ;
	}
	return sb.toString() ;
}

static char[] hexChar =
{
'0' , '1' , '2' , '3' ,
'4' , '5' , '6' , '7' ,
'8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' }
;

}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]