Created
May 15, 2012 11:17
-
-
Save roundhill/2700928 to your computer and use it in GitHub Desktop.
Remove junk characters before xml in an InputStream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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