Class ByteTrackingReader
java.lang.Object
java.io.Reader
java.io.FilterReader
com.thirdpartylabs.xmlscalpel.io.reader.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
-
Constructor Summary
Constructors Modifier Constructor Description protectedByteTrackingReader(java.io.Reader in)Creates a new byte tracking filtered reader. -
Method Summary
Modifier and Type Method Description longgetByteOffsetForCharOffset(long charOffset)Return the byte offset for the provided char offsetintread()Read a char and count the bytesintread(char[] buffer, int off, int len)Read and count the bytes in the chars that are readlongskip(long n)We read and count the bytes in chars we're skipping over
-
Constructor Details
-
ByteTrackingReader
protected ByteTrackingReader(java.io.Reader in)Creates a new byte tracking filtered reader.- Parameters:
in- aReaderobject providing the underlying stream.- Throws:
java.lang.NullPointerException- ifinisnull
-
-
Method Details
-
skip
public long skip(long n) throws java.io.IOExceptionWe read and count the bytes in chars we're skipping over- Overrides:
skipin classjava.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.IOExceptionRead and count the bytes in the chars that are read- Overrides:
readin classjava.io.FilterReader- Parameters:
buffer- Char array to be filledoff- The buffer will be filled starting at this offsetlen- The number of chars to read- Returns:
- the number of chars read
- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOExceptionRead a char and count the bytes- Overrides:
readin classjava.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
-