从InputStream读取数据时的字节数较少

时间:2015-10-26 20:06:04

标签: java android sockets inputstream fileoutputstream

所以这是场景......我在android中进行蓝牙文件传输。首先我发送文件名,然后一旦收到文件名,我发送文件并接收它。

以下是接收文件名和文件的代码

 while (hasFile) {


            try {
                if (Flags.isGotFileName()) {
                    mmInStream.mark(0);
                    bytes = mmInStream.read(buffer,0,8);

                    Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();

                } else {
                    mmInStream.reset();
                    int KB = 0;
                    int compKB = 0;
                    int hasdata = 0;                    
                    File file = new File(Environment.getExternalStorageDirectory(), Flags.getFileName());
                    fos = new FileOutputStream(file);
                    hasdata=mmInStream.read(buffer,8,1016);
                    fos.write(buffer, 0, hasdata);
                    while ((hasdata = mmInStream.read(buffer)) != -1) {
                        Log.e(TAG, KB + "KB Copied");
                        fos.write(buffer, 0, hasdata);
                        KB++;
                        if (KB - compKB == 200) {
                            Log.e(TAG, KB + "KB Copied");
                            compKB = KB;
                        }
                    }
                }

            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothChatService.this.start();
                break;
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    Log.e("FOSSSS", "CLOSEDDDD");
                    hasFile = false;
                }


            }
        }

我的问题是,当我收到文件名(如果是部分)时,当我尝试接收文件(其他部分)时,它会收到8个字节,但是它的大小比原来的小8个字节

我是套接字通信的新手,所以我无法弄清楚如何处理这个问题。

0 个答案:

没有答案