I am dealing with a Java method that stops working silently. I've put catches around the method and am trying to catch Throwables, but I don't catch anything. I also tried debugging, but the method just stops working on a line for no reason.
I have a simple client-server app and the client sends a file to the server. Here's the relevant code for the server handling the file transfer:
public void receiveFile(String filePath) throws IOException
{
InputStream inStream = null;
FileOutputStream outStream = null;
try {
inStream = fSocket.getInputStream();
File f = new File(filePath);
if (f.exists()) {
throw new FileAlreadyExistsException(filePath + " already exists!");
}
outStream = new FileOutputStream(filePath);
byte[] bytes = new byte[8192];
int count;
while ((count = inStream.read(bytes)) > 0) {
outStream.write(bytes, 0, count);
}
} catch (Throwable t) {
System.out.println("Error when fetching file");
System.out.println(t.toString());
} finally {
if (outStream != null) outStream.close();
}
}
I can get the file fine. When I look in my files, the file has been transfered and is complete. For some reason, as soon as the transfer is done, my problem comes with the line while ((count = inStream.read(bytes)) > 0)
. I tried debugging, and when it gets to that line when the transfer is done, it just stops. I have no idea where the execution goes. Here's a gif of the issue.
Does anyone know what this could be? I am using threads if that's of any relevance.
Here's the client code:
public void sendFile(String fileName) throws IOException
{
InputStream inStream = null;
OutputStream outStream = null;
try {
File file = new File(fileName);
byte[] buffer = new byte[8192];
inStream = new FileInputStream(file);
outStream = fSocket.getOutputStream();
int count;
while ((count = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, count);
}
System.out.println("Done sending data");
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (outStream != null) outStream.flush();
if (inStream != null) inStream.close();
}
}