XmlReader extends Reader { static String getXmlProlog(final InputStream is, final String guessedEnc) throws IOException { String encoding = null; if (guessedEnc != null) { final byte[] bytes = new byte[BUFFER_SIZE]; is.mark(BUFFER_SIZE); int offset = 0; int max = BUFFER_SIZE; int c = is.read(bytes, offset, max); int firstGT = -1; while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) { offset += c; max -= c; c = is.read(bytes, offset, max); firstGT = new String(bytes, 0, offset, guessedEnc).indexOf(">"); } if (firstGT == -1) { if (c == -1) { throw new IOException("Unexpected end of XML stream"); } else { throw new IOException("XML prolog or ROOT element not found on first " + offset + " bytes"); } } final int bytesRead = offset; if (bytesRead > 0) { is.reset(); String prolog = new String(bytes, guessedEnc).substring(0, firstGT); final Matcher m = ENCODING_PATTERN.matcher(prolog); if (m.find()) { encoding = m.group(1).toUpperCase(Locale.ENGLISH); encoding = encoding.substring(1, encoding.length() - 1); } } } return encoding; } XmlReader(final File file);  XmlReader(final InputStream is);  XmlReader(final InputStream is, final boolean lenient, final String defaultEncoding);  XmlReader(final InputStream is, final boolean lenient);  XmlReader(final URL url);  XmlReader(final URL url, final Map<String, String> requestHeaders);  XmlReader(final URLConnection conn);  XmlReader(final URLConnection conn, final Map<String, String> requestHeaders);  XmlReader(final InputStream is, final String httpContentType);  XmlReader(final InputStream is, final String httpContentType, final boolean lenient, final String defaultEncoding);  XmlReader(final InputStream is, final String httpContentType, final boolean lenient); static String getDefaultEncoding(); static void setDefaultEncoding(final String encoding); String getEncoding(); @Override int read(final char[] buf, final int offset, final int len); @Override void close();  }
@Test public void testGetXmlProlog_NoClosingAngleBracket() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"TEST\"", "UTF-8"); final String guessedEncoding = "UTF-8"; expectedException.expect(IOException.class); XmlReader.getXmlProlog(input, guessedEncoding); }
@Test public void testGetXmlProlog_Empty() throws IOException { final InputStream input = stringToStream("", "UTF-8"); final String guessedEncoding = "UTF-8"; expectedException.expect(IOException.class); XmlReader.getXmlProlog(input, guessedEncoding); }
@Test public void testGetXmlProlog_ClosingAngleBracketIsTooFar() throws IOException { final StringBuilder spaces = new StringBuilder(); for (int i = 0; i < 5000; i++) { spaces.append(" "); } final InputStream input = stringToStream("<?xml encoding=\"TEST\"?" + spaces + ">", "UTF-8"); final String guessedEncoding = "UTF-8"; expectedException.expect(IOException.class); XmlReader.getXmlProlog(input, guessedEncoding); }
@Test public void testGetXmlProlog() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"TEST\"?>", "UTF-8"); final String guessedEncoding = "UTF-8"; final String prologEncoding = XmlReader.getXmlProlog(input, guessedEncoding); assertEquals("TEST", prologEncoding); }
@Test public void testGetXmlProlog_Utf8() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"UTF-8\"?>", "UTF-8"); final String guessedEncoding = "UTF-8"; final String prologEncoding = XmlReader.getXmlProlog(input, guessedEncoding); assertEquals("UTF-8", prologEncoding); }
@Test public void testGetXmlProlog_Utf16() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"UTF-16\"?>", "UTF-16"); final String guessedEncoding = "UTF-16"; assertEquals("UTF-16", XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_Cp1047() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"CP1047\"?>", "CP1047"); final String guessedEncoding = "CP1047"; assertEquals("CP1047", XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_NoEncoding() throws IOException { final InputStream input = stringToStream("<?xml>", "UTF-8"); final String guessedEncoding = "UTF-8"; assertNull(XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_GuessedIsNull() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"UTF-8\"?>", "UTF-8"); final String guessedEncoding = null; assertNull(XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_UppercaseResult() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"utf-8\"?>", "UTF-8"); final String guessedEncoding = "UTF-8"; assertEquals("UTF-8", XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_DifferentAsciiCompatible() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"TEST\"?>", "ISO-8859-1"); final String guessedEncoding = "UTF-8"; assertEquals("TEST", XmlReader.getXmlProlog(input, guessedEncoding)); }
@Test public void testGetXmlProlog_DifferentAsciiIncompatible() throws IOException { final InputStream input = stringToStream("<?xml encoding=\"TEST\"?>", "UTF-16BE"); final String guessedEncoding = "UTF-16LE"; expectedException.expect(IOException.class); XmlReader.getXmlProlog(input, guessedEncoding); }