001
014
015 package com.liferay.portal.kernel.io;
016
017
022 public class BigEndianCodec {
023
024 public static boolean getBoolean(byte[] bytes, int index) {
025 return bytes[index] != 0;
026 }
027
028 public static char getChar(byte[] bytes, int index) {
029 return (char)((bytes[index] << 8) + (bytes[index + 1] & 0xFF));
030 }
031
032 public static double getDouble(byte[] bytes, int index) {
033 return Double.longBitsToDouble(getLong(bytes, index));
034 }
035
036 public static float getFloat(byte[] bytes, int index) {
037 return Float.intBitsToFloat(getInt(bytes, index));
038 }
039
040 public static int getInt(byte[] bytes, int index) {
041 return (bytes[index] << 24) + ((bytes[index + 1] & 0xFF) << 16) +
042 ((bytes[index + 2] & 0xFF) << 8) + (bytes[index + 3] & 0xFF);
043 }
044
045 public static long getLong(byte[] bytes, int index) {
046 return (((long)bytes[index]) << 56) +
047 ((bytes[index + 1] & 0xFFL) << 48) +
048 ((bytes[index + 2] & 0xFFL) << 40) +
049 ((bytes[index + 3] & 0xFFL) << 32) +
050 ((bytes[index + 4] & 0xFFL) << 24) +
051 ((bytes[index + 5] & 0xFFL) << 16) +
052 ((bytes[index + 6] & 0xFFL) << 8) +
053 (bytes[index + 7] & 0xFFL);
054 }
055
056 public static short getShort(byte[] bytes, int index) {
057 return (short)((bytes[index] << 8) + (bytes[index + 1] & 0xFF));
058 }
059
060 public static void putBoolean(byte[] bytes, int index, boolean b) {
061 bytes[index] = (byte)(b ? 1 : 0);
062 }
063
064 public static void putChar(byte[] bytes, int index, char c) {
065 bytes[index] = (byte)(c >>> 8);
066 bytes[index + 1] = (byte)c;
067 }
068
069 public static void putDouble(byte[] bytes, int index, double d) {
070 putLong(bytes, index, Double.doubleToLongBits(d));
071 }
072
073 public static void putFloat(byte[] bytes, int index, float f) {
074 putInt(bytes, index, Float.floatToIntBits(f));
075 }
076
077 public static void putInt(byte[] bytes, int index, int i) {
078 bytes[index] = (byte)(i >>> 24);
079 bytes[index + 1] = (byte)(i >>> 16);
080 bytes[index + 2] = (byte)(i >>> 8);
081 bytes[index + 3] = (byte)i;
082 }
083
084 public static void putLong(byte[] bytes, int index, long l) {
085 bytes[index] = (byte)(l >>> 56);
086 bytes[index + 1] = (byte)(l >>> 48);
087 bytes[index + 2] = (byte)(l >>> 40);
088 bytes[index + 3] = (byte)(l >>> 32);
089 bytes[index + 4] = (byte)(l >>> 24);
090 bytes[index + 5] = (byte)(l >>> 16);
091 bytes[index + 6] = (byte)(l >>> 8);
092 bytes[index + 7] = (byte)l;
093 }
094
095 public static void putShort(byte[] bytes, int index, short s) {
096 bytes[index] = (byte)(s >>> 8);
097 bytes[index + 1] = (byte)s;
098 }
099
100 }