tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Jodd > Commons > How to convert Byte array to Object

How to convert Byte array to Object 

Jodd is an open-source java library with lot of reusable components and feature rich utilities. This requires the library jodd-3.3.2.jar to be in classpath. The following example shows how to use ObjectUtil.byteArrayToObject() API. It converts the given byte array to object.

File Name  :  
com/bethecoder/tutorials/jodd/common/Student.java 
   
package com.bethecoder.tutorials.jodd.common;

public class Student implements java.io.Serializable {

  private static final long serialVersionUID = -5962595557796049374L;
  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/jodd/encode/ByteArrayToObjectTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.jodd.encode;

import java.io.IOException;

import jodd.util.ObjectUtil;

import com.bethecoder.tutorials.jodd.common.Student;

public class ByteArrayToObjectTest {

  /**
   @param args
   @throws IOException 
   @throws ClassNotFoundException 
   */
  public static void main(String[] argsthrows IOException, ClassNotFoundException {
    
    byte [] bytes = -84, -19051151140449911110946
          981011161041019911110010111446116
          1171161111141059710811546106111100
          10046991111091091111104683116117
          100101110116, -8364, -102, -1811062, -114
          342037303971031017605104111
          989812111601876106971189747108
          9711010347831161141051101035976
          0411097109101113012601120112
          000211605671041011151151160
          68311410511497109
    };

    Student std = (StudentObjectUtil.byteArrayToObject(bytes);
    System.out.println(std);
  }

}
   

It gives the following output,
Student[name = Sriram, age = 2, hobby = Chess]


                    Print

   



Books Recommendation

 
 
bl  br