0

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); 
Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71
  • What do you use to write the bytes. Make sure to use writeObject(bytes) and not write(bytes). – JB Nizet Dec 05 '11 at 22:30
  • The following works on my box: http://pastebin.com/xP2T0Ux2. There must be somthing wrong with the way you write the data. Please post an SSCCE reproducing the problem. – JB Nizet Dec 05 '11 at 22:43
  • There really isnt that much more to it. Cannot give you a runnable SSCCE but heres all that matters anyway: http://pastebin.com/ybx4ZK7K. Difference from your example is that i send my bytes by network, and there by never get to use ByteArrayInputStream. – Lucas Arrefelt Dec 05 '11 at 23:53
  • Try reading the file correctly (read() doesn't necessarily fill the whole buffer), try flushing the output stream, and don't start a thread in the constructor. – JB Nizet Dec 06 '11 at 08:35
  • Bah, i tried to create a new objectoutput stream that was the problem >.<. Whats the problem with starting the thread in a contructor? Havent come upon any and i thought it only was when i extend Thread. – Lucas Arrefelt Dec 06 '11 at 22:53
  • See http://stackoverflow.com/questions/8057510/java-starting-a-new-thread-in-a-constructor – JB Nizet Dec 07 '11 at 07:53

1 Answers1

1

You should use buffered input output stream instead of object. As object stream are intended to work with serialization and deserialization of java objects.

Jaroslaw.zawila
  • 509
  • 2
  • 4
  • 14