473,418 Members | 2,055 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,418 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 13224
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.