Class ByteTrackingReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class ByteTrackingReader
    extends java.io.FilterReader
    FilterReader that tracks byte progress as chars are read.

    Char offsets are mapped to byte offsets, so that known char offsets can be used to determine byte offsets. Only a certain number (set in charMemoryDepth, currently 8192) of the most recent offsets are remembered. This reader is specifically intended to be used in conjunction with the Woodstox streaming reader, which seems to read 2048 chars ahead of the reported progress, so we need to account for that, and accommodate potentially large tag names, since the end tag plus the read-ahead determine the length of memory we need.

    • Field Summary

      • Fields inherited from class java.io.FilterReader

        in
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ByteTrackingReader​(java.io.Reader in)
      Creates a new byte tracking filtered reader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long getByteOffsetForCharOffset​(long charOffset)
      Return the byte offset for the provided char offset
      int read()
      Read a char and count the bytes
      int read​(char[] buffer, int off, int len)
      Read and count the bytes in the chars that are read
      long skip​(long n)
      We read and count the bytes in chars we're skipping over
      • Methods inherited from class java.io.FilterReader

        close, mark, markSupported, ready, reset
      • Methods inherited from class java.io.Reader

        nullReader, read, read, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ByteTrackingReader

        protected ByteTrackingReader​(java.io.Reader in)
        Creates a new byte tracking filtered reader.
        Parameters:
        in - a Reader object providing the underlying stream.
        Throws:
        java.lang.NullPointerException - if in is null
    • Method Detail

      • skip

        public long skip​(long n)
                  throws java.io.IOException
        We read and count the bytes in chars we're skipping over
        Overrides:
        skip in class java.io.FilterReader
        Parameters:
        n - Number of chars to skip
        Returns:
        Number of chars actually skipped
        Throws:
        java.io.IOException
      • read

        public int read​(char[] buffer,
                        int off,
                        int len)
                 throws java.io.IOException
        Read and count the bytes in the chars that are read
        Overrides:
        read in class java.io.FilterReader
        Parameters:
        buffer - Char array to be filled
        off - The buffer will be filled starting at this offset
        len - The number of chars to read
        Returns:
        the number of chars read
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Read a char and count the bytes
        Overrides:
        read in class java.io.FilterReader
        Returns:
        A single char read from the file
        Throws:
        java.io.IOException
      • getByteOffsetForCharOffset

        public long getByteOffsetForCharOffset​(long charOffset)
        Return the byte offset for the provided char offset
        Parameters:
        charOffset - A character offset
        Returns:
        -1 if if the charOffset is negative, or if the offset is outside the bounds of our memory