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