Showing posts with label Serilization. Show all posts
Showing posts with label Serilization. Show all posts

Saturday, September 20, 2014

Serilization Users

import java.io.*;
 
public class PlayerData implements Serializable
{
    public PlayerData(String name, int score, String username,
          String password)
    {
       this.name = name;
       this.score = score;
       this.username = username;
       this.password = password;
    }
   
    public String name;
    public int score;
    public String username;
    public String password;
}



import java.io.*;

import org.junit.Test;
   
public class SerializationExample
{
    public SerializationExample()
    {    
       // Create an instance of our PlayerData class...
       PlayerData playerData = new PlayerData("abishkar", 400, 
             "abishkar", "abishkar");
    
       // Create out file object...
       File theFile = new File("output.txt");
       
       // Create the file output stream...
       FileOutputStream fileOutputStream = null;
       try
       {
           fileOutputStream = new FileOutputStream(theFile);
       }    
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object output stream...
       ObjectOutputStream objectOutputStream = null;
       
       try
       {
          objectOutputStream = new ObjectOutputStream
                (fileOutputStream);
       
          // Write the object to the object output stream...
          objectOutputStream.writeObject(playerData);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
       
       // Read the object back into a new instance of our
       // 'PlayerData' class
       PlayerData newPlayerData = null;
       
       // Create the file input stream...
       FileInputStream fileInputStream = null;
       try
       {
           fileInputStream = new FileInputStream(theFile);
       }
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object input stream...
       ObjectInputStream objectInputStream = null;
       
       try
       {
          objectInputStream = new ObjectInputStream(fileInputStream);
       
          // Read the object from the object input stream...
          newPlayerData = (PlayerData) objectInputStream
                 .readObject();
       }
       catch(ClassNotFoundException e)
       {
          System.out.println(e);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
    
       // Print what was read in...
       System.out.println("The player's name was: "+newPlayerData.name);
               
       System.out.println("The player's score was:   "+newPlayerData.score);
             
       System.out.println("The player's username was: "+newPlayerData.username);
               
       System.out.println("The player's password was: "+newPlayerData.password);
               
    }
    
    @Test
 public void    M1(){
    
       SerializationExample App = new SerializationExample();
    }
    
}
 

Object Serilization

package asychonous;


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

public class Serilization {

private static final byte[] serializedForm = new byte[] {
(byte)0xac, (byte)0xed, 0x00, 0x05, 0x73, 0x72, 0x00, 0x06,
0x50, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x40, 0x7e, (byte)0xf8,
0x2b, 0x4f, 0x46, (byte)0xc0, (byte)0xf4, 0x02, 0x00, 0x02,
0x4c, 0x00, 0x03, 0x65, 0x6e, 0x64, 0x74, 0x00, 0x10, 0x4c,
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f,
0x44, 0x61, 0x74, 0x65, 0x3b, 0x4c, 0x00, 0x05, 0x73, 0x74,
0x61, 0x72, 0x74, 0x71, 0x00, 0x7e, 0x00, 0x01, 0x78, 0x70,
0x73, 0x72, 0x00, 0x0e, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75,
0x74, 0x69, 0x6c, 0x2e, 0x44, 0x61, 0x74, 0x65, 0x68, 0x6a,
(byte)0x81, 0x01, 0x4b, 0x59, 0x74, 0x19, 0x03, 0x00, 0x00,
0x78, 0x70, 0x77, 0x08, 0x00, 0x00, 0x00, 0x66, (byte)0xdf,
0x6e, 0x1e, 0x00, 0x78, 0x73, 0x71, 0x00, 0x7e, 0x00, 0x03,
0x77, 0x08, 0x00, 0x00, 0x00, (byte)0xd5, 0x17, 0x69, 0x22,
0x00, 0x78 };
public static void main(String[] args) {
Test p = (Test) deserialize(serializedForm);
System.out.println(p);
}
//Returns the object with the specified serialized form
public static Object deserialize(byte[] sf) {
try {
InputStream is = new ByteArrayInputStream(sf);
ObjectInputStream ois = new ObjectInputStream(is);
return ois.readObject();
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
}

Monday, June 30, 2014

Serialization part 2

import java.io.*;
 
public class PlayerData implements Serializable
{
    public PlayerData(String name, int score, String username,
          String password)
    {
       this.name = name;
       this.score = score;
       this.username = username;
       this.password = password;
    }
   
    public String name;
    public int score;
    public String username;
    public String password;
}


import java.io.*;

import org.junit.Test;
   
public class SerializationExample
{
    public SerializationExample()
    {    
       // Create an instance of our PlayerData class...
       PlayerData playerData = new PlayerData("abishkar", 400, 
             "abishkar", "abishkar");
    
       // Create out file object...
       File theFile = new File("output.txt");
       
       // Create the file output stream...
       FileOutputStream fileOutputStream = null;
       try
       {
           fileOutputStream = new FileOutputStream(theFile);
       }    
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object output stream...
       ObjectOutputStream objectOutputStream = null;
       
       try
       {
          objectOutputStream = new ObjectOutputStream
                (fileOutputStream);
       
          // Write the object to the object output stream...
          objectOutputStream.writeObject(playerData);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
       
       // Read the object back into a new instance of our
       // 'PlayerData' class
       PlayerData newPlayerData = null;
       
       // Create the file input stream...
       FileInputStream fileInputStream = null;
       try
       {
           fileInputStream = new FileInputStream(theFile);
       }
       catch(FileNotFoundException e)
       {
          System.out.println(e);
       }
       
       // Create the object input stream...
       ObjectInputStream objectInputStream = null;
       
       try
       {
          objectInputStream = new ObjectInputStream(fileInputStream);
       
          // Read the object from the object input stream...
          newPlayerData = (PlayerData) objectInputStream
                 .readObject();
       }
       catch(ClassNotFoundException e)
       {
          System.out.println(e);
       }
       catch(IOException e)
       {
          System.out.println(e);
       }
    
       // Print what was read in...
       System.out.println("The player's name was: "+newPlayerData.name);
               
       System.out.println("The player's score was:   "+newPlayerData.score);
             
       System.out.println("The player's username was: "+newPlayerData.username);
               
       System.out.println("The player's password was: "+newPlayerData.password);
               
    }
    
    @Test
 public void    M1(){
    
       SerializationExample App = new SerializationExample();
    }
    
}
 

Deserialize Byte array to Object

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

public class Serilization {

private static final byte[] serializedForm = new byte[] {
(byte)0xac, (byte)0xed, 0x00, 0x05, 0x73, 0x72, 0x00, 0x06,
0x50, 0x65, 0x72, 0x69, 0x6f, 0x64, 0x40, 0x7e, (byte)0xf8,
0x2b, 0x4f, 0x46, (byte)0xc0, (byte)0xf4, 0x02, 0x00, 0x02,
0x4c, 0x00, 0x03, 0x65, 0x6e, 0x64, 0x74, 0x00, 0x10, 0x4c,
0x6a, 0x61, 0x76, 0x61, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f,
0x44, 0x61, 0x74, 0x65, 0x3b, 0x4c, 0x00, 0x05, 0x73, 0x74,
0x61, 0x72, 0x74, 0x71, 0x00, 0x7e, 0x00, 0x01, 0x78, 0x70,
0x73, 0x72, 0x00, 0x0e, 0x6a, 0x61, 0x76, 0x61, 0x2e, 0x75,
0x74, 0x69, 0x6c, 0x2e, 0x44, 0x61, 0x74, 0x65, 0x68, 0x6a,
(byte)0x81, 0x01, 0x4b, 0x59, 0x74, 0x19, 0x03, 0x00, 0x00,
0x78, 0x70, 0x77, 0x08, 0x00, 0x00, 0x00, 0x66, (byte)0xdf,
0x6e, 0x1e, 0x00, 0x78, 0x73, 0x71, 0x00, 0x7e, 0x00, 0x03,
0x77, 0x08, 0x00, 0x00, 0x00, (byte)0xd5, 0x17, 0x69, 0x22,
0x00, 0x78 };
public static void main(String[] args) {
Test p = (Test) deserialize(serializedForm);
System.out.println(p);
}
//Returns the object with the specified serialized form
public static Object deserialize(byte[] sf) {
try {
InputStream is = new ByteArrayInputStream(sf);
ObjectInputStream ois = new ObjectInputStream(is);
return ois.readObject();
} catch (Exception e) {
throw new IllegalArgumentException(e.toString());
}
}
}