zoukankan      html  css  js  c++  java
  • 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));
            
        }
    }
  • 相关阅读:
    数组(Array)的使用方法
    Django中的事务操作
    什么是事务(Transaction)?事务的四个特性以及事务的隔离级别
    0.2 Django + Uwsgi + Nginx 的生产环境部署之实战篇
    0.1 Django + Uwsgi + Nginx 的生产环境部署之理论篇
    linux 进入编辑文件,保存退出相关命令
    Django--restframework
    使用用户名/邮箱/手机号 + 密码登陆 多形式登陆
    Django中异步任务---django-celery
    Redis 常用命令
  • 原文地址:https://www.cnblogs.com/dmir/p/4915790.html
Copyright © 2011-2022 走看看