检测缓冲流complet检测

时间:2016-10-22 14:17:49

标签: java android

我试图将文件从一个设备流式传输到另一个设备,

发件人代码:

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);

和Receiver代码是

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();
}

我的问题是当我在接收端fos.write(buffer, 0, n);上发送来自我的设备的任何文件时,但我们如何检测流写入是否完成,因为Log.e("Complete","Stream read done");此日志从未被打印过。那么我们如何检测流写完成?

在reciver上添加日志while while循环

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

并且日志是:

  

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

0 个答案:

没有答案