Path: blob/master/src/java.base/share/classes/java/io/Bits.java
41152 views
/*1* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* Utility methods for packing/unpacking primitive values in/out of byte arrays29* using big-endian byte ordering.30*/31class Bits {3233/*34* Methods for unpacking primitive values from byte arrays starting at35* given offsets.36*/3738static boolean getBoolean(byte[] b, int off) {39return b[off] != 0;40}4142static char getChar(byte[] b, int off) {43return (char) ((b[off + 1] & 0xFF) +44(b[off] << 8));45}4647static short getShort(byte[] b, int off) {48return (short) ((b[off + 1] & 0xFF) +49(b[off] << 8));50}5152static int getInt(byte[] b, int off) {53return ((b[off + 3] & 0xFF) ) +54((b[off + 2] & 0xFF) << 8) +55((b[off + 1] & 0xFF) << 16) +56((b[off ] ) << 24);57}5859static float getFloat(byte[] b, int off) {60return Float.intBitsToFloat(getInt(b, off));61}6263static long getLong(byte[] b, int off) {64return ((b[off + 7] & 0xFFL) ) +65((b[off + 6] & 0xFFL) << 8) +66((b[off + 5] & 0xFFL) << 16) +67((b[off + 4] & 0xFFL) << 24) +68((b[off + 3] & 0xFFL) << 32) +69((b[off + 2] & 0xFFL) << 40) +70((b[off + 1] & 0xFFL) << 48) +71(((long) b[off]) << 56);72}7374static double getDouble(byte[] b, int off) {75return Double.longBitsToDouble(getLong(b, off));76}7778/*79* Methods for packing primitive values into byte arrays starting at given80* offsets.81*/8283static void putBoolean(byte[] b, int off, boolean val) {84b[off] = (byte) (val ? 1 : 0);85}8687static void putChar(byte[] b, int off, char val) {88b[off + 1] = (byte) (val );89b[off ] = (byte) (val >>> 8);90}9192static void putShort(byte[] b, int off, short val) {93b[off + 1] = (byte) (val );94b[off ] = (byte) (val >>> 8);95}9697static void putInt(byte[] b, int off, int val) {98b[off + 3] = (byte) (val );99b[off + 2] = (byte) (val >>> 8);100b[off + 1] = (byte) (val >>> 16);101b[off ] = (byte) (val >>> 24);102}103104static void putFloat(byte[] b, int off, float val) {105putInt(b, off, Float.floatToIntBits(val));106}107108static void putLong(byte[] b, int off, long val) {109b[off + 7] = (byte) (val );110b[off + 6] = (byte) (val >>> 8);111b[off + 5] = (byte) (val >>> 16);112b[off + 4] = (byte) (val >>> 24);113b[off + 3] = (byte) (val >>> 32);114b[off + 2] = (byte) (val >>> 40);115b[off + 1] = (byte) (val >>> 48);116b[off ] = (byte) (val >>> 56);117}118119static void putDouble(byte[] b, int off, double val) {120putLong(b, off, Double.doubleToLongBits(val));121}122}123124125