IO包之Bits类

  1. Bits类是default类型的并非public类型,所以Bits类仅仅只能被IO包下的类所引用

  2. 构造方法无参数 所有方法都为静态方法,所以可以直接通过类名去访问其下的所有方法

  3. 其文件开头写到:

  4. /**
     * Utility methods for packing/unpacking primitive values in/out of byte arrays
     * using big-endian byte ordering.
     */

  5. 翻译过来的意思就是:一种对字节数组高效的封装和解封手段

  6. 使用大端字节序

  7. 这里我们解释一下什么叫大端字节序?

  8. 字节序为大于一个字节大小的数据在内存中的存储顺序(你可以理解为字节为内存最小单元的大小),一般来说一个数据的存储空间在内存中是相邻的,对于CPU读取时,需要辨别多个存储单元所存储数据的顺序,顺序错了可能会得到完全不一样的数据。
    一般读取方式为顺序读取,分为两种,大端字节序(big-endian)和小端字节序(little-endian),其中大端模式跟正常人类的阅读模式一样 从高地址忘低地址读(从左往右)
    大端模式:数据的高位存储在低地址处,低位存储在高地址处
    小端模式:数据的高位储存在高地址处,低位存储在低地址处

  9. 回归到主题

  10. Bits类下一共包括两种类型的方法

  11. 1.getXXX()

    扫描二维码关注公众号,回复: 1791201 查看本文章
  12. 2.putXXX()

  13. 先解释一下getXXX()

  14. 举个例子如getBoolean方法

  15. static boolean getBoolean(byte[] b, int off) {
            return b[off] != 0;
        }

  16. 由于JAVA中Boolean类型是占一个字节,所以只需要从字节数组读取指定位置的值即可

  17. 再比如 getChar方法

  18. static char getChar(byte[] b, int off) {
            return (char) ((b[off + 1] & 0xFF) +
                           (b[off] << 8));
        }

  19. 由于JAVA中char类型是占两个字节,所以需要读取off和off加一位置的值(因为是大端模式)

  20. 所以需要将off和off+1位的二进制流转换成16位的字节流转换成char类型返回

  21. putXXX()为getXXX()的逆过程

  22. 比如说putChar()方法 

  23. static void putChar(byte[] b, int off, char val) {
            b[off + 1] = (byte) (val      );
            b[off    ] = (byte) (val >>> 8);
        }

  24.  即将val的高八位存在在b[off+1],低8位存在在b[off]中

  25. Bits类的源码如下:

  26. /*
     * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */


    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[] b, int off) {
            return b[off] != 0;
        }


        static char getChar(byte[] b, int off) {
            return (char) ((b[off + 1] & 0xFF) +
                           (b[off] << 8));
        }


        static short getShort(byte[] b, int off) {
            return (short) ((b[off + 1] & 0xFF) +
                            (b[off] << 8));
        }


        static int getInt(byte[] b, int off) {
            return ((b[off + 3] & 0xFF)      ) +
                   ((b[off + 2] & 0xFF) <<  8) +
                   ((b[off + 1] & 0xFF) << 16) +
                   ((b[off    ]       ) << 24);
        }


        static float getFloat(byte[] b, int off) {
            return Float.intBitsToFloat(getInt(b, off));
        }


        static long getLong(byte[] b, int off) {
            return ((b[off + 7] & 0xFFL)      ) +
                   ((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) +
                   (((long) b[off])      << 56);
        }


        static double getDouble(byte[] b, int off) {
            return Double.longBitsToDouble(getLong(b, off));
        }


        /*
         * Methods for packing primitive values into byte arrays starting at given
         * offsets.
         */


        static void putBoolean(byte[] b, int off, boolean val) {
            b[off] = (byte) (val ? 1 : 0);
        }


        static void putChar(byte[] b, int off, char val) {
            b[off + 1] = (byte) (val      );
            b[off    ] = (byte) (val >>> 8);
        }


        static void putShort(byte[] b, int off, short val) {
            b[off + 1] = (byte) (val      );
            b[off    ] = (byte) (val >>> 8);
        }


        static void putInt(byte[] b, int off, int val) {
            b[off + 3] = (byte) (val       );
            b[off + 2] = (byte) (val >>>  8);
            b[off + 1] = (byte) (val >>> 16);
            b[off    ] = (byte) (val >>> 24);
        }


        static void putFloat(byte[] b, int off, float val) {
            putInt(b, off,  Float.floatToIntBits(val));
        }


        static void putLong(byte[] b, int off, long val) {
            b[off + 7] = (byte) (val       );
            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    ] = (byte) (val >>> 56);
        }


        static void putDouble(byte[] b, int off, double val) {
            putLong(b, off, Double.doubleToLongBits(val));
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_35211818/article/details/80855683
今日推荐