I need to serialize
ArrayList<String> arr=new ArrayList<String>();
on server and then send this array to client.
I serialize it and write to socket.getOutputStream():
ByteArrayOutputStream bos=new ByteArrayOutputStream();
try{
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(arr);
oos.close();
socket.getOutputStream().write(bos.toByteArray());
}catch(IOException exc)
{
System.out.println("Series erro!");
}
Client receives bytes from server
byte[] buf=new byte[1024*100];
//данные приходные
int r=s.getInputStream().read(buf);
And how to check if it is my ArrayList and then deserialize or just a String?
You don’t have to check. You wrote a serialized object: it’s a serialized object. Read it with
ObjectInputStream.readObject().You can check what type of serialized object with
instanceof.And you don’t need the ByteArrayOutputStream there. Just construct
new ObjectOutputStream(socket.getOutputStream())and do thewriteObject()directly. You’re just wasting time and space and money this way.