0

So here is the scenario...I am making bluetooth file transfer in android. First I am sending file name and then once file name is received I send file and receive it too.

Here is the code to receive the file name and file

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


            }
        }

My problem is that after I get my file name (if part) which is of 8 bytes when I try to receive the file (else part) it is received but its size is 8 bytes less than the original one

I am new to socket communication so I cant figure out how to deal with this.

11
  • 8 bytes less than the original file? And you're reading the first 8 bytes to get the file name? Could it be caused by that first read? Commented Oct 26, 2015 at 20:16
  • Yes obviously....but I don't know how to read the full file considering my scenario. Also I am confused about this while ((hasdata = mmInStream.read(buffer)) != -1) how come -1 comes before 8 bytes. Commented Oct 26, 2015 at 20:20
  • It's because you've already read those 8 bytes off of your stream on line 4 of the code you posted.If you want those bytes to be part of your file, you should write them to your FileOutputStream before reading more data, or use a PushBackInputStream Commented Oct 26, 2015 at 20:25
  • But those are file name....actual file stats after 8 bytes...see the file size is 794246 bytes and I am getting only 794238 bytes if I exclude the first 8 bytes. Commented Oct 26, 2015 at 20:30
  • @ControlAltDel see I edited the code Commented Oct 26, 2015 at 20:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.