473,389 Members | 1,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

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 13209
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

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

Similar topics

1
by: Lou | last post by:
How do you convert a byte array to a string?
6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
5
by: Andrew Inwards | last post by:
Can anyone tell me how to convert a byte array to a stream.? Thanks Andrew
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
6
by: | last post by:
Hi, How do I convert a byte to an object? I am trying to use IADs PutEx and the last parameter required a variant array. Thanks, Alex
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.