jdk/src/share/classes/java/io/Bits.java
author mchung
Thu, 12 Mar 2009 10:27:22 -0700
changeset 2277 445a331b4a8b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6810254: Lazily instantiate the shared secret access objects Summary: Register the shutdown hooks only when needed and remove JavaIODeleteOnExitAccess Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Utility methods for packing/unpacking primitive values in/out of byte arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * using big-endian byte ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class Bits {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * Methods for unpacking primitive values from byte arrays starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     * given offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static boolean getBoolean(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        return b[off] != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static char getChar(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return (char) (((b[off + 1] & 0xFF) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                       ((b[off + 0]) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static short getShort(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        return (short) (((b[off + 1] & 0xFF) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                        ((b[off + 0]) << 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static int getInt(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return ((b[off + 3] & 0xFF) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
               ((b[off + 2] & 0xFF) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
               ((b[off + 1] & 0xFF) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
               ((b[off + 0]) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static float getFloat(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int i = ((b[off + 3] & 0xFF) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                ((b[off + 2] & 0xFF) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                ((b[off + 1] & 0xFF) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                ((b[off + 0]) << 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return Float.intBitsToFloat(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static long getLong(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return ((b[off + 7] & 0xFFL) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
               ((b[off + 6] & 0xFFL) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               ((b[off + 5] & 0xFFL) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
               ((b[off + 4] & 0xFFL) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
               ((b[off + 3] & 0xFFL) << 32) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
               ((b[off + 2] & 0xFFL) << 40) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
               ((b[off + 1] & 0xFFL) << 48) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
               (((long) b[off + 0]) << 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static double getDouble(byte[] b, int off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long j = ((b[off + 7] & 0xFFL) << 0) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                 ((b[off + 6] & 0xFFL) << 8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                 ((b[off + 5] & 0xFFL) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                 ((b[off + 4] & 0xFFL) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                 ((b[off + 3] & 0xFFL) << 32) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                 ((b[off + 2] & 0xFFL) << 40) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                 ((b[off + 1] & 0xFFL) << 48) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                 (((long) b[off + 0]) << 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return Double.longBitsToDouble(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Methods for packing primitive values into byte arrays starting at given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * offsets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static void putBoolean(byte[] b, int off, boolean val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        b[off] = (byte) (val ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void putChar(byte[] b, int off, char val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        b[off + 1] = (byte) (val >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        b[off + 0] = (byte) (val >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void putShort(byte[] b, int off, short val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        b[off + 1] = (byte) (val >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        b[off + 0] = (byte) (val >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static void putInt(byte[] b, int off, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        b[off + 3] = (byte) (val >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        b[off + 2] = (byte) (val >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        b[off + 1] = (byte) (val >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        b[off + 0] = (byte) (val >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static void putFloat(byte[] b, int off, float val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int i = Float.floatToIntBits(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        b[off + 3] = (byte) (i >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        b[off + 2] = (byte) (i >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        b[off + 1] = (byte) (i >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        b[off + 0] = (byte) (i >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static void putLong(byte[] b, int off, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        b[off + 7] = (byte) (val >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        b[off + 6] = (byte) (val >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        b[off + 5] = (byte) (val >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        b[off + 4] = (byte) (val >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        b[off + 3] = (byte) (val >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        b[off + 2] = (byte) (val >>> 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        b[off + 1] = (byte) (val >>> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        b[off + 0] = (byte) (val >>> 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static void putDouble(byte[] b, int off, double val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        long j = Double.doubleToLongBits(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        b[off + 7] = (byte) (j >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        b[off + 6] = (byte) (j >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        b[off + 5] = (byte) (j >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        b[off + 4] = (byte) (j >>> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        b[off + 3] = (byte) (j >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        b[off + 2] = (byte) (j >>> 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        b[off + 1] = (byte) (j >>> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        b[off + 0] = (byte) (j >>> 56);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}