javaオブジェクトとbyte[]配列の相互変換、圧縮解凍動作


javaオブジェクトとbyte[]配列の相互変換を紹介します。byte[]データを圧縮します。javaオブジェクトをbyte[]配列に変換して、redisにおいてキャッシュを実現するために使用することができる。(ここでは紹介しないでください。)話は多くないです。直接的に例を挙げます。まず、javaオブジェクトを作成します。Person.java
public class Person implements Serializable{
    private String userName;
    private String password;
    private String phone;
    private String email;
    private String sex;
    private String age;

    public Person(){}

    public Person(String userName, String password, String phone, String email,
            String sex, String age) {
        super();
        this.userName = userName;
        this.password = password;
        this.phone = phone;
        this.email = email;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [userName=" + userName + ", password=" + password
                + ", phone=" + phone + ", email=" + email + ", sex=" + sex
                + ", age=" + age + "]";
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }


}
次のデモでは、personオブジェクトへの変換:Object 2 Bytearray.java
public class Object2ByteArray {
    public static void main(String[] args) {
        try {
            Person person=new Person("userName", "password", "phone", "email", "sex", "age");
            System.out.println("person:"+person);
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(bos);
            oos.writeObject(person);
            //  person   byte  
            byte[] personByteArray = bos.toByteArray();
            System.out.println("before compress:"+personByteArray.length);
            // byte    
            byte[] zipPersonByteArray = compress(personByteArray);
            System.out.println("after compress:"+zipPersonByteArray.length);
            closeStream(oos);
            closeStream(bos);
            // byte     person  
            ByteArrayInputStream bin=new ByteArrayInputStream(personByteArray);
            ObjectInputStream ois=new ObjectInputStream(bin);
            Person restorePerson = (Person) ois.readObject();
            System.out.println(restorePerson);
            closeStream(ois);
            closeStream(bin);
            //    byte     person  
            byte[] unCompressByte = unCompress(zipPersonByteArray);
            ByteArrayInputStream zipBin=new ByteArrayInputStream(unCompressByte);
            ObjectInputStream zipOis=new ObjectInputStream(zipBin);
            Person zipBytePerson=(Person) zipOis.readObject();
            System.out.println("compress person:"+zipBytePerson.toString());
            closeStream(zipOis);
            closeStream(zipBin);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** * * @description       * @param oStream * */
    public static void closeStream(Closeable oStream){
        if(null!=oStream){
            try {
                oStream.close();
            } catch (IOException e) {
                oStream=null;//   null,      
                e.printStackTrace();
            }
        }
    }

    /** * * @description  byte      * @param bt * @return */
    public static byte[] compress(byte[] bt){
        // byte       
        ByteArrayOutputStream bos=null;
        GZIPOutputStream gzipos=null;
        try {
            bos=new ByteArrayOutputStream();
            gzipos=new GZIPOutputStream(bos);
            gzipos.write(bt);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeStream(gzipos);
            closeStream(bos);
        }
        return bos.toByteArray();
    }

    /** * * @description    byte   * @param bt * @return */
    public static byte[] unCompress(byte[] bt){
        //byte[] unCompress=null;
        ByteArrayOutputStream byteAos=null;
        ByteArrayInputStream byteArrayIn=null;
        GZIPInputStream gzipIn=null;
        try {
            byteArrayIn=new ByteArrayInputStream(bt);
            gzipIn=new GZIPInputStream(byteArrayIn);
             byteAos=new ByteArrayOutputStream();
            byte[] b=new byte[4096];
            int temp = -1;
            while((temp=gzipIn.read(b))>0){
                byteAos.write(b, 0, temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            closeStream(byteAos);
            closeStream(gzipIn);
            closeStream(byteArrayIn);
        }
        return byteAos.toByteArray();
    }
}
上記の例は、javaオブジェクトからbyte[]データへの変換を示しています。byte[]データの圧縮と解凍操作;byte[]データ復元javaオブジェクトの操作。
実行結果:
person:Person [userName=userName, password=password, phone=phone, email=email, sex=sex, age=age]
before compress:189
after compress:156
Person [userName=userName, password=password, phone=phone, email=email, sex=sex, age=age]
compress person:Person [userName=userName, password=password, phone=phone, email=email, sex=sex, age=age]