Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

83 Further optimize declaration parsing #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ protected XMLDeclarationParser lookForXmlDecl() throws SOAPException {
}
if (reader != null) {
PushbackReader pushbackReader =
new PushbackReader(reader, 4096); //some size to unread <?xml ....?>
new PushbackReader(reader, 12); //some size to unread <?xml ....?>
XMLDeclarationParser ev =
new XMLDeclarationParser(pushbackReader);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public class XMLDeclarationParser {
private String xmlDecl = null;
static String gt16 = null;
static String utf16Decl = null;
static int maxDeclPrefixLength;
static {
try {
gt16 = new String(">".getBytes("utf-16"));
utf16Decl = new String("<?xml".getBytes("utf-16"));
maxDeclPrefixLength = utf16Decl.length();
} catch (Exception e) {}
}

Expand Down Expand Up @@ -91,35 +93,47 @@ public void parse() throws TransformerException, IOException
{
int c = 0;
int index = 0;
boolean utf16 = false;
boolean utf8 = false;
int xmlIndex = -1;
StringBuilder xmlDeclStr = new StringBuilder();
while ((c = m_pushbackReader.read()) != -1) {
xmlDeclStr.append((char)c);
index++;
if (index == maxDeclPrefixLength) {
xmlIndex = xmlDeclStr.indexOf(utf16Decl);
if (xmlIndex > -1) {
utf16 = true;
} else {
xmlIndex = xmlDeclStr.indexOf("<?xml");
if (xmlIndex > -1) {
utf8 = true;
}
}
// no XML decl
if (!utf16 && !utf8) {
int len = index;
m_pushbackReader.unread(xmlDeclStr.toString().toCharArray(), 0, len);
return;
}
}
if (c == '>') {
break;
}
}
int len = index;

String decl = xmlDeclStr.toString();
boolean utf16 = false;
boolean utf8 = false;

int xmlIndex = decl.indexOf(utf16Decl);
if (xmlIndex > -1) {
utf16 = true;
} else {
xmlIndex = decl.indexOf("<?xml");
if (xmlIndex > -1) {
if (len < maxDeclPrefixLength) {
xmlIndex = xmlDeclStr.indexOf("<?xml");
if (xmlIndex == -1) {
m_pushbackReader.unread(decl.toCharArray(), 0, len);
return;
} else {
utf8 = true;
}
}

// no XML decl
if (!utf16 && !utf8) {
m_pushbackReader.unread(decl.toCharArray(), 0, len);
return;
}
m_hasHeader = true;

if (utf16) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void transform(
if (reader.markSupported())
reader.mark(Integer.MAX_VALUE);

PushbackReader pushbackReader = new PushbackReader(reader, 4096);
PushbackReader pushbackReader = new PushbackReader(reader, 12);
//some size to unread <?xml ....?>
XMLDeclarationParser ev =
new XMLDeclarationParser(pushbackReader);
Expand Down