0

I want to Export a ByteBuffer as Little Endian into a file, but when I read in the file again I have to read it as Big Endian to get the right values. How can I export a ByteBuffer in a way that I can read in the created file as Little Endian and get the right values?

    //In the ByteBuffer tgxImageData there are the bytes which I want to export and import again, the ByteBuffer is ordered as little endian


    //export ByteBuffer into File
    FileOutputStream fos = new FileOutputStream(outputfile);            
    tgxImageData.position(0);
    byte[] tgxImageDataByte = new byte[tgxImageData.limit()];
    tgxImageData.get(tgxImageDataByte);         
    fos.write(tgxImageDataByte);            
    fos.close();


    //import File into ByteBuffer
    FileInputStream fis2 = new FileInputStream(outputfile);     
    byte [] arr2 = new byte[(int)outputfile.length()];
    fis2.read(arr2);
    fis2.close();           
    ByteBuffer fileData2 = ByteBuffer.wrap(arr2);


    fileData2.order(ByteOrder.LITTLE_ENDIAN);           
    System.out.println(fileData2.getShort(0));          //Wrong output, but here should be right output
    fileData2.order(ByteOrder.BIG_ENDIAN);          
    System.out.println(fileData2.getShort(0));          //Right output, but here should be wrong output
Ephisray
  • 3
  • 5
  • Where is the part where you _write_ in little endian? I only see you writing a byte[]. Have the values written into the byte[] been written in little endian? That would be into tgxImageData, I guess? – Fildor Apr 18 '13 at 09:24

1 Answers1

-1

This is an example how to write shorts as little endian

    FileOutputStream out = new FileOutputStream("test");
    ByteBuffer bbf = ByteBuffer.allocate(4);
    bbf.order(ByteOrder.LITTLE_ENDIAN);
    bbf.putShort((short)1);
    bbf.putShort((short)2);
    out.write(bbf.array());
    out.close();

you need to do something like that in your code

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275