InputStream, mark(), reset()
Asked Answered
B

3

16

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);
        }
    }
}
Bewick answered 2/11, 2012 at 14:1 Comment(3)
What exception is being thrown? Do you have a stack trace? Please, be more specific on your question.Dourine
The exception being thrown is I/O Error: java.io.IOException: Resetting to invalid mark, but I wrote That exception is being thrown in another example. Mainly I'm asking what is mark(32) and reset doing in this example.Bewick
@user1165499 See my answer for details on how this code is working, and what I suspect the issue is, in your other example.Bats
B
8

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) 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.

Bats answered 2/11, 2012 at 14:15 Comment(0)
P
6

See the API documentation:

mark(int)

Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

This method simply performs in.mark(readlimit).

reset()

Repositions this stream to the position at the time the mark method was last called on this input stream.

This method simply performs in.reset().

Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

Perspire answered 2/11, 2012 at 14:7 Comment(0)
H
0

If you need to mark/reset recursively several times (nested marks) take a look here: MultiMarkedInputStream

Hyrcania answered 25/1 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.