Skip to content

Instantly share code, notes, and snippets.

@roundhill
Created May 15, 2012 11:17
Show Gist options
  • Save roundhill/2700928 to your computer and use it in GitHub Desktop.
Save roundhill/2700928 to your computer and use it in GitHub Desktop.
Remove junk characters before xml in an InputStream
int bomCheck = -1;
int stopper = 0;
while ((bomCheck = is.read()) != -1 && stopper <= 5000) {
stopper++;
String snippet = "";
//60 == '<' character
if (bomCheck == 60) {
for (int i = 0; i < 4; i++) {
byte[] chunk = new byte[1];
is.read(chunk);
snippet += new String(chunk);
}
if (snippet.equals("?xml")) {
//it's all good, add xml tag back and start parsing
String start = "<" + snippet;
List<InputStream> streams = Arrays.asList(
new ByteArrayInputStream(start.getBytes()),
is);
is = new SequenceInputStream(Collections.enumeration(streams));
break;
} else {
//keep searching...
List<InputStream> streams = Arrays.asList(
new ByteArrayInputStream(snippet.getBytes()),
is);
is = new SequenceInputStream(Collections.enumeration(streams));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment