推回缓冲区溢出 - 但我的缓冲区尚未满?

发布于 2024-11-09 13:16:34 字数 1083 浏览 3 评论 0 原文

我正在实现一个语法阅读器作为家庭作业的副产品,并且我正在一次读取一个字符的 XML 文件。我的目标是利用我通过实验室作业的代码,并改进结构和设计。

我有一个继承自 PushbackReaderSyntaxReader,它有一些额外的方法,例如 readAndExpect(char c)trimWhitespace()和<代码>readUntilChar(char c)。然后我有一个继承自 SyntaxReader 的 XMLSyntaxReader 并添加了 readAttribute() 和 readNode() 等功能(后者对于树中的子级是递归的......)。

我基本上只是从我的实验室作业中获取工作代码并将其粘贴到此方法中的适当位置。但是,当我尝试读取第一个子项时,我收到了一个我无法弄清楚的 IOException: Pushback buffer Overflow 。

这是我的 XML 文件的开头:

<Biosfar namn="Liv">ar allt som fortplantar sig
    <Rike namn="Vaxter"> etc...

我已经调试并观察了语法读取器成功地读取了子标记开头的 。此时,XMLSyntaxReader 确认它是一个子标记,未读取最后两个字符(R<,按此顺序)并调用readNode()来解析子节点。当我尝试取消读取 < 时,会发生异常。

在调试器中,我还验证了 PushbackReader 的私有 pos 变量确实设置为 0,但我无法弄清楚为什么。我已经解析了一整行文本 - 为什么这些字符仍然不算数?

我会发布我的代码,但它相当冗长,并且在我不确定我的确切实现是否相关之前同样有效。如果是,请指出我可以上传片段的地方,我将分享所有代码。

I'm implementing a syntax reader as a spin-off from a homework assignment, and I'm reading an XML file one character at a time. My goal is to take the code that I passed the lab assignment with, and improve the structure and design.

I have a SyntaxReader that inherits from PushbackReader, which has some extra methods like readAndExpect(char c), trimWhitespace() and readUntilChar(char c). Then I have an XMLSyntaxReader that inherits from SyntaxReader and adds functionality like readAttribute() and readNode() (the latter being recursive for children in the tree...).

I have basically just taken the working code from my lab assignment and pasted it into the appropriate places in this approach. However, when I try to read the first child, I'm getting an IOException: Pushback buffer overflow that I can't figure out.

This is the beginning of my XML file:

<Biosfar namn="Liv">ar allt som fortplantar sig
    <Rike namn="Vaxter"> etc...

I have debugged and watched the syntax readers successfully read all the way through <R at the beginning of the child tag. At that point, the XMLSyntaxReader confirms that it is a child tag, unreads the two last characters (R and <, in that order) and calls on readNode() to parse the child node. The exception occurs when I try to unread <.

In the debugger, I have also verified that the private pos variable of the PushbackReader is really set to 0, but I can't figure out why. I've parsed a full line of text - why don't those characters still count?

I'd post my code, but it's quite lengthy and as the same worked before I'm not sure my exact implementation is relevant. If it is, please point to someplace I can upload snippets, and I'll share all the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-11-16 13:16:34

不是 PushbackReader 的默认大小缓冲区只有1?当您按下第二个字符时,这会导致此问题。如果增加缓冲区的大小会发生什么?

Isn't the default size of the PushbackReader buffer only 1? That would cause this problem when you push the second character. What happens if you increase the size of the buffer?

悲念泪 2024-11-16 13:16:34

我得到了这个:

byte[] bytes = programaFonte.getBytes();

PushbackReader reader = new PushbackReader(new InputStreamReader(new ByteArrayInputStream(bytes)),bytes.length);

I got with it this:

byte[] bytes = programaFonte.getBytes();

PushbackReader reader = new PushbackReader(new InputStreamReader(new ByteArrayInputStream(bytes)),bytes.length);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文