我有2个BufferedInputStreams,它们都包含一个xml字符串:一个小字符串和一个非常大的字符串。BufferedInputStream标记/重置无效标记
这里的每个XML字符串的开始看起来像:
<RootElement success="true">
我创建了一个方法:
- 在InputStream的开始
- 读取设置标记xml的前几个字节来检查根元素是否具有特定的属性。
- 将输入流重置到标记位置,因此另一种方法可以享受完整的完整流。
我的印象是,缓冲输入流的缓冲区大小(默认值为8012bytes)和标记读取限制的大小实际上很重要,因为我只是在重置前像前50个字节一样读取,而不管我的输入流有多大。
不幸的是我得到一个“IOException: resseting to invalid mark
”异常。这里是相关的代码:
private boolean checkXMLForSuccess(BufferedInputStream responseStream) throws XMLStreamException, FactoryConfigurationError
{
//Normally, this should be set to the amount of bytes that can be read before invalidating.
//the mark. Because we use a default buffer size (1024 or 2048 bytes) that is much greater
//than the amount of bytes we will read here (less than 100 bytes) this is not a concern.
responseStream.mark(100);
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(responseStream);
xmlReader.next(); //Go to the root element
//This is for loop, but the root element can only have 1 attribute.
for (int i=0; i < xmlReader.getAttributeCount(); i++)
{
if(xmlReader.getAttributeLocalName(i).equals(SUCCES_ATTRIBUTE))
{
Boolean isSuccess = Boolean.parseBoolean(xmlReader.getAttributeValue(i));
if (isSuccess)
{
try
{
responseStream.reset();
}
catch (IOException e)
{
//Oh oh... reset mark problem??
}
return true;
}
}
}
return false;
}
现在,我尝试将标记读取限制设置为更高的数字。在它最终奏效之前,我必须将它设置为10000。我无法想象我的下面的代码需要读取10000个字节!还有哪些其他因素可以对此行为负责?
如果xmlReader.next()读取的字节数超过100个字节,为什么500字节的小XML文件可以工作,但大小为5000的xml文件不起作用?都具有相同的根元素,并具有相同的属性,并且都大于100个字节。 – user1884155 2014-09-04 15:46:56
@ user1884155在这一点上,没有真正查看XMLReader在OpenJDK代码中做了什么,这都是猜测。 – Powerlord 2014-09-04 15:56:37
@ user1884155,@ Powerlord - 已更新我的回复与您的意见 – BatScream 2014-09-04 15:57:35