jaroslav@601: /* jaroslav@601: * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: jaroslav@601: package java.io; jaroslav@601: jaroslav@601: /** jaroslav@601: * Utility methods for packing/unpacking primitive values in/out of byte arrays jaroslav@601: * using big-endian byte ordering. jaroslav@601: */ jaroslav@601: class Bits { jaroslav@601: jaroslav@601: /* jaroslav@601: * Methods for unpacking primitive values from byte arrays starting at jaroslav@601: * given offsets. jaroslav@601: */ jaroslav@601: jaroslav@601: static boolean getBoolean(byte[] b, int off) { jaroslav@601: return b[off] != 0; jaroslav@601: } jaroslav@601: jaroslav@601: static char getChar(byte[] b, int off) { jaroslav@601: return (char) ((b[off + 1] & 0xFF) + jaroslav@601: (b[off] << 8)); jaroslav@601: } jaroslav@601: jaroslav@601: static short getShort(byte[] b, int off) { jaroslav@601: return (short) ((b[off + 1] & 0xFF) + jaroslav@601: (b[off] << 8)); jaroslav@601: } jaroslav@601: jaroslav@601: static int getInt(byte[] b, int off) { jaroslav@601: return ((b[off + 3] & 0xFF) ) + jaroslav@601: ((b[off + 2] & 0xFF) << 8) + jaroslav@601: ((b[off + 1] & 0xFF) << 16) + jaroslav@601: ((b[off ] ) << 24); jaroslav@601: } jaroslav@601: jaroslav@601: static float getFloat(byte[] b, int off) { jaroslav@601: return Float.intBitsToFloat(getInt(b, off)); jaroslav@601: } jaroslav@601: jaroslav@601: static long getLong(byte[] b, int off) { jaroslav@601: return ((b[off + 7] & 0xFFL) ) + jaroslav@601: ((b[off + 6] & 0xFFL) << 8) + jaroslav@601: ((b[off + 5] & 0xFFL) << 16) + jaroslav@601: ((b[off + 4] & 0xFFL) << 24) + jaroslav@601: ((b[off + 3] & 0xFFL) << 32) + jaroslav@601: ((b[off + 2] & 0xFFL) << 40) + jaroslav@601: ((b[off + 1] & 0xFFL) << 48) + jaroslav@601: (((long) b[off]) << 56); jaroslav@601: } jaroslav@601: jaroslav@601: static double getDouble(byte[] b, int off) { jaroslav@601: return Double.longBitsToDouble(getLong(b, off)); jaroslav@601: } jaroslav@601: jaroslav@601: /* jaroslav@601: * Methods for packing primitive values into byte arrays starting at given jaroslav@601: * offsets. jaroslav@601: */ jaroslav@601: jaroslav@601: static void putBoolean(byte[] b, int off, boolean val) { jaroslav@601: b[off] = (byte) (val ? 1 : 0); jaroslav@601: } jaroslav@601: jaroslav@601: static void putChar(byte[] b, int off, char val) { jaroslav@601: b[off + 1] = (byte) (val ); jaroslav@601: b[off ] = (byte) (val >>> 8); jaroslav@601: } jaroslav@601: jaroslav@601: static void putShort(byte[] b, int off, short val) { jaroslav@601: b[off + 1] = (byte) (val ); jaroslav@601: b[off ] = (byte) (val >>> 8); jaroslav@601: } jaroslav@601: jaroslav@601: static void putInt(byte[] b, int off, int val) { jaroslav@601: b[off + 3] = (byte) (val ); jaroslav@601: b[off + 2] = (byte) (val >>> 8); jaroslav@601: b[off + 1] = (byte) (val >>> 16); jaroslav@601: b[off ] = (byte) (val >>> 24); jaroslav@601: } jaroslav@601: jaroslav@601: static void putFloat(byte[] b, int off, float val) { jaroslav@601: putInt(b, off, Float.floatToIntBits(val)); jaroslav@601: } jaroslav@601: jaroslav@601: static void putLong(byte[] b, int off, long val) { jaroslav@601: b[off + 7] = (byte) (val ); jaroslav@601: b[off + 6] = (byte) (val >>> 8); jaroslav@601: b[off + 5] = (byte) (val >>> 16); jaroslav@601: b[off + 4] = (byte) (val >>> 24); jaroslav@601: b[off + 3] = (byte) (val >>> 32); jaroslav@601: b[off + 2] = (byte) (val >>> 40); jaroslav@601: b[off + 1] = (byte) (val >>> 48); jaroslav@601: b[off ] = (byte) (val >>> 56); jaroslav@601: } jaroslav@601: jaroslav@601: static void putDouble(byte[] b, int off, double val) { jaroslav@601: putLong(b, off, Double.doubleToLongBits(val)); jaroslav@601: } jaroslav@601: }