0

I am first transferring a file from a client to my master, the stores the byte array and then sends to the slave. Where the slave stores the byte array. But when The file is sent properly from client to master but when I send the byte array to the slave it to the slave the read method in input stream constantly reads 0.

// This method writes the file to the master

public void writeFile(File file) {
    try {
        this.write(String.valueOf(file.length()));

        byte[] bytearray = new byte[(int) file.length()];
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream bin = new BufferedInputStream(fin);
        bin.read(bytearray, 0, bytearray.length);
        BufferedOutputStream bos;
        OutputStream os = socket.getOutputStream();
        bos= new BufferedOutputStream(os);
        bos.write(bytearray, 0, bytearray.length);
        bos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

//This method reads the file into the master as a byte array and the byte array from the master into slave

public byte[] readFile() {

    byte[] bytearray = null;
    try {
        int currentTot = 0;
        int filesize = Integer.parseInt(this.read());
        System.out.println(filesize);
        bytearray = new byte[filesize];
        InputStream is = socket.getInputStream();
        int bytesRead;

        bytesRead = is.read(bytearray, 0, bytearray.length);
        currentTot = bytesRead;
        int count = 0;
        do {
            bytesRead = is.read(bytearray, currentTot, (bytearray.length - currentTot));
            if (bytesRead > 0) {
                currentTot += bytesRead;
                count = 0;
            } else {
                count++;
                System.out.println("count " + count);
            }
        } while (bytesRead > -1);
        System.out.println(currentTot);
        // bos.write(bytearray, 0, currentTot);
        // bos.flush();
        // bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytearray;
}
//This method writes from the master to the slave 
public void writeByte(byte[] m) {
        this.write(String.valueOf(m.length));
        System.out.println("File side inside sender" + m.length);
        // byte[] bytearray = m;
        OutputStream os;
        try {
            os = socket.getOutputStream();
            os.write(m, 0, m.length);
            os.flush();
            //os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Interestingly if I close my output stream after I send my byte array from my master it works well. But I cannot close stream because the slave needs to communicate with the master further. Thanks in advance.

public void write(String output) {
    if (pw == null)
        this.openWriter();
    pw.println(output);
}
public String read() {
    try {
        if (br == null) {
            if (this.socket != null)
                br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        }
        return br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Krish
  • 159
  • 3
  • 14

1 Answers1

1

You're misreading the file length in the receiver. You are getting zero, so you're constructing a zero length byte array, so read() returns zero.

You need to send the length via DataOutputStream.writeLong() and read it via DataInputStream.readLong(). And then your sending and receiving code is all wrong as well. See my answer here for complete code.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
  • No I tested that. The entire byteArray is being read. I put break points and checked. Its just the last chunk of the byte array which does not come through – Krish May 08 '16 at 16:44
  • Also I checked your answer I don't know why do you need to read till fileSize>0 because the Javadoc for read clearly states that it will return -1 when there is nothing left in the stream. I tried using that solution and when I use the stream to send the byte array from my Master to one of the slaves the last bit that was not read now gets pushed onto the slave as an extra. – Krish May 08 '16 at 16:48
  • Yes, you are misreading the file length. You are sending it as a string and only reading one byte of it. The only way `read()` can return zero is if you provide a zero length. The cited answer is to a question about sending multiple files, where end of stream cannot be used to delimit the sent file. – user207421 May 08 '16 at 16:50
  • the this.read is a call to a method of the class which reads a string and this.write is a call to the method of the class which writes a string. Updated the code above to reflect that. Also the Javadoc of the Input]Streams read() method states that it returns -1 when there is nothing left to read. – Krish May 08 '16 at 16:56
  • If there is more code that concerns this question you should have provided it. `read()` returns zero if you provide a zero length and -1 if you reach end of stream. You are providing a zero length. The code in the cited answer works. The code in your question doesn't. I don't know why you're arguing about it. – user207421 May 08 '16 at 16:59
  • Hi Thanks. Was not arguing. Was trying to figure what was wrong with mine. I am not getting an issue anymore. But I am getting a slightly different problem where my first packet is getting lost. I sometimes get it sometimes don't. I even tried the readFully method of dataInputStream but still the same issue. Any clues? – Krish May 08 '16 at 23:39