Showing the changes to java/pkg/java/io/Bits.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,123 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package java.io; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Utility methods for packing/unpacking primitive values in/out of byte arrays | ||
| 30 | * using big-endian byte ordering. | ||
| 31 | */ | ||
| 32 | class Bits { | ||
| 33 | |||
| 34 | /* | ||
| 35 | * Methods for unpacking primitive values from byte arrays starting at | ||
| 36 | * given offsets. | ||
| 37 | */ | ||
| 38 | |||
| 39 | static boolean getBoolean(byte[] b, int off) { | ||
| 40 | return b[off] != 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | static char getChar(byte[] b, int off) { | ||
| 44 | return (char) ((b[off + 1] & 0xFF) + | ||
| 45 | (b[off] << 8)); | ||
| 46 | } | ||
| 47 | |||
| 48 | static short getShort(byte[] b, int off) { | ||
| 49 | return (short) ((b[off + 1] & 0xFF) + | ||
| 50 | (b[off] << 8)); | ||
| 51 | } | ||
| 52 | |||
| 53 | static int getInt(byte[] b, int off) { | ||
| 54 | return ((b[off + 3] & 0xFF) ) + | ||
| 55 | ((b[off + 2] & 0xFF) << 8) + | ||
| 56 | ((b[off + 1] & 0xFF) << 16) + | ||
| 57 | ((b[off ] ) << 24); | ||
| 58 | } | ||
| 59 | |||
| 60 | static float getFloat(byte[] b, int off) { | ||
| 61 | return Float.intBitsToFloat(getInt(b, off)); | ||
| 62 | } | ||
| 63 | |||
| 64 | static long getLong(byte[] b, int off) { | ||
| 65 | return ((b[off + 7] & 0xFFL) ) + | ||
| 66 | ((b[off + 6] & 0xFFL) << 8) + | ||
| 67 | ((b[off + 5] & 0xFFL) << 16) + | ||
| 68 | ((b[off + 4] & 0xFFL) << 24) + | ||
| 69 | ((b[off + 3] & 0xFFL) << 32) + | ||
| 70 | ((b[off + 2] & 0xFFL) << 40) + | ||
| 71 | ((b[off + 1] & 0xFFL) << 48) + | ||
| 72 | (((long) b[off]) << 56); | ||
| 73 | } | ||
| 74 | |||
| 75 | static double getDouble(byte[] b, int off) { | ||
| 76 | return Double.longBitsToDouble(getLong(b, off)); | ||
| 77 | } | ||
| 78 | |||
| 79 | /* | ||
| 80 | * Methods for packing primitive values into byte arrays starting at given | ||
| 81 | * offsets. | ||
| 82 | */ | ||
| 83 | |||
| 84 | static void putBoolean(byte[] b, int off, boolean val) { | ||
| 85 | b[off] = (byte) (val ? 1 : 0); | ||
| 86 | } | ||
| 87 | |||
| 88 | static void putChar(byte[] b, int off, char val) { | ||
| 89 | b[off + 1] = (byte) (val ); | ||
| 90 | b[off ] = (byte) (val >>> 8); | ||
| 91 | } | ||
| 92 | |||
| 93 | static void putShort(byte[] b, int off, short val) { | ||
| 94 | b[off + 1] = (byte) (val ); | ||
| 95 | b[off ] = (byte) (val >>> 8); | ||
| 96 | } | ||
| 97 | |||
| 98 | static void putInt(byte[] b, int off, int val) { | ||
| 99 | b[off + 3] = (byte) (val ); | ||
| 100 | b[off + 2] = (byte) (val >>> 8); | ||
| 101 | b[off + 1] = (byte) (val >>> 16); | ||
| 102 | b[off ] = (byte) (val >>> 24); | ||
| 103 | } | ||
| 104 | |||
| 105 | static void putFloat(byte[] b, int off, float val) { | ||
| 106 | putInt(b, off, Float.floatToIntBits(val)); | ||
| 107 | } | ||
| 108 | |||
| 109 | static void putLong(byte[] b, int off, long val) { | ||
| 110 | b[off + 7] = (byte) (val ); | ||
| 111 | b[off + 6] = (byte) (val >>> 8); | ||
| 112 | b[off + 5] = (byte) (val >>> 16); | ||
| 113 | b[off + 4] = (byte) (val >>> 24); | ||
| 114 | b[off + 3] = (byte) (val >>> 32); | ||
| 115 | b[off + 2] = (byte) (val >>> 40); | ||
| 116 | b[off + 1] = (byte) (val >>> 48); | ||
| 117 | b[off ] = (byte) (val >>> 56); | ||
| 118 | } | ||
| 119 | |||
| 120 | static void putDouble(byte[] b, int off, double val) { | ||
| 121 | putLong(b, off, Double.doubleToLongBits(val)); | ||
| 122 | } | ||
| 123 | } | ||