InputStream,mark(),reset() [英] InputStream, mark(), reset()

查看:419
本文介绍了InputStream,mark(),reset()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 mark() reset()方法正常工作(在下面的代码中),一步一步?我试着写自己的例子,但是开始抛出错误的标记异常或类似的东西,我无法理解在这段代码中放置标记和重置方法有什么意义,因为我没有看到这个或没有区别。

How are mark() and reset() methods working exactly(in code below), step by step ? I tried to write my own example but is starts to throw wrong mark exception or similar to that, and I cannot understand what is the point of placing mark and reset methods in this code because I don't see difference with this or without.

import java.io.*;

class BufferedInputStreamDemo {
    public static void main(String args[]) {
        String s = "© is a copyright symbol, "
                + "however &copy isn't.\n";
        byte buf[] = s.getBytes();

        ByteArrayInputStream in = new ByteArrayInputStream(buf);
        int c;
        boolean marked = false;

        //try_with_resources
        try (BufferedInputStream f = new BufferedInputStream(in)) {
            while ((c = f.read()) != -1) {
                switch (c) {
                    case '&':
                        if (!marked) {
                            f.mark(32);
                            marked = true;
                        } else {
                            marked = false;
                        }
                        break;
                    case ';':
                        if (marked) {
                            marked = false;
                            System.out.print("(c)");
                        } else
                            System.out.print((char) c);
                        break;
                    case ' ':
                        if (marked) {
                            marked = false;
                            f.reset();
                            System.out.print("&");
                        } else
                            System.out.print((char) c);
                        break;
                    default:
                        if (!marked)
                            System.out.print((char) c);
                        break;
                }
            }
        } catch (IOException e) {
            System.out.println("I/O Error: " + e);
        }
    }
}


推荐答案

f.mark(32); 到达时,读取光标已经在& 之后,并且为重置设置标记,以了解跳回的位置。因此,当您检测到关闭元素时缺少; 时,您手动打印& 并移动使用标记(32)调用,向后读取光标(在& 放置标记的位置后) 重置方法。在下一次阅读时,因为未设置标记变量,它将打印字符。

When f.mark(32); is reached the read cursor is already after &, and a marker is set for reset to know where to jump back. So when you have detected that a ; is missing to close the element, you are manually printing & and moving the read cursor right back (after & where the marker was placed, using the mark(32) call), using the reset method. On the next read, because your marked variable is not set it will print the characters.

mark(32)表示如果您的读取光标超过32个字符,则自动删除标记。这可能是您的其他代码中的问题,即触发错误,因为标记已经失效。

mark(32) means to automatically remove the marker if your read cursor will advance more then 32 character. This may be the issue in your other code, that is triggering an error, because the marker was invalidated already.

这篇关于InputStream,mark(),reset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆