468,738 Members | 2,455 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

How can I convert a byte array to an object ?

108 100+
I want to convert my object to byte array and then next to an object and run a method..The codes are below:

Expand|Select|Wrap|Line Numbers
  1. public class test_serialisation implements Serializable{
  2.  
  3.     /**
  4.      * 
  5.      */
  6.     private static final long serialVersionUID = 1L;
  7.  
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         ObjectOutputStream ob;
  12.         FileOutputStream f;
  13.         Object ob1;
  14.         //DatagramSocket sck=new DatagramSocket();
  15.         ByteArrayOutputStream buf;
  16.         byte[] arr1=new byte[256];
  17.         try {
  18.             buf=new ByteArrayOutputStream();
  19.  
  20.  
  21.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  22.             ObjectOutputStream oos = new ObjectOutputStream(baos);
  23.             oos.writeObject(new test_serialisation());
  24.             oos.flush();
  25.             oos.close();
  26.  
  27.  
  28.             byte[] sendBytes = baos.toByteArray();
  29.             System.out.println("testing bytes array.........");
  30.             System.out.println(sendBytes.length);
  31.  
  32.             System.out.println("sendBytes.length : " + sendBytes.length);
  33.  
  34.             //ds.send(dp);
  35.         ByteArrayInputStream out=new ByteArrayInputStream(arr1);
  36.             ObjectInputStream n=new ObjectInputStream(out);
  37.             test_serialisation bb=(test_serialisation) n.readObject();
  38.             bb.test();
  39.  
  40.             FileInputStream in = new FileInputStream("tmp");
  41.             ObjectInputStream ss = new ObjectInputStream(in);
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. } catch (Exception e) {
  49.  
  50.             e.printStackTrace();
  51.         }
  52.  
  53.     }
  54.  
  55.  
  56.     public void test(){
  57.         System.out.println("Serialisation working");
  58.     }
  59.  
  60. }
  61.  
  62.  
  63.  
When I run this code,I get the java.io.StreamCorruptedException and the stacktrace is:;
Expand|Select|Wrap|Line Numbers
  1. java.io.StreamCorruptedException: invalid stream header
  2.     at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
  3.     at java.io.ObjectInputStream.<init>(Unknown Source)
  4.     at test_serialisation.main(test_serialisation.java:55)
  5.  
I would appreciate if anyone could help me out wd this exception ...


Thanks in advance
Oct 11 '10 #1
1 10839
Nepomuk
3,112 Expert 2GB
Which is the line 55 in your code? Because in the code you posted, line 55 is empty, although the error message states, that your problem is there.

And what error does it through, if you don't catch the exception?

Greetings,
Nepomuk
Oct 19 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Lou | last post: by
5 posts views Thread by Andrew Inwards | last post: by
2 posts views Thread by Dave | last post: by
5 posts views Thread by Terry Olsen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.