Put an myObject to ObjectInputStream without using ObjectOutputStream - java

I want to put an object in an ObjectInputStream. I wrote the following code to write the object to an ObjectOuputStream and saved it in "myObjectFile". Then read "myObjectFile" to get an ObjectInputStream:
val oos = new ObjectOutputStream(new FileOutputStream("myObjectFile"))
oos.writeObject(myObject);
oos.close();
val ois = new ObjectInputStream(new FileInputStream("myObjectFile"))
:
ois.close();
Is it possible to create an ObjectInputStream for myObject directly without writing an intermediate "myObjectFile"?
I also tried the following:
val baos = new ByteArrayOutputStream
val oos = new ObjectOutputStream(baos)
oos.writeObject(myObject);
oos.flush();
oos.close();
val is = new ByteArrayInputStream(baos.toByteArray())
val ois = new ObjectInputStream(is)
println(" baos size = " + baos.size())
println(" ois size = " + IOUtils.toByteArray(ois).length)
But I got:
baos size = 369
ois size = 0
Why the ois size is 0? What did I do wrong? Thanks!

Related

Java ObjectOutputStream Not Writing to ZipEntry

I am trying to serialize an object into a ZipEntry using an ObjectOutputStream, however it doesn't appear to be writing anything because when I print the byte array produced, it shows null. I tried writing a string with the ZipOutputStream, and upon printing the resulting byte array got a sizeable result. SO my question is: why is the objectoutput stream not correctly writing into the ZipEntry. (ConfigEntry does implement Serializable).
String s = "Tired, Exhausted";
ConfigEntry con = new ConfigEntry("rand", "random", 3);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ZipOutputStream zos = new ZipOutputStream(baos);
ZipEntry entry = new ZipEntry("test.txt");
ObjectOutputStream obs = new ObjectOutputStream(zos);
zos.putNextEntry(entry);
obs.writeObject(con);
obs.close();
zos.closeEntry();
zos.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
os = bs.getOutputStream();
byte[] result = baos.toByteArray();
String test = new String(result, "UTF-8");
Log.v("Mac Address", test);
Log.v("Mac Address", Arrays.toString(result));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
This baos goes out of scope after the try block. You are writing to one baos and you are looking into another baos declared in an outer scope, probably an instance member of the class.

Serialization issues

public void writeObject(String outFile) {
try {
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Student[] copy = this.getStudents();
for (Student st : copy){
oos.writeObject(st);}
oos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
The code above is the function I use to serialize the contents of my repository,getStudens() is returning an array of my data.
public void readSerialized(String fileName) throws Exception {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
while(fis.available()>0){
ctrl.addC((Student) ois.readObject());}
ois.close();
}
This my deserialization function which should recreate my data and add it again in my repository.The problem is that it doesn't recreate the data I had in my repository when I serialized it first.
What I had in repository before serialization:
1 a 4.0 6.0
2 b 10.0 10.0
3 c 2.0 2.0
4 d 8.0 2.0
5 e 6.0 2.0
What the deserialization returns:
0 3.0
0 5.0
Does this means that my serialization function isn't correct or something goes wrong when I deserialize?
Your code is needlessly complicated and using available() is always rather confusing I find. It means you can read without a system call it doesn't mean there is nothing left.
I suggest just serializing the array.
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.getStudents());
oos.close();
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
Student[] copy = (Student[]) ois.readObject();
ois.close();
In Java, arrays are objects too.

Convert android.graphics.Path object to byte[] and back

I'm trying to convert an android.graphics.Path object to byte[] so that I could store it in a blob storage in SQLite, also to convert it back.
So far I don't even know where to begin...
Thanks to anyone willing to help.
As Path extends Object, you can use something like this:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(path);
byte[] array = outputStream.toByteArray();
Serialize your object and upload that file .
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ObjectOutput out = new ObjectOutputStream(baos);
out.writeObject(android.graphics.Path);
out.close()
byte[] buf = bos.toByteArray(); //byte array
to recover that object use deserialization
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf));
class_name recover =(clas_name) in.readObject();
in.close();
return object;

Convert byte [] to ArrayList<Object>

I have a byte[] that i obtained using Object ArrayList<Obj>
Can anyone tell me how to convert my byte[] to Object ArrayList?
Coveting ArrayList like this:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(bos);
oos.writeObject(mArrayList);//mArrayList is the array to convert
byte[] buff = bos.toByteArray();
Now you've given us the information about how you did the conversion one way... you need:
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
try {
#SuppressWarnings("unchecked")
ArrayList<Object> list = (ArrayList<Object>) ois.readObject();
...
} finally {
ois.close();
}
I'm going to go with the obvious answer here...
for(byte b : bytearray) {
arraylist.add(new Byte(b));
}

How can I convert an Object to Inputstream

How can I convert the java Object into a InputStream?
You can use ObjectOutputStream
You write the object (obj in the code below) to the ObjectOutputStream, your object you want to convert to an input stream must implement Serializable.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
oos.flush();
oos.close();
InputStream is = new ByteArrayInputStream(baos.toByteArray());

Categories

Resources