473,748 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I convert a byte array to an object ?

108 New Member
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.StreamC orruptedExcepti on 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 13398
Nepomuk
3,112 Recognized Expert Specialist
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
7307
by: Lou | last post by:
How do you convert a byte array to a string?
6
15321
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 the another client in C#. Server side can not be changed :( So, I am using tcp/ip sockets. In the end I get byte on the client side, which originally was some C++ class object, converted to byte array.
5
134765
by: Andrew Inwards | last post by:
Can anyone tell me how to convert a byte array to a stream.? Thanks Andrew
2
9605
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 "System.Byte". How do I do this with System.Convert.ToString()???
5
4196
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
29728
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 BitConverter, I get 04030201.
6
4156
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
43110
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 byte array (byte). Can anyone tell me how to convert byte array to byte pointer in CSharp? Please show me an simple example about how to do it. Thank you for your help
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8252
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.