J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001-2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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) << 0) + |
| 45 | ((b[off + 0]) << 8)); |
| 46 | } |
| 47 | |
| 48 | static short getShort(byte[] b, int off) { |
| 49 | return (short) (((b[off + 1] & 0xFF) << 0) + |
| 50 | ((b[off + 0]) << 8)); |
| 51 | } |
| 52 | |
| 53 | static int getInt(byte[] b, int off) { |
| 54 | return ((b[off + 3] & 0xFF) << 0) + |
| 55 | ((b[off + 2] & 0xFF) << 8) + |
| 56 | ((b[off + 1] & 0xFF) << 16) + |
| 57 | ((b[off + 0]) << 24); |
| 58 | } |
| 59 | |
| 60 | static float getFloat(byte[] b, int off) { |
| 61 | int i = ((b[off + 3] & 0xFF) << 0) + |
| 62 | ((b[off + 2] & 0xFF) << 8) + |
| 63 | ((b[off + 1] & 0xFF) << 16) + |
| 64 | ((b[off + 0]) << 24); |
| 65 | return Float.intBitsToFloat(i); |
| 66 | } |
| 67 | |
| 68 | static long getLong(byte[] b, int off) { |
| 69 | return ((b[off + 7] & 0xFFL) << 0) + |
| 70 | ((b[off + 6] & 0xFFL) << 8) + |
| 71 | ((b[off + 5] & 0xFFL) << 16) + |
| 72 | ((b[off + 4] & 0xFFL) << 24) + |
| 73 | ((b[off + 3] & 0xFFL) << 32) + |
| 74 | ((b[off + 2] & 0xFFL) << 40) + |
| 75 | ((b[off + 1] & 0xFFL) << 48) + |
| 76 | (((long) b[off + 0]) << 56); |
| 77 | } |
| 78 | |
| 79 | static double getDouble(byte[] b, int off) { |
| 80 | long j = ((b[off + 7] & 0xFFL) << 0) + |
| 81 | ((b[off + 6] & 0xFFL) << 8) + |
| 82 | ((b[off + 5] & 0xFFL) << 16) + |
| 83 | ((b[off + 4] & 0xFFL) << 24) + |
| 84 | ((b[off + 3] & 0xFFL) << 32) + |
| 85 | ((b[off + 2] & 0xFFL) << 40) + |
| 86 | ((b[off + 1] & 0xFFL) << 48) + |
| 87 | (((long) b[off + 0]) << 56); |
| 88 | return Double.longBitsToDouble(j); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Methods for packing primitive values into byte arrays starting at given |
| 93 | * offsets. |
| 94 | */ |
| 95 | |
| 96 | static void putBoolean(byte[] b, int off, boolean val) { |
| 97 | b[off] = (byte) (val ? 1 : 0); |
| 98 | } |
| 99 | |
| 100 | static void putChar(byte[] b, int off, char val) { |
| 101 | b[off + 1] = (byte) (val >>> 0); |
| 102 | b[off + 0] = (byte) (val >>> 8); |
| 103 | } |
| 104 | |
| 105 | static void putShort(byte[] b, int off, short val) { |
| 106 | b[off + 1] = (byte) (val >>> 0); |
| 107 | b[off + 0] = (byte) (val >>> 8); |
| 108 | } |
| 109 | |
| 110 | static void putInt(byte[] b, int off, int val) { |
| 111 | b[off + 3] = (byte) (val >>> 0); |
| 112 | b[off + 2] = (byte) (val >>> 8); |
| 113 | b[off + 1] = (byte) (val >>> 16); |
| 114 | b[off + 0] = (byte) (val >>> 24); |
| 115 | } |
| 116 | |
| 117 | static void putFloat(byte[] b, int off, float val) { |
| 118 | int i = Float.floatToIntBits(val); |
| 119 | b[off + 3] = (byte) (i >>> 0); |
| 120 | b[off + 2] = (byte) (i >>> 8); |
| 121 | b[off + 1] = (byte) (i >>> 16); |
| 122 | b[off + 0] = (byte) (i >>> 24); |
| 123 | } |
| 124 | |
| 125 | static void putLong(byte[] b, int off, long val) { |
| 126 | b[off + 7] = (byte) (val >>> 0); |
| 127 | b[off + 6] = (byte) (val >>> 8); |
| 128 | b[off + 5] = (byte) (val >>> 16); |
| 129 | b[off + 4] = (byte) (val >>> 24); |
| 130 | b[off + 3] = (byte) (val >>> 32); |
| 131 | b[off + 2] = (byte) (val >>> 40); |
| 132 | b[off + 1] = (byte) (val >>> 48); |
| 133 | b[off + 0] = (byte) (val >>> 56); |
| 134 | } |
| 135 | |
| 136 | static void putDouble(byte[] b, int off, double val) { |
| 137 | long j = Double.doubleToLongBits(val); |
| 138 | b[off + 7] = (byte) (j >>> 0); |
| 139 | b[off + 6] = (byte) (j >>> 8); |
| 140 | b[off + 5] = (byte) (j >>> 16); |
| 141 | b[off + 4] = (byte) (j >>> 24); |
| 142 | b[off + 3] = (byte) (j >>> 32); |
| 143 | b[off + 2] = (byte) (j >>> 40); |
| 144 | b[off + 1] = (byte) (j >>> 48); |
| 145 | b[off + 0] = (byte) (j >>> 56); |
| 146 | } |
| 147 | } |