Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude BOM with BOM InputStream

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.

like image 565
David Kasabji Avatar asked Sep 21 '25 11:09

David Kasabji


1 Answers

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.

like image 165
user2910552 Avatar answered Sep 23 '25 01:09

user2910552