Ive come across something i cant come up with a descent solution for. I send some string[] messages from a server to a client, but now i want to send images aswell. Problem is that i check for objects on the client part, and not for byte[], which gives me problem handling the images when they arrive.
Now i use this for my incoming strings[]:
public void run()
{
while(active)
{
try
{
Object o;
o = input.readObject();
System.out.println("Received from server!");
if ( o instanceof String[])
{
String[] names = (String[]) o;
Refresh.getInstance().update( names );
}
}
catch (OptionalDataException e) { e.printStackTrace(); Terminate(); }
catch (ClassNotFoundException e) { e.printStackTrace(); Terminate(); }
catch (IOException e) { e.printStackTrace(); Terminate(); }
}
}
But now i want this stream to be able to handle both String[] and bytes[], any advice would be great, im lost :(
I would want something like this:
if ( o instanceof byte[])
{
// ...
}
But it doesnt matter since o = input.readObject() gives me this when the image file comes:
12-05 23:00:20.255: W/System.err(16157): java.io.StreamCorruptedException: Wrong format: ac
Code for writing bytes:
FileInputStream fis = new FileInputStream("images\\test.jpg");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
ObjectOutputStream oos = new ObjectOutputStream(serverSocket.getOutputStream()) ;
oos.writeObject(buffer);