i have a run method that launches a new thread and want to launch another method from it after it ends but this is not working. at the end of the method i put some printout statements and they only show a few times like when the i first load the java program. usually they don't show.
i wanted to know why they are not showing up and it there is a reliable way like a callback method such as in AsyncTask's onPostExecute() that i can use on Java threads.
have no idea why the printout statements don't show 98% of the time. any ideas?
a little confused here. as i thought that the file would reach the end and the count variable in the while loop and it would get to 0 or -1 then the while loop would end and the execution of the process and it would exit the while loop and continue on with the thread. this is not happening and it does not make sense.
the file i am receiving with this code is complete and there are no problems. however i need to launch another thread method to send a message back to the Android client to tell it that the file had been received successfully.
the rest of the code
@Override
public void run() {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
int bufferSize = 0;
File file = new File("/Users/UserName/sampleFile.db");
// get input streams
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bis = new BufferedInputStream(socket.getInputStream());
dis = new DataInputStream(bis);
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
}
try {
bufferSize = socket.getReceiveBufferSize();
} catch (SocketException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
}
int fileSizeFromClient = 0;
long serialNumber = 0;
try {
fileSizeFromClient = dis.readInt();
serialNumber = dis.readLong();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
}
String serialString = String.valueOf(serialNumber);
System.out.println("filesize " + fileSizeFromClient);
System.out.println("serialNumber " + serialString);
System.out.println("bufferSize " + bufferSize);
int count = 0;
try {
bos.flush();
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] buffer = new byte[fileSizeFromClient];
try {
while((count = dis.read(buffer)) > 0){
bos.write(buffer, 0, count);
if(count == -1){ // <-- this line does not help break out of loop
break;
}
}
System.out.println("size of file written " + buffer.length); // <-- does not print out
socket.close();
System.out.println("test out 1"); // <-- does not print out
} catch (IOException ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(MultiThreader.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("test out 2"); // <-- does not print out
} // end run
EDIT: forgot to mention that this is for a Java desktop app, not Android, but the client it is connecting to is an Android device. there is no problem with the sending and receiving of any file, socket and TCP/IP file networking has no problems