4

I am trying to figure out how to simply exclude the BOM while using the example given by Apache. I am reading a file from Internal Storage and converting it first into a String. Then I convert it into ByteArray so that I get an InputStream. Then I check with BOMInputStream for BOMs, since I had errors for "Unexpected Tokens". Now I don't know how to exclude the BOM if I have it.

CODE:

StringBuffer fileContent = new StringBuffer("");
String temp = "";
int ch;
try{
    FileInputStream fis = ctx.openFileInput("dataxml");
try {
    while( (ch = fis.read()) != -1)
        fileContent.append((char)ch);
        temp = temp + Character.toString((char)ch);
} catch (IOException e) {
    e.printStackTrace();
}
} catch (FileNotFoundException e) {
    e.printStackTrace();
}


InputStream ins = new ByteArrayInputStream(temp.getBytes(StandardCharsets.UTF_8));
BOMInputStream bomIn = new BOMInputStream(ins);
if (bomIn.hasBOM()) {
    // has a UTF-8 BOM

}

xpp.setInput(ins,"UTF-8");
parseXMLAndStoreIt(xpp);
ins.close();

The filename is "dataxml", which I store in different Class with openFileOutput.

3 Answers 3

11

You can wrap initial stream in BOMInputStream:

    InputStream stream = new BOMInputStream(inputStream);
    // code using stream goes here

This way stream skips BOM prefix automagically. BOMInputStream lives in Apache Commons IO library.

2
  • 2
    BOMInputStream! Where have you been all my life?! Commented Dec 2, 2021 at 16:50
  • If you don't want to exclude the BOM-> InputStream stream = new BOMInputStream(inputStream, false);
    – chujudzvin
    Commented Apr 21, 2023 at 11:50
1

I've never used BOMInputStream before but to exclude a byte order mark from the stream you'd just have to read starting at an offset that is one greater than the location of the end of the BOM. Does BOMInputStream have a property indicating the location of the BOM? Also, you can have a look here: http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

3
  • I am thinking of something else currently. Is it possible to just use the FileInputStream as the InputStream for the XmlParser? Commented Nov 25, 2014 at 21:01
  • Absolutely you can. I notice you are reading 1 char at a time so you can ID the BOM as it comes in and only append when you have read the last BOM char. ( BTW you can read at much larger blocks like 4096 but that would make it impossible to find the bom) Commented Nov 25, 2014 at 21:04
  • I will post another question because I dont want to use BOM if not necessary in m case. I have stored a file on internal storage. Now I use FileInputStream to open it. Why doesn't my XMLPULLPARSER recognize it ? In my App I put a Word that needs to be found in the file. I know the Word exists, but the App does not return that it finds it. Commented Nov 25, 2014 at 21:06
1

You are building a String reading characters from an InputStream disregarding BOM and encoding. The way you read characters from the steam converting one byte to one character is bad, very bad. Please use any implementation of Reader (specifying the encoding) to read characters from a sequence of bytes.

Later you convert the String back to bytes (and there you take care specifying the encoding. If you compare the sequence of byte you obtain at this point, it is probably very different than the one you fetched from your store.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.