0

Here is some methods to read file to byte array that I use in my android app.

  • M1:
   private static byte[] readFileAsBytes(String filePath)
            throws java.io.IOException{   
        FileInputStream fisTargetFile = new FileInputStream(new File(filePath));
        String targetFileStr = IOUtils.toString(fisTargetFile, "UTF-8");
        byte[] inputData =  IOUtils.toByteArray(new StringReader(targetFileStr),"UTF-8");
        return inputData;
}
  • M2
 private static byte[] readFileAsBytes(String filePath)
            throws java.io.IOException{

        File file = new File(filePath);
        FileInputStream inputFile = new FileInputStream(file);
        byte inputData[] = new byte[(int)file.length()];
        inputFile.read(inputData);
        inputFile.close();
        return inputData;
    }

I also use method in this ...

But, when I debug, I dectect some redundant bytes, example:

File text: ABCDEF

When debug:
- In M1: inputData: {-17,-69,-65,65,66,67,68,69,70}

I know A -> 65, B -> 66,... But Why appear {-17,-69,-65}

-In M2: Appear many redundancy than M1.

I have searched, but not find same problem.

Any suggestions for me.

Thanks!!!

Community
  • 1
  • 1
DustinPham
  • 91
  • 1
  • 6
  • both snippets are wrong. – Henry Mar 16 '17 at 10:55
  • Please use hexadecimal notation for byte values. Decimal ones are unreadable. – greenapps Mar 16 '17 at 11:08
  • `File text: ABCDEF`. Whith which program/app did you make the file? What is the size of the file? 6 or 9? 9 i believe as you have 9 values. But ABCDEF is only 6 characters. Most probably the first three values are a BOM. A byte order mark. – greenapps Mar 16 '17 at 11:10
  • `-In M2: Appear many redundancy than M1.`??? What are you trying to tell? Dont understand a word of this. Please post the values when using M2. – greenapps Mar 16 '17 at 11:12
  • @greenapps, I make app to encrypt/decrypt file with key 128 bit. Above example is short. Actual text key have 16 characters,,... So, with add first three values, it not make 128 bit. I will read about BOM, Thank you for your answer. – DustinPham Mar 16 '17 at 12:00

1 Answers1

0
 String targetFileStr = IOUtils.toString(fisTargetFile, "UTF-8");
 byte[] inputData =  IOUtils.toByteArray(new StringReader(targetFileStr),"UTF-8");

Do not use intermediate String or StringReader to put the bytes of a file in a byte array.

For instance you cannot put a jpg file in a String as it's no text.

Put the bytes directly in a byte array as you do in the second readFileAsBytes(). That one should work. But you have to check the return value of inputFile.read(inputData); and eventually make a loop.

The three leading bytes are a BOM. A byte order mark. See: https://en.wikipedia.org/wiki/Byte_order_mark

 The UTF-8 representation of the BOM is the byte sequence 0xEF,0xBB,0xBF 
greenapps
  • 11,154
  • 2
  • 16
  • 19
  • The second one is not guranteed to work (though it often will) since the read call need not read the full array nor the full file. – Henry Mar 16 '17 at 12:06