I have designed a sender-reciever type program where the sender sends a file and the reciever recieves it automatically. The problem is that though I am using byte-based streams,the output in ceratin cases as follows are getting distorted:
[A]: Bitmap images are getting color-inverted.
[B]: Files other than text files are not uploaded properly,like DOCX files open up corrupt.

Here's the code for the sender:

public void transferFile(String file) throws Exception{
    FileInputStream fis=new FileInputStream(fileToUpload.getText());
    Socket s=null;
    s=ss.accept();
    OutputStream out=s.getOutputStream();
    byte ch[]=new byte[4096];
    while(fis.read(ch)!=-1){
        out.write(ch,0,ch.length);
    }
    out.close();
    s.close();
    }

For the reciever:

public void recieveFile(String fileName) throws Exception{
        s=new Socket(controller,1911);
        InputStream in=s.getInputStream();
        FileOutputStream fos=new FileOutputStream(new File(fileName));
        byte ch[]=new byte[4096];
        while(in.read(ch)!=-1)
        fos.write(ch,0,ch.length);
        fos.close();
        in.close();
        s.close();      
    }

Before going any further I would catch and printStackTrace any Exceptions, rather than throwing them up and hoping for the best.
You are writing your output streams with the assumption that every read returns a full buffer. This is a bad assumption, especially when reading a socket stream. Also it will increase the file length and pad the end of the file with garbage.
The read method returns the actual number of bytes read, so that is what you should use in the writes, rather than ch.length

Okay,improving upon the write() method,here's what I tried
~~~~Sender~~~~

public void transferFile(String file) throws Exception{
    FileInputStream fis=new FileInputStream(fileToUpload.getText());
    Socket s=null;
    s=ss.accept();
    OutputStream out=s.getOutputStream();
    byte ch[]=new byte[4096];
    while(fis.read(ch)!=-1){
        out.write(ch);
    }
    out.close();
    s.close();
    }

~~~~Reciever~~~~

public void recieveFile(String fileName) throws Exception{
        s=new Socket(controller,1911);
        InputStream in=s.getInputStream();
        FileOutputStream fos=new FileOutputStream(new File(fileName));
        byte ch[]=new byte[4096];
        while(in.read(ch)!=-1)
        fos.write(ch);
        fos.close();
        in.close();
        s.close();      
    }

But this doesn't produce any different output.
Also what I noticed is that if I flushed the streams after exiting the while loop,the bitmap image has half of its upper half color inverted and the lower half as required/expected.

You are still ignoring the actual number of byes read by your reads.

Sorry,but I don't get your suggestion.It would be helpful if you show what changes be made to the above code.

You need to see how many bytes were actually read into the array (the array may not have been filled)
int numBytes = in.read(ch) ...
then write the same number of bytes
write(ch,0,numBytes)

Thank You JamesCherrill.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.