Java. Reading from BufferedInputStream and write to FileOutputStream

sanatik picture sanatik · Sep 13, 2013 · Viewed 12.4k times · Source

I'm trying to write client and server side on java. Server side is ok (checked on several clients).

So, the problem is on client side. I allocate memory for bytearray, read from BufferedInputStream and write to that bytearray. Then writing from bytearray to FileOutputStream. Everything is ok but free space of bytearray is filling by NULLs, so received file is not correct(e.g. images).

I found 2 decision of that problem:

  1. Read to bytearray till the end of file(but I don't know where the end of file)
  2. Read from BufferedInputStream to FileInputStream, but it doesn't work:

I actually need receive header and file. Output header to console and write the file to the disc.

Full Source

public class SClient {
private static int bufferSize = 8192;
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println("Enter the address:");
    BufferedReader bufferRead = new BufferedReader
                (new InputStreamReader(System.in));

    try {
        String address = bufferRead.readLine();
        System.out.println("Enter the extention of receiving file:");
        String fileExt = bufferRead.readLine();
        // TODO code application logic here
        Socket socket = new Socket(address,4040);
        BufferedInputStream bis = new BufferedInputStream
                (socket.getInputStream());

        BufferedOutputStream bout = new BufferedOutputStream
                (socket.getOutputStream());
        System.out.println("Enter the request:");
        String message = bufferRead.readLine();// GET /index.html HTTP/1.0

        System.out.println("Header read");
        if(message!=null){
            bout.write(message.getBytes());
        }
        FileOutputStream fout = new FileOutputStream("out"+fileExt);
        String s1 = "\r\n\r\n";
        bout.write(s1.getBytes());
        bout.flush();
        System.out.println("Header sent");

        byte[] res = new byte[bufferSize];
        int got;
        while((got = bis.read(res))!=-1){
            fout.write(res,0,got);
        }
        fout.close();
        bout.flush();
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

Server side source:

    String endLine = "\r\n";
    File f = new File(fileName);
    FileInputStream fstream;
    fstream = new FileInputStream(f);
    response = "HTTP/1.0 200 OK" + endLine;
    header = "Content-type: "+ contentType + endLine + "Content-length: " + f.length() + endLine + endLine;
    bout.write(response.getBytes());
    bout.write(header.getBytes());
    while(fstream.read(buffer) != -1) {
        bout.write(buffer);
    }
    System.out.println("Message sent");
    bout.flush();
    socket.close();

Answer

Matthias picture Matthias · Sep 13, 2013

You have to remember the amount of bytes you read into your buffer and must only write those bytes back. Like this here:

int got;
while ((got = bis.read(res)) != -1) {

    fout.write(res, 0, got);
}