集册 Java实例教程 将数据的长度写为24位无符号值,后跟实际数据。

将数据的长度写为24位无符号值,后跟实际数据。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

532
将数据的长度写为24位无符号值,后跟实际数据。


import java.io.IOException;

import java.io.InputStream;

import java.nio.ByteBuffer;
/*
来 自*
 n o w j a v a . c o m
*/

import java.util.Iterator;

import org.apache.log4j.Logger;


public class Main{

    /**

     * writes the length of data as an 24 bit unsigned value, followed by the

     * actual data. does not check that the length of the data-array actually

     * fits in the lengths field.

     * 

     * @param buf

     * @param data

     */

    final static public void writeArray24(ByteBuffer buf, byte[] data) {

        putUnsigned24(buf, data.length);

        buf.put(data);

    }

    /**

     * Puts the 24 least significant bits of value in to the byte buffer, big

     * endian.

     * 

     * @param buf

     * @param value

     */

    final public static void putUnsigned24(ByteBuffer buf, int value) {
    /*来自 
     n o w j a v a . c o m - 时代Java*/

        buf.put((byte) (value >> 16));

        buf.put((byte) (value >> 8));

        buf.put((byte) value);

    }

    /**

     * Puts the 24 least significant bits of value in to the byte buffer, big

     * endian.

     * 

     * @param buf

     * @param value

     */

    final public static void putUnsigned24(ByteBuffer b
展开阅读全文