Attualmente ho un ByteBuffer Java che ha già i dati in formato Big Endian. Quindi voglio scrivere su un file binario come Little Endian.Come scrivere big endian ByteBuffer su little endian in Java
Ecco il codice che scrive solo il file ancora in Big Endian:
public void writeBinFile(String fileName, boolean append) throws FileNotFoundException, IOException
{
FileOutputStream outStream = null;
try
{
outStream = new FileOutputStream(fileName, append);
FileChannel out = outStream.getChannel();
byteBuff.position(byteBuff.capacity());
byteBuff.flip();
byteBuff.order(ByteOrder.LITTLE_ENDIAN);
out.write(byteBuff);
}
finally
{
if (outStream != null)
{
outStream.close();
}
}
}
Nota che byteBuff è un ByteBuffer che è stato riempito in formato Big Endian.
La mia ultima risorsa è un metodo di forza bruta per creare un altro buffer e impostare ByteBuffer su little endian e quindi leggere i valori "getInt" dal buffer originale (big endian) e "setInt" il valore per il little endian buffer. Immagino che ci sia un modo migliore ...
Avete provato i comuni Apache (http://commons.apache.org/io/api-release/org/apache/commons/io/EndianUtils.html) per quello? –
+1 per Apache Commons: non reinventare la ruota :) – BachT