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));
}
Related
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!
I have a code where I am converting array list to byte array and then saving that byte array as a BLOB in MySQL database. Below is code:-
Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
List extraAttributes = (ArrayList) temp;
resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));
The method createByteArray is defined as below:
private byte [] createByteArray( Object obj)
{
byte [] bArray = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objOstream = new ObjectOutputStream(baos);
objOstream.writeObject(obj);
bArray = baos.toByteArray();
}
catch (Exception e)
{
TraceDbLog.writeError("Problem in createByteArray", e);
}
return bArray;
}
Well the above code was written earlier for writing HashMap to BLOB i am using same for converting ArrayList if HashMap to BLOB.
The problem which is occurring in read code when i am reading the blob.
private Object readBytes (ResultSet rs, String columnName)
throws SQLException
{
ObjectInputStream ois = null;
byte [] newArray;
Object obj = null;
try
{
newArray = rs.getBytes(columnName);
ois = new ObjectInputStream (new ByteArrayInputStream(newArray));
obj = ois.readObject ();
}
In the read part the object is not coming as arrayList of hasMap and in debug perspective in eclipse eclipse is also not able to inspect the object which is coming.
I have also tried typecasting the object to List but still no success in getting the right response.
Please tell me whether there is any flaw in reading/writing the above BLOB.
I have added sample coding for convert ArrayList to byte[].
One reasonable way would be to use UTF-8 encoding like DataOutputStream does for each string in the list. For a string it writes 2 bytes for the length of the UTF-8 encoding followed by the UTF-8 bytes.
This would be portable if you're not using Java on the other end. Here's an example of encoding and decoding an ArrayList:
// example input list
List<String> list = new ArrayList<String>();
list.add("foo");
list.add("bar");
list.add("baz");
// write to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);
for (String element : list) {
out.writeUTF(element);
}
byte[] bytes = baos.toByteArray();
// read from byte array
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream in = new DataInputStream(bais);
while (in.available() > 0) {
String element = in.readUTF();
System.out.println(element);
}
The easiest way is to convert it to json string and then to bytes
Gson gson = new Gson();
Type type = new TypeToken<List<Alarm>>() {}.getType();
String json = gson.toJson(list, type);
byte[] bytes = json.getBytes();
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;
I have an array list that I want to serialize please advise how to I will be able to do that..
ArrayList list=new ArrayList();
list.add("Ram");
list.add("Sachin");
list.add("Dinesh");
list.add(1,"Ravi");
list.add("Dinesh");
list.add("Anupam");
System.out.println("There are "+list.size()+" elements in the list.");
System.out.println("Content of list are : ");
Iterator itr=list.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
I want to use the serialization mechanism so that I can save it in file
It is very simple. Both ArrayList and String (that you store in the list) implement Serializable interface, so you can use the standard java mechanism for serialization:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myfile"));
oos.writeObject(list);
............
oos.fluch();
oos.close();
In this example I wrapped FileOutputStream with ObjectOutputStream but obviously you can use any other payload stream.
You have to create your own methods for serializing and deserializing objects. The below are useful methods for doing just that.
public static Object deserializeBytes(byte[] bytes) throws IOException, ClassNotFoundException
{
ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bytesIn);
Object obj = ois.readObject();
ois.close();
return obj;
}
public static byte[] serializeObject(Object obj) throws IOException
{
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bytesOut);
oos.writeObject(obj);
oos.flush();
byte[] bytes = bytesOut.toByteArray();
bytesOut.close();
oos.close();
return bytes;
}
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());