⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 byteutils.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return bytes;
    }
    
    /**
     * Read in a long from an InputStream
     * 
     * @param inputStream
     *            The InputStream used to read the long
     * @return A long, which is the next 8 bytes converted from the InputStream
     * @throws IOException
     *             Thrown if there is a problem reading from the InputStream
     */
    public static long readLong(InputStream inputStream) throws IOException {
        byte[] byteArray = new byte[8];

        // Read in the next 8 bytes
        inputStream.read(byteArray);

        long number = convertLongFromBytes(byteArray);

        return number;
    }

    public static long convertLongFromBytes(byte[] bytes) {
        return convertLongFromBytes(bytes, 0);
    }

    public static long convertLongFromBytes(byte[] bytes, int offset) {
        // Convert it to an long
        return    ((((long) bytes[offset+7]) & 0xFF) 
                + ((((long) bytes[offset+6]) & 0xFF) << 8)
                + ((((long) bytes[offset+5]) & 0xFF) << 16)
                + ((((long) bytes[offset+4]) & 0xFF) << 24)
                + ((((long) bytes[offset+3]) & 0xFF) << 32)
                + ((((long) bytes[offset+2]) & 0xFF) << 40)
                + ((((long) bytes[offset+1]) & 0xFF) << 48) 
                + ((((long) bytes[offset+0]) & 0xFF) << 56));
    }

    
    // **********  byte <> double  METHODS  **********
    
    /**
     * Writes a double out to an OutputStream.
     * 
     * @param outputStream
     *            The OutputStream the double will be written to
     * @param value
     *            The double to write
     * @throws IOException
     *             Thrown if there is a problem writing to the OutputStream
     */
    public static void writeDouble(OutputStream outputStream, double value)
            throws IOException {
        byte[] byteArray = convertToBytes(value);

        outputStream.write(byteArray);

        return;
    }

    public static byte[] convertToBytes(double n) {
        long bits = Double.doubleToLongBits(n);
        return convertToBytes(bits);
    }
    
    /**
     * Read in a double from an InputStream
     * 
     * @param inputStream
     *            The InputStream used to read the double
     * @return A double, which is the next 8 bytes converted from the InputStream
     * @throws IOException
     *             Thrown if there is a problem reading from the InputStream
     */
    public static double readDouble(InputStream inputStream) throws IOException {
        byte[] byteArray = new byte[8];

        // Read in the next 8 bytes
        inputStream.read(byteArray);

        double number = convertDoubleFromBytes(byteArray);

        return number;
    }

    public static double convertDoubleFromBytes(byte[] bytes) {
        return convertDoubleFromBytes(bytes, 0);
    }

    public static double convertDoubleFromBytes(byte[] bytes, int offset) {
        // Convert it to a double
        long bits = convertLongFromBytes(bytes, offset);
        return Double.longBitsToDouble(bits);
    }
    
    //  **********  byte <> float  METHODS  **********

    /**
     * Writes an float out to an OutputStream.
     * 
     * @param outputStream
     *            The OutputStream the float will be written to
     * @param fVal
     *            The float to write
     * @throws IOException
     *             Thrown if there is a problem writing to the OutputStream
     */
    public static void writeFloat(OutputStream outputStream, float fVal)
            throws IOException {
        byte[] byteArray = convertToBytes(fVal);

        outputStream.write(byteArray);

        return;
    }

    public static byte[] convertToBytes(float f) {
        int temp = Float.floatToIntBits(f);
        return convertToBytes(temp);
    }

    /**
     * Read in a float from an InputStream
     * 
     * @param inputStream
     *            The InputStream used to read the float
     * @return A float, which is the next 4 bytes converted from the InputStream
     * @throws IOException
     *             Thrown if there is a problem reading from the InputStream
     */
    public static float readFloat(InputStream inputStream) throws IOException {
        byte[] byteArray = new byte[4];

        // Read in the next 4 bytes
        inputStream.read(byteArray);

        float number = convertFloatFromBytes(byteArray);

        return number;
    }

    public static float convertFloatFromBytes(byte[] byteArray) {
        return convertFloatFromBytes(byteArray, 0); 
    }
    public static float convertFloatFromBytes(byte[] byteArray, int offset) {
        // Convert it to an int
        int number = convertIntFromBytes(byteArray, offset);
        return Float.intBitsToFloat(number);
    }

    
    
    //  **********  byte <> boolean  METHODS  **********

    /**
     * Writes a boolean out to an OutputStream.
     * 
     * @param outputStream
     *            The OutputStream the boolean will be written to
     * @param bVal
     *            The boolean to write
     * @throws IOException
     *             Thrown if there is a problem writing to the OutputStream
     */
    public static void writeBoolean(OutputStream outputStream, boolean bVal)
            throws IOException {
        byte[] byteArray = convertToBytes(bVal);

        outputStream.write(byteArray);

        return;
    }

    public static byte[] convertToBytes(boolean b) {
        byte[] rVal = new byte[1];
        rVal[0] = b ? (byte)1 : (byte)0;
        return rVal;
    }

    /**
     * Read in a boolean from an InputStream
     * 
     * @param inputStream
     *            The InputStream used to read the boolean
     * @return A boolean, which is the next byte converted from the InputStream (iow, byte != 0)
     * @throws IOException
     *             Thrown if there is a problem reading from the InputStream
     */
    public static boolean readBoolean(InputStream inputStream) throws IOException {
        byte[] byteArray = new byte[1];

        // Read in the next byte
        inputStream.read(byteArray);

        return convertBooleanFromBytes(byteArray);
    }

    public static boolean convertBooleanFromBytes(byte[] byteArray) {
        return convertBooleanFromBytes(byteArray, 0); 
    }
    public static boolean convertBooleanFromBytes(byte[] byteArray, int offset) {
        return byteArray[offset] != 0;
    }

    
    /**
     * Properly reads in data from the given stream until the specified number
     * of bytes have been read.
     * 
     * @param store
     *            the byte array to store in. Should have a length > bytes
     * @param bytes
     *            the number of bytes to read.
     * @param is
     *            the stream to read from
     * @return the store array for chaining purposes
     * @throws IOException
     *             if an error occurs while reading from the stream
     * @throws ArrayIndexOutOfBoundsException
     *             if bytes greater than the length of the store.
     */
    public static byte[] readData(byte[] store, int bytes, InputStream is) throws IOException {
        for (int i = 0; i < bytes; i++) {
            store[i] = (byte)is.read();
        }
        return store;
    }

    public static byte[] rightAlignBytes(byte[] bytes, int width) {
        if (bytes.length != width) {
            byte[] rVal = new byte[width];
            for (int x = width - bytes.length; x < width; x++) {
                rVal[x] = bytes[x - (width - bytes.length)];
            }
            return rVal;
        }
            
        return bytes;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -