【发布时间】:2014-09-04 15:14:05
【问题描述】:
我有 2 个 BufferedInputStreams,它们都包含一个 xml 字符串:一个很小,一个很大。
每个 xml 字符串的开头如下所示:
<RootElement success="true">
我创建了一个方法:
- 在输入流的开头设置标记
- 读取 xml 的前几个字节以检查根元素是否具有特定属性。
- 将输入流重置到标记位置,这样其他方法就可以享受完整的完整流了。
我的印象是缓冲输入流的缓冲区(默认为 8012 字节)和标记 readlimit 的大小实际上都无关紧要,因为无论我有多大,我都只会在重置前读取前 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 个字节!还有哪些其他因素可能导致这种行为?
【问题讨论】: