1

I'm trying to Stream file from one device to another device,

Sender Code :

BufferedOutputStream bos= new BufferedOutputStream(mmOutStream);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer = new byte[8192];
while((n = bis.read(buffer))>-1)
  bos.write(buffer,0,n);

and Receiver code is

BufferedInputStream bis = new BufferedInputStream(mmInStream);
FileOutputStream fos = null;
try {
  fos = new FileOutputStream(file);
  int n;
  byte[] buffer = new byte[8192];
  while ((n = bis.read(buffer)) > 0) {
    fos.write(buffer, 0, n);
  }

  Log.e("Complete","Stream read done");
}catch (Exception ex) {
  ex.printStackTrace();
}

My problem is when I send any file from my device on receiving end fos.write(buffer, 0, n); is called but how will we detect stream write is complete, because Log.e("Complete","Stream read done"); this log never got printed. So how will we detect stream write complete?

On reciver Add log inside While loop

while ((n = bis.read(buffer)) >= -1) {
  Log.e("Complete ",""+n);
  fos.write(buffer, 0, n);
}

and log is:

01-02 02:37:19.285 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.293 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.297 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.300 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.304 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.308 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.336 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.339 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 01-02 02:37:19.339 2940-3010/com.ganesh.bluetoothterminal E/Complete: 280 01-02 02:37:19.347 2940-3010/com.ganesh.bluetoothterminal E/Complete: 989 . . . . . 01-02 02:37:20.058 2940-3010/com.ganesh.bluetoothterminal E/Complete: 1237

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
Ganesh P
  • 11
  • 3

0 Answers0