001: /*
002: * @(#)Bits.java 1.8 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: // Copied from JDK 1.4 for serialization back-port.
029: package java.io;
030:
031: /**
032: * Utility methods for packing/unpacking primitive values in/out of byte arrays
033: * using big-endian byte ordering.
034: */
035: class Bits {
036:
037: /*
038: * Methods for unpacking primitive values from byte arrays starting at
039: * given offsets.
040: */
041:
042: static boolean getBoolean(byte[] b, int off) {
043: return b[off] != 0;
044: }
045:
046: static char getChar(byte[] b, int off) {
047: return (char) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
048: }
049:
050: static short getShort(byte[] b, int off) {
051: return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
052: }
053:
054: static int getInt(byte[] b, int off) {
055: return ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
056: + ((b[off + 1] & 0xFF) << 16)
057: + ((b[off + 0] & 0xFF) << 24);
058: }
059:
060: static float getFloat(byte[] b, int off) {
061: int i = ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
062: + ((b[off + 1] & 0xFF) << 16)
063: + ((b[off + 0] & 0xFF) << 24);
064: return Float.intBitsToFloat(i);
065: }
066:
067: static long getLong(byte[] b, int off) {
068: return ((b[off + 7] & 0xFFL) << 0)
069: + ((b[off + 6] & 0xFFL) << 8)
070: + ((b[off + 5] & 0xFFL) << 16)
071: + ((b[off + 4] & 0xFFL) << 24)
072: + ((b[off + 3] & 0xFFL) << 32)
073: + ((b[off + 2] & 0xFFL) << 40)
074: + ((b[off + 1] & 0xFFL) << 48)
075: + ((b[off + 0] & 0xFFL) << 56);
076: }
077:
078: static double getDouble(byte[] b, int off) {
079: long j = ((b[off + 7] & 0xFFL) << 0)
080: + ((b[off + 6] & 0xFFL) << 8)
081: + ((b[off + 5] & 0xFFL) << 16)
082: + ((b[off + 4] & 0xFFL) << 24)
083: + ((b[off + 3] & 0xFFL) << 32)
084: + ((b[off + 2] & 0xFFL) << 40)
085: + ((b[off + 1] & 0xFFL) << 48)
086: + ((b[off + 0] & 0xFFL) << 56);
087: return Double.longBitsToDouble(j);
088: }
089:
090: /*
091: * Methods for packing primitive values into byte arrays starting at given
092: * offsets.
093: */
094:
095: static void putBoolean(byte[] b, int off, boolean val) {
096: b[off] = (byte) (val ? 1 : 0);
097: }
098:
099: static void putChar(byte[] b, int off, char val) {
100: b[off + 1] = (byte) (val >>> 0);
101: b[off + 0] = (byte) (val >>> 8);
102: }
103:
104: static void putShort(byte[] b, int off, short val) {
105: b[off + 1] = (byte) (val >>> 0);
106: b[off + 0] = (byte) (val >>> 8);
107: }
108:
109: static void putInt(byte[] b, int off, int val) {
110: b[off + 3] = (byte) (val >>> 0);
111: b[off + 2] = (byte) (val >>> 8);
112: b[off + 1] = (byte) (val >>> 16);
113: b[off + 0] = (byte) (val >>> 24);
114: }
115:
116: static void putFloat(byte[] b, int off, float val) {
117: int i = Float.floatToIntBits(val);
118: b[off + 3] = (byte) (i >>> 0);
119: b[off + 2] = (byte) (i >>> 8);
120: b[off + 1] = (byte) (i >>> 16);
121: b[off + 0] = (byte) (i >>> 24);
122: }
123:
124: static void putLong(byte[] b, int off, long val) {
125: b[off + 7] = (byte) (val >>> 0);
126: b[off + 6] = (byte) (val >>> 8);
127: b[off + 5] = (byte) (val >>> 16);
128: b[off + 4] = (byte) (val >>> 24);
129: b[off + 3] = (byte) (val >>> 32);
130: b[off + 2] = (byte) (val >>> 40);
131: b[off + 1] = (byte) (val >>> 48);
132: b[off + 0] = (byte) (val >>> 56);
133: }
134:
135: static void putDouble(byte[] b, int off, double val) {
136: long j = Double.doubleToLongBits(val);
137: b[off + 7] = (byte) (j >>> 0);
138: b[off + 6] = (byte) (j >>> 8);
139: b[off + 5] = (byte) (j >>> 16);
140: b[off + 4] = (byte) (j >>> 24);
141: b[off + 3] = (byte) (j >>> 32);
142: b[off + 2] = (byte) (j >>> 40);
143: b[off + 1] = (byte) (j >>> 48);
144: b[off + 0] = (byte) (j >>> 56);
145: }
146: }
|