001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io;
016    
017    /**
018     * Encodes/decodes primitive types to/from big-endian byte sequences.
019     *
020     * @author Shuyang Zhou
021     */
022    public class BigEndianCodec {
023    
024            public static boolean getBoolean(byte[] bytes, int index) {
025                    if (bytes[index] != 0) {
026                            return true;
027                    }
028    
029                    return false;
030            }
031    
032            public static char getChar(byte[] bytes, int index) {
033                    return (char)((bytes[index] << 8) + (bytes[index + 1] & 0xFF));
034            }
035    
036            public static double getDouble(byte[] bytes, int index) {
037                    return Double.longBitsToDouble(getLong(bytes, index));
038            }
039    
040            public static float getFloat(byte[] bytes, int index) {
041                    return Float.intBitsToFloat(getInt(bytes, index));
042            }
043    
044            public static int getInt(byte[] bytes, int index) {
045                    return (bytes[index] << 24) + ((bytes[index + 1] & 0xFF) << 16) +
046                            ((bytes[index + 2] & 0xFF) << 8) + (bytes[index + 3] & 0xFF);
047            }
048    
049            public static long getLong(byte[] bytes, int index) {
050                    return (((long)bytes[index]) << 56) +
051                            ((bytes[index + 1] & 0xFFL) << 48) +
052                            ((bytes[index + 2] & 0xFFL) << 40) +
053                            ((bytes[index + 3] & 0xFFL) << 32) +
054                            ((bytes[index + 4] & 0xFFL) << 24) +
055                            ((bytes[index + 5] & 0xFFL) << 16) +
056                            ((bytes[index + 6] & 0xFFL) << 8) +
057                            (bytes[index + 7] & 0xFFL);
058            }
059    
060            public static short getShort(byte[] bytes, int index) {
061                    return (short)((bytes[index] << 8) + (bytes[index + 1] & 0xFF));
062            }
063    
064            public static void putBoolean(byte[] bytes, int index, boolean b) {
065                    bytes[index] = (byte)(b ? 1 : 0);
066            }
067    
068            public static void putChar(byte[] bytes, int index, char c) {
069                    bytes[index] = (byte)(c >>> 8);
070                    bytes[index + 1] = (byte)c;
071            }
072    
073            public static void putDouble(byte[] bytes, int index, double d) {
074                    putLong(bytes, index, Double.doubleToLongBits(d));
075            }
076    
077            public static void putFloat(byte[] bytes, int index, float f) {
078                    putInt(bytes, index, Float.floatToIntBits(f));
079            }
080    
081            public static void putInt(byte[] bytes, int index, int i) {
082                    bytes[index] = (byte)(i >>> 24);
083                    bytes[index + 1] = (byte)(i >>> 16);
084                    bytes[index + 2] = (byte)(i >>> 8);
085                    bytes[index + 3] = (byte)i;
086            }
087    
088            public static void putLong(byte[] bytes, int index, long l) {
089                    bytes[index] = (byte)(l >>> 56);
090                    bytes[index + 1] = (byte)(l >>> 48);
091                    bytes[index + 2] = (byte)(l >>> 40);
092                    bytes[index + 3] = (byte)(l >>> 32);
093                    bytes[index + 4] = (byte)(l >>> 24);
094                    bytes[index + 5] = (byte)(l >>> 16);
095                    bytes[index + 6] = (byte)(l >>> 8);
096                    bytes[index + 7] = (byte)l;
097            }
098    
099            public static void putShort(byte[] bytes, int index, short s) {
100                    bytes[index] = (byte)(s >>> 8);
101                    bytes[index + 1] = (byte)s;
102            }
103    
104    }