Kaydet (Commit) 9e207520 authored tarafından Robert Antoni Buj i Gelonch's avatar Robert Antoni Buj i Gelonch Kaydeden (comit) Noel Grandin

xmerge: use java.nio.ByteBuffer (JDK 1.4+)

Change-Id: I6072bb4699c9cd361e0cfe0a12b46cfc1abfe368
Reviewed-on: https://gerrit.libreoffice.org/11998Reviewed-by: 's avatarNoel Grandin <noelgrandin@gmail.com>
Tested-by: 's avatarNoel Grandin <noelgrandin@gmail.com>
üst 7d966b1d
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
package org.openoffice.xmerge.util; package org.openoffice.xmerge.util;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/** /**
* Helper class providing static methods to convert data to/from Network Byte * Helper class providing static methods to convert data to/from Network Byte
* Order (Big Endian). * Order (Big Endian).
...@@ -38,12 +41,8 @@ public class EndianConverter { ...@@ -38,12 +41,8 @@ public class EndianConverter {
* @return Two element {@code byte} array containing the converted value. * @return Two element {@code byte} array containing the converted value.
*/ */
public static byte[] writeShort (short value) { public static byte[] writeShort (short value) {
byte[] leShort = new byte[2]; return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN)
.putShort(value).array();
leShort[0] = (byte) value;
leShort[1] = (byte) (value >>> 8);
return leShort;
} }
/** /**
...@@ -54,14 +53,8 @@ public class EndianConverter { ...@@ -54,14 +53,8 @@ public class EndianConverter {
* @return Four element {@code byte} array containing the converted value. * @return Four element {@code byte} array containing the converted value.
*/ */
public static byte[] writeInt (int value) { public static byte[] writeInt (int value) {
byte[] leInt = new byte[4]; return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN)
.putInt(value).array();
leInt[0] = (byte) value;
leInt[1] = (byte) (value >>> 8);
leInt[2] = (byte) (value >>> 16);
leInt[3] = (byte) (value >>> 24);
return leInt;
} }
/** /**
...@@ -77,20 +70,8 @@ public class EndianConverter { ...@@ -77,20 +70,8 @@ public class EndianConverter {
* IEEE-754 float. * IEEE-754 float.
*/ */
public static byte[] writeDouble(double value) { public static byte[] writeDouble(double value) {
return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(
long myDouble = Double.doubleToLongBits(value); Double.doubleToLongBits(value)).array();
byte[] leDouble = new byte[8];
leDouble[0] = (byte) (myDouble >>> 0);
leDouble[1] = (byte) (myDouble >>> 8);
leDouble[2] = (byte) (myDouble >>> 16);
leDouble[3] = (byte) (myDouble >>> 24);
leDouble[4] = (byte) (myDouble >>> 32);
leDouble[5] = (byte) (myDouble >>> 40);
leDouble[6] = (byte) (myDouble >>> 48);
leDouble[7] = (byte) (myDouble >>> 56);
return leDouble;
} }
/** /**
...@@ -106,12 +87,8 @@ public class EndianConverter { ...@@ -106,12 +87,8 @@ public class EndianConverter {
* @return {@code short} containing the converted value. * @return {@code short} containing the converted value.
*/ */
public static short readShort (byte[] value) { public static short readShort (byte[] value) {
int high, low; return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN)
.put(value).getShort(0);
high = value[1] & 0xFF;
low = value[0] & 0xFF;
return (short)(high << 8 | low);
} }
/** /**
...@@ -127,13 +104,8 @@ public class EndianConverter { ...@@ -127,13 +104,8 @@ public class EndianConverter {
* @return {@code int} containing the converted value. * @return {@code int} containing the converted value.
*/ */
public static int readInt(byte[] value) { public static int readInt(byte[] value) {
int number = 0; return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN)
.put(value).getInt(0);
for (int i = 0; i < 4; i++) {
number |= (value[i] & 0xFF) << ( i * 8);
}
return number;
} }
/** /**
...@@ -149,16 +121,8 @@ public class EndianConverter { ...@@ -149,16 +121,8 @@ public class EndianConverter {
* @return {@code double} containing the converted value. * @return {@code double} containing the converted value.
*/ */
public static double readDouble(byte[] value) { public static double readDouble(byte[] value) {
return Double.longBitsToDouble(
long lvalue = ( ((long)(value[7]) << 56) + ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).put(value)
((long)(value[6]&0xFF) << 48) + .getLong(0));
((long)(value[5]&0xFF) << 40) +
((long)(value[4]&0xFF) << 32) +
((long)(value[3]&0xFF) << 24) +
((long)(value[2]&0xFF) << 16) +
((long)(value[1]&0xFF) << 8) +
(value[0]&0xFF));
return Double.longBitsToDouble(lvalue);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment