java--字节数组输入、输出流

        在java网络编程中,字节数组很重要,它可以传输任何资料(文本,音频,视频,图片等),因此掌握字节数组和其它数据类型的相互转化尤为重要。

 

示例代码:

package com.lky.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lky.model.Student;

/**
* @ClassName: objectToByteArray
* @Description: 字节数组工具类
* @author lky
* @date 2015年10月26日
* @vesion 1.0
 */
public class objectToByteArray {

    private static Log log = LogFactory.getLog(objectToByteArray.class);
    private static Integer MAX_LENGTH = 2048;// 每次从流中读入多少字节
    
    /**
    * @Title: getByteArray
    * @Description: 从输入流中读取数据,并返回字节数组
    * @param in 输入流
     */
    public static byte[] getByteArray(InputStream in) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        try {
            byte[] buffer = new byte[MAX_LENGTH];
            int len = 0;

            while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                baos.write(buffer, 0, len);
                baos.flush();
                buffer = new byte[MAX_LENGTH];
            }

        } catch (Exception e) {

        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e2) {
                log.info("字节流关闭异常!!!");
            }
        }
        return baos.toByteArray();
    }
    
    /**
    * @Title: ObjectToByte
    * @Description: 将对象转化为字节数组
    * @param object
     */
    public static byte[] ObjectToByte(Object object) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;

        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);

            oos.writeObject(object);
            oos.flush();

        } catch (Exception e) {
            log.info("object转化为byte数组异常!!!");
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (Exception e2) {
                log.info("字节流关闭异常!!!");
            }
        }
        return baos.toByteArray();
    }
    
    /**
    * @Title: byteArrayToObject
    * @Description: 将字节数组转化为对象
    * @param buffer
     */
    public static Object byteArrayToObject(byte[] buffer) {
        Object object = null;
        ByteArrayInputStream bais = null;
        ObjectInputStream oss = null;
        
        try {
            bais = new ByteArrayInputStream(buffer);
            oss = new ObjectInputStream(bais);

            object = oss.readObject();

        } catch (Exception e) {
            log.info("object转化为byte数组异常!!!");
        } finally {
            try {
                if (oss != null) {
                    oss.close();
                }
                if (bais != null) {
                    bais.close();
                }
            } catch (Exception e2) {
                log.info("字节流关闭异常!!!");
            }
        }
        return object;
    }
    
    public static void main(String args[]){
        Student student=new Student();
        student.setId(1);
        student.setLog("log");
        student.setName("lky");
        System.out.println(student);
        
        byte[] buffer=ObjectToByte(student);
        
        System.out.println(buffer.length);
        System.out.println((Student)byteArrayToObject(buffer));
        
    }
}
posted @ 2015-10-27 22:15  奋斗的珞珞  阅读(2621)  评论(0编辑  收藏  举报