Getting Object back from an ObjectOutputStream - java

I have created an ObjectOutputStream
ObjectOutputStream stream = new ObjectOutputStream(new ByteArrayOutputStream());
stream.writeObject(myObject);
but how do I now convert this back into an Object, or even a ByteArray?
I've tried getting an ObjectInputStream like this
ByteArrayOutputStream outputStream = (ByteArrayOutputStream) myProcess.getOutputStream();
final ObjectInputStream objectInputStream = new ObjectInputStream(
new ByteArrayInputStream(outputStream.toByteArray()));
however I get a compile error saying it can't cast the ObjectOutputStream to a ByteArrayOutputStream; yet there seem to be no methods on the ObjectOutputStream to get the data back?

Here how you do it
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(baos);
stream.writeObject(myObject);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream inputStream = new ObjectInputStream(bais);
Object o = inputStream.readObject();

Related

java.io.StreamCorruptedException: invalid stream header when sending an image

In my server i'm trying to split an image and send it to the clients (here are called resources). The problem is that I get StreamCorruptedException when I read the object.
Server side:
Socket resSocket = resourceQueue.get(k).getSocket();
DataOutputStream dos = new DataOutputStream(resSocket.getOutputStream());
FileInputStream fis = new FileInputStream(chunksList.get(i));
byte[] data = new byte[fis.available()];
fis.read(data);
ObjectOutputStream oos = new ObjectOutputStream(resSocket.getOutputStream());
oos.writeObject(data);
oos.close();
Client side
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); // Here i get the error
byte[] buffer = (byte[]) ois.readObject();
String path = "c:/JGCF/temp."+"jpg";
System.out.println(path);
FileOutputStream fos = new FileOutputStream(path);
fos.write(buffer);

ObjectInputStream over DeflaterInputStream throws StreamCorruptedException [duplicate]

This question already has answers here:
DeflatorInputStream and DeflatorOutputStream do not reconstruct the original data
(3 answers)
Closed 5 years ago.
I'm trying to use ObjectOutputStream over a DeflaterOutputStream to write deflated data into an underlying stream. But when I try to read the data with their InputStream counterparts, an exception is thrown. It is worth of note that replacing Deflate{Output,Input}Stream with GZip{Output,Input}Stream, it works as expected. An example code that shows this behavior can be seen below:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream oos = new ObjectOutputStream(new DeflaterOutputStream(baos))) {
oos.writeObject("test");
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try(ObjectInputStream oos = new ObjectInputStream(new DeflaterInputStream(bais))) {
System.out.println(oos.readObject());
}
It throws the following exception:
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 789CAB98
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:857)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
at Main.main(Main.java:23)
Does anyone know why exacly this happens?
I've already figured it out and it is a silly mistake. But answering my own question so future people don't fall for it again:
The inverse class of DeflaterOutputStream is InflaterInputStream, not DeflaterInputStream. So the code should look like the one below:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream oos = new ObjectOutputStream(new DeflaterOutputStream(baos))) {
oos.writeObject("test");
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
try(ObjectInputStream oos = new ObjectInputStream(new InflaterInputStream(bais))) {
System.out.println(oos.readObject());
}

Deserializable exception : local class is not compatible cause of serialVersionID

Here is my code:
MyOwnObject deserializedObject = null;
try{
ByteArrayInputStream bis = new ByteArrayInputStream(serializedObject.getBytes());
ObjectInputStream ois= new ObjectInputStream(bis);
deserializedObject = (MyOwnObject)ois.readObject();
ois.close();
}catch(Exception e){
e.printStackTrace();
}
someMapper.insert(deserializedObject);
PS: serializedObject is a string which i get from serialization process before, and it's working well i think.
The code throws an exception:
local class incompatible: stream classdesc serialVersionUID = 1360826667802527544, local class serialVersionUID = 1360826667806852920
And in the stacktrace there's something about the type Integer of some attribute in my object.
UPDATE:
serializeObject is a string,from this code:
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(myObject);
so.flush();
serializedObject = bo.toString();
}catch (Exception e) {
System.out.println(e);
}
ANSWER:
//Serialization from object to string
String serializedObject="";
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
serializedObject = new String(Base64.encode(baos.toByteArray()));
oos.close();
}catch(Exception e){
e.printStackTrace();
}
//Deserialization from string to object
MyOwnObject deserializedObject = null;
try{
byte[] bytes = Base64.decode(serializedObject.getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
deserializedObject = (MyOwnObject)ois.readObject();
ois.close();
}catch(Exception e){
e.printStackTrace();
}
From here,i can use deserializedObject as an object,and it worked well!
The problem is in how you create your serializedObject.
You use a ByteArrayOutputStream. You shouldn't call toString() on it. Instead call its toByteArray() method to get the underlying data as a byte array, and you can use that to create your ByteArrayInputStream and it will work.
Example:
// Serialization
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(myObject);
so.flush();
byte[] serializedObject = bo.toByteArray();
// Deserialization
MyOwnObject deserializedObject = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(serializedObject);
ObjectInputStream ois = new ObjectInputStream(bis);
deserializedObject = (MyOwnObject)ois.readObject();
ois.close();
} catch (Exception e){
e.printStackTrace();
}
A serialized object is a sequence of bytes (a byte array) and not a sequence of characters. You cannot create a String from the bytes of a serialized object, because it may not contain valid unicode codepoints for example.
If you really need to represent the serialized object as a String, try representing the byte array in hex string or use base64 encoding.
besides base64, you can use hex string also!i always use it when i encounter a similar problems!

Pass serilaized object as parameter to HTTPServer in Java

How can I pass a serilaized object as a param in a GET request to my HTTPServer? Please let me know as I have been unable to find a way to do the same.
You can try something like this
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
String prm = URLEncoder.encode(new String(bos.toByteArray(), "ISO-8859-1"), "ISO-8859-1");
On the server side:
servletRequest.setCharacterEncoding("ISO-8859-1");
String s = servletRequeset.getParameter("obj");
byte[] bytes = s.getBytes("ISO-8859-1");
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = ois.readObject();
It's also possible to use Base64 instead of URL Encoding, the main idea is to pass serialized bytes in URL

byte array in java returning null after conversion from objects

I need to send my phone contacts in android as bytes. So i have crated a bean class implementing serializable , but after converting the arraylist of bean class to byte array, byte array is always showing null. Here is my sample code.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(presentContacts);
byte[] buf = baos.toByteArray();
Here presentContacts is the ArrayList of bean class. Byte array, buf is always returning null but presentContacts is not null.
You should probably close or at least flush the ObjectOutputStream.
Something like this
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(presentContacts);
oos.flush();
byte[] buf = baos.toByteArray();

Categories