天天看點

與位元組數組相關的IO操作

總結一下與位元組數組相關的io操作。

關于 把十六進制的位串轉化為byte數組,請參閱 http://hw1287789687.iteye.com/blog/1882644

(1)從inputstream 讀取位元組數組

方式一:

與位元組數組相關的IO操作

/*** 

     * has been tested 

     *  

     * @param in 

     * @return 

     * @throws ioexception 

     */  

    public static byte[] readbytes3(inputstream in) throws ioexception {  

        bufferedinputstream bufin = new bufferedinputstream(in);  

        int buffsize = buffsize_1024;  

        bytearrayoutputstream out = new bytearrayoutputstream(buffsize);  

        // system.out.println("available bytes:" + in.available());  

        byte[] temp = new byte[4096];  

        int size = 0;  

        while ((size = bufin.read(temp)) != -1) {  

            out.write(temp, 0, size);  

        }  

        bufin.close();  

        in.close();  

        byte[] content = out.tobytearray();  

        return content;  

    }  

 說明:先把inputstream的位元組讀到bytearrayoutputstream中,讀完之後再調用tobytearray() 轉化為位元組數組。

方式二:

與位元組數組相關的IO操作

    public static byte[] readbytes(inputstream in) throws ioexception {  

        byte[] temp = new byte[in.available()];  

        byte[] result = new byte[0];  

        while ((size = in.read(temp)) != -1) {  

            byte[] readbytes = new byte[size];  

            system.arraycopy(temp, 0, readbytes, 0, size);  

            result = mergearray(result, readbytes);  

        return result;  

     * 合并位元組數組 

     * @param a 

    public static byte[] mergearray(byte[]... a) {  

        // 合并完之後數組的總長度  

        int index = 0;  

        int sum = 0;  

        for (int i = 0; i < a.length; i++) {  

            sum = sum + a[i].length;  

        byte[] result = new byte[sum];  

            int lengthone = a[i].length;  

            if (lengthone == 0) {  

                continue;  

            }  

            // 拷貝數組  

            system.arraycopy(a[i], 0, result, index, lengthone);  

            index = index + lengthone;  

(2)把位元組數組寫入檔案

與位元組數組相關的IO操作

     * write byte[] to file 

     * @param bytes 

     * @param destfile 

    public static void writebytestofile(byte[] bytes, file destfile)  

            throws ioexception {  

        fileoutputstream out = new fileoutputstream(destfile);  

        write2file(bytes, out);  

     * @param out 

    public static void write2file(byte[] bytes, fileoutputstream out)  

        out.write(bytes);  

        out.close();  

(3)在已有位元組數組基礎上追加一個位元組

與位元組數組相關的IO操作

     * append a byte. 

     * @param b 

    public static byte[] appandbyte(byte[] a, byte b) {  

        int length = a.length;  

        byte[] resultbytes = new byte[length + 1];  

        system.arraycopy(a, 0, resultbytes, 0, length);  

        resultbytes[length] = b;  

        return resultbytes;  

(4)比較兩個位元組數組是否相同

與位元組數組相關的IO操作

     * compare two byte arrays whether are the same. 

    public static boolean arrayisequal(byte[] a, byte[] b) {  

        if(a==null&&b==null){  

            return true;  

        if (a != null && b != null) {  

            if (a.length != b.length) {  

                return false;  

            } else {  

                for (int i = 0; i < a.length; i++) {  

                    if (a[i] != b[i]) {  

                        return false;  

                    }  

                }  

        }else {//one is null, the other is not null  

            return false;  

        return true;  

(5)查找指定位元組findtarget在指定位元組數組source中的位置

與位元組數組相關的IO操作

     * @param source 

     * @param findtarget 

     *            :key word 

     * @param pos 

     *            :where start from 

     * @return index 

    public static int findbytes(byte[] source, byte[] findtarget, int pos) {  

        int i, j, k = 0;  

        i = pos;  

        j = 0;  

        while (i < source.length && j < findtarget.length) {  

            if (source[i] == findtarget[j]) {  

                ++i;  

                ++j;  

                if (j == findtarget.length) {  

                    k = k + 1;// k++  

                    break;  

                    // j = 0;  

                i = i - j + 1;  

                j = 0;  

        return k == 0 ? -1 : i - j;  

測試代碼:

與位元組數組相關的IO操作

@test  

   public void test_arrayisequal2(){  

    system.out.println("test_filterfrontbytes");  

    byte[] a = new byte[] { 1, 2, 3, 4 };  

    byte[] b = new byte[] { 1, 2, 3 };  

    system.out.println( <span style="font-size: 1em; line-height: 1.5;">arrayisequal</span><span style="font-size: 1em; line-height: 1.5;">(a, b));</span>  

與位元組數組相關的IO操作

}  

    public void test_appandbyte(){  

        byte[]bytes=new byte[]{1,2,3};  

        byte[]resultbytes=appandbyte(bytes, (byte)32);  

        <span style="font-size: 1em; line-height: 1.5;">arrayisequal</span><span style="font-size: 1em; line-height: 1.5;">(resultbytes, new byte[]{1, 2, 3, 32});</span>  

與位元組數組相關的IO操作

}