Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Sun designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Sun in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * have any questions.
  */
 
 package java.io;

Utility methods for packing/unpacking primitive values in/out of byte arrays using big-endian byte ordering.
 
 class Bits {
 
     /*
      * Methods for unpacking primitive values from byte arrays starting at
      * given offsets.
      */
 
     static boolean getBoolean(byte[] bint off) {
         return b[off] != 0;
     }
 
     static char getChar(byte[] bint off) {
         return (char) (((b[off + 1] & 0xFF) << 0) +
                        ((b[off + 0]) << 8));
     }
 
     static short getShort(byte[] bint off) {
         return (short) (((b[off + 1] & 0xFF) << 0) +
                         ((b[off + 0]) << 8));
     }
 
     static int getInt(byte[] bint off) {
         return ((b[off + 3] & 0xFF) << 0) +
                ((b[off + 2] & 0xFF) << 8) +
                ((b[off + 1] & 0xFF) << 16) +
                ((b[off + 0]) << 24);
     }
 
     static float getFloat(byte[] bint off) {
         int i = ((b[off + 3] & 0xFF) << 0) +
                 ((b[off + 2] & 0xFF) << 8) +
                 ((b[off + 1] & 0xFF) << 16) +
                 ((b[off + 0]) << 24);
         return Float.intBitsToFloat(i);
     }
 
     static long getLong(byte[] bint off) {
         return ((b[off + 7] & 0xFFL) << 0) +
                ((b[off + 6] & 0xFFL) << 8) +
                ((b[off + 5] & 0xFFL) << 16) +
                ((b[off + 4] & 0xFFL) << 24) +
                ((b[off + 3] & 0xFFL) << 32) +
                ((b[off + 2] & 0xFFL) << 40) +
                ((b[off + 1] & 0xFFL) << 48) +
                (((longb[off + 0]) << 56);
     }
 
     static double getDouble(byte[] bint off) {
         long j = ((b[off + 7] & 0xFFL) << 0) +
                  ((b[off + 6] & 0xFFL) << 8) +
                  ((b[off + 5] & 0xFFL) << 16) +
                  ((b[off + 4] & 0xFFL) << 24) +
                  ((b[off + 3] & 0xFFL) << 32) +
                  ((b[off + 2] & 0xFFL) << 40) +
                  ((b[off + 1] & 0xFFL) << 48) +
                  (((longb[off + 0]) << 56);
         return Double.longBitsToDouble(j);
     }
 
     /*
      * Methods for packing primitive values into byte arrays starting at given
      * offsets.
      */
 
     static void putBoolean(byte[] bint offboolean val) {
         b[off] = (byte) (val ? 1 : 0);
     }
 
    static void putChar(byte[] bint offchar val) {
        b[off + 1] = (byte) (val >>> 0);
        b[off + 0] = (byte) (val >>> 8);
    }
    static void putShort(byte[] bint offshort val) {
        b[off + 1] = (byte) (val >>> 0);
        b[off + 0] = (byte) (val >>> 8);
    }
    static void putInt(byte[] bint offint val) {
        b[off + 3] = (byte) (val >>> 0);
        b[off + 2] = (byte) (val >>> 8);
        b[off + 1] = (byte) (val >>> 16);
        b[off + 0] = (byte) (val >>> 24);
    }
    static void putFloat(byte[] bint offfloat val) {
        int i = Float.floatToIntBits(val);
        b[off + 3] = (byte) (i >>> 0);
        b[off + 2] = (byte) (i >>> 8);
        b[off + 1] = (byte) (i >>> 16);
        b[off + 0] = (byte) (i >>> 24);
    }
    static void putLong(byte[] bint offlong val) {
        b[off + 7] = (byte) (val >>> 0);
        b[off + 6] = (byte) (val >>> 8);
        b[off + 5] = (byte) (val >>> 16);
        b[off + 4] = (byte) (val >>> 24);
        b[off + 3] = (byte) (val >>> 32);
        b[off + 2] = (byte) (val >>> 40);
        b[off + 1] = (byte) (val >>> 48);
        b[off + 0] = (byte) (val >>> 56);
    }
    static void putDouble(byte[] bint offdouble val) {
        long j = Double.doubleToLongBits(val);
        b[off + 7] = (byte) (j >>> 0);
        b[off + 6] = (byte) (j >>> 8);
        b[off + 5] = (byte) (j >>> 16);
        b[off + 4] = (byte) (j >>> 24);
        b[off + 3] = (byte) (j >>> 32);
        b[off + 2] = (byte) (j >>> 40);
        b[off + 1] = (byte) (j >>> 48);
        b[off + 0] = (byte) (j >>> 56);
    }
New to GrepCode? Check out our FAQ X