Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从InputStream读取数据时字节数减少_Java_Android_Sockets_Inputstream_Fileoutputstream - Fatal编程技术网

Java 从InputStream读取数据时字节数减少

Java 从InputStream读取数据时字节数减少,java,android,sockets,inputstream,fileoutputstream,Java,Android,Sockets,Inputstream,Fileoutputstream,下面是一个场景…我正在android中进行蓝牙文件传输。首先我发送文件名,然后一旦收到文件名,我就发送文件并接收它 下面是接收文件名和文件名的代码 while (hasFile) { try { if (Flags.isGotFileName()) { mmInStream.mark(0); bytes = mmInStream.read(buffer,0

下面是一个场景…我正在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;
                }


            }
        }

我的问题是,当我尝试接收文件(else-part)时,当我得到文件名(if-part)时,它是8个字节,但它的大小比原始文件小8个字节


我不熟悉套接字通信,所以我不知道如何处理此问题。

比原始文件少8字节?你正在读取前8个字节来获取文件名?这可能是由于第一次读取造成的吗?是的,很明显……但是考虑到我的情况,我不知道如何读取完整的文件。另外,我对此感到困惑((hasdata=mmInStream.read(buffer))!=-1)为什么-1在8个字节之前。这是因为您已经从发布的代码第4行的流中读取了8个字节。如果您希望这些字节成为文件的一部分,您应该在读取更多数据之前将它们写入您的FileOutputStream,或者使用PushbackInputStream,但这些是文件名…8个字节后的实际文件统计信息…请参阅文件大小为794246字节,如果排除前8个字节,我只得到794238字节。@ControlAltDel请参阅我编辑了代码