jdk/src/share/classes/java/io/Bits.java
changeset 5993 47d52d67b5b2
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5992:15c59951d875 5993:47d52d67b5b2
    39     static boolean getBoolean(byte[] b, int off) {
    39     static boolean getBoolean(byte[] b, int off) {
    40         return b[off] != 0;
    40         return b[off] != 0;
    41     }
    41     }
    42 
    42 
    43     static char getChar(byte[] b, int off) {
    43     static char getChar(byte[] b, int off) {
    44         return (char) (((b[off + 1] & 0xFF) << 0) +
    44         return (char) ((b[off + 1] & 0xFF) +
    45                        ((b[off + 0]) << 8));
    45                        (b[off] << 8));
    46     }
    46     }
    47 
    47 
    48     static short getShort(byte[] b, int off) {
    48     static short getShort(byte[] b, int off) {
    49         return (short) (((b[off + 1] & 0xFF) << 0) +
    49         return (short) ((b[off + 1] & 0xFF) +
    50                         ((b[off + 0]) << 8));
    50                         (b[off] << 8));
    51     }
    51     }
    52 
    52 
    53     static int getInt(byte[] b, int off) {
    53     static int getInt(byte[] b, int off) {
    54         return ((b[off + 3] & 0xFF) << 0) +
    54         return ((b[off + 3] & 0xFF)      ) +
    55                ((b[off + 2] & 0xFF) << 8) +
    55                ((b[off + 2] & 0xFF) <<  8) +
    56                ((b[off + 1] & 0xFF) << 16) +
    56                ((b[off + 1] & 0xFF) << 16) +
    57                ((b[off + 0]) << 24);
    57                ((b[off    ]       ) << 24);
    58     }
    58     }
    59 
    59 
    60     static float getFloat(byte[] b, int off) {
    60     static float getFloat(byte[] b, int off) {
    61         int i = ((b[off + 3] & 0xFF) << 0) +
    61         return Float.intBitsToFloat(getInt(b, off));
    62                 ((b[off + 2] & 0xFF) << 8) +
       
    63                 ((b[off + 1] & 0xFF) << 16) +
       
    64                 ((b[off + 0]) << 24);
       
    65         return Float.intBitsToFloat(i);
       
    66     }
    62     }
    67 
    63 
    68     static long getLong(byte[] b, int off) {
    64     static long getLong(byte[] b, int off) {
    69         return ((b[off + 7] & 0xFFL) << 0) +
    65         return ((b[off + 7] & 0xFFL)      ) +
    70                ((b[off + 6] & 0xFFL) << 8) +
    66                ((b[off + 6] & 0xFFL) <<  8) +
    71                ((b[off + 5] & 0xFFL) << 16) +
    67                ((b[off + 5] & 0xFFL) << 16) +
    72                ((b[off + 4] & 0xFFL) << 24) +
    68                ((b[off + 4] & 0xFFL) << 24) +
    73                ((b[off + 3] & 0xFFL) << 32) +
    69                ((b[off + 3] & 0xFFL) << 32) +
    74                ((b[off + 2] & 0xFFL) << 40) +
    70                ((b[off + 2] & 0xFFL) << 40) +
    75                ((b[off + 1] & 0xFFL) << 48) +
    71                ((b[off + 1] & 0xFFL) << 48) +
    76                (((long) b[off + 0]) << 56);
    72                (((long) b[off])      << 56);
    77     }
    73     }
    78 
    74 
    79     static double getDouble(byte[] b, int off) {
    75     static double getDouble(byte[] b, int off) {
    80         long j = ((b[off + 7] & 0xFFL) << 0) +
    76         return Double.longBitsToDouble(getLong(b, off));
    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     }
    77     }
    90 
    78 
    91     /*
    79     /*
    92      * Methods for packing primitive values into byte arrays starting at given
    80      * Methods for packing primitive values into byte arrays starting at given
    93      * offsets.
    81      * offsets.
    96     static void putBoolean(byte[] b, int off, boolean val) {
    84     static void putBoolean(byte[] b, int off, boolean val) {
    97         b[off] = (byte) (val ? 1 : 0);
    85         b[off] = (byte) (val ? 1 : 0);
    98     }
    86     }
    99 
    87 
   100     static void putChar(byte[] b, int off, char val) {
    88     static void putChar(byte[] b, int off, char val) {
   101         b[off + 1] = (byte) (val >>> 0);
    89         b[off + 1] = (byte) (val      );
   102         b[off + 0] = (byte) (val >>> 8);
    90         b[off    ] = (byte) (val >>> 8);
   103     }
    91     }
   104 
    92 
   105     static void putShort(byte[] b, int off, short val) {
    93     static void putShort(byte[] b, int off, short val) {
   106         b[off + 1] = (byte) (val >>> 0);
    94         b[off + 1] = (byte) (val      );
   107         b[off + 0] = (byte) (val >>> 8);
    95         b[off    ] = (byte) (val >>> 8);
   108     }
    96     }
   109 
    97 
   110     static void putInt(byte[] b, int off, int val) {
    98     static void putInt(byte[] b, int off, int val) {
   111         b[off + 3] = (byte) (val >>> 0);
    99         b[off + 3] = (byte) (val       );
   112         b[off + 2] = (byte) (val >>> 8);
   100         b[off + 2] = (byte) (val >>>  8);
   113         b[off + 1] = (byte) (val >>> 16);
   101         b[off + 1] = (byte) (val >>> 16);
   114         b[off + 0] = (byte) (val >>> 24);
   102         b[off    ] = (byte) (val >>> 24);
   115     }
   103     }
   116 
   104 
   117     static void putFloat(byte[] b, int off, float val) {
   105     static void putFloat(byte[] b, int off, float val) {
   118         int i = Float.floatToIntBits(val);
   106         putInt(b, off,  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     }
   107     }
   124 
   108 
   125     static void putLong(byte[] b, int off, long val) {
   109     static void putLong(byte[] b, int off, long val) {
   126         b[off + 7] = (byte) (val >>> 0);
   110         b[off + 7] = (byte) (val       );
   127         b[off + 6] = (byte) (val >>> 8);
   111         b[off + 6] = (byte) (val >>>  8);
   128         b[off + 5] = (byte) (val >>> 16);
   112         b[off + 5] = (byte) (val >>> 16);
   129         b[off + 4] = (byte) (val >>> 24);
   113         b[off + 4] = (byte) (val >>> 24);
   130         b[off + 3] = (byte) (val >>> 32);
   114         b[off + 3] = (byte) (val >>> 32);
   131         b[off + 2] = (byte) (val >>> 40);
   115         b[off + 2] = (byte) (val >>> 40);
   132         b[off + 1] = (byte) (val >>> 48);
   116         b[off + 1] = (byte) (val >>> 48);
   133         b[off + 0] = (byte) (val >>> 56);
   117         b[off    ] = (byte) (val >>> 56);
   134     }
   118     }
   135 
   119 
   136     static void putDouble(byte[] b, int off, double val) {
   120     static void putDouble(byte[] b, int off, double val) {
   137         long j = Double.doubleToLongBits(val);
   121         putLong(b, off, 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     }
   122     }
   147 }
   123 }