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