Class LineByteStream
- java.lang.Object
-
- edu.stanford.slac.archiverappliance.PB.utils.LineByteStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class LineByteStream extends Object implements Closeable
This class wraps a RandomAccessFile and returns byte arrays separated by lines. In addition it also maintains a count of the bytes read. We expect the file channel to be positioned correctly for the initial read. After each read, the channel is positioned just after the newline.- Author:
- mshankar
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_ITERATIONS_TO_DETERMINE_LINE
static int
MAX_LINE_SIZE
-
Constructor Summary
Constructors Constructor Description LineByteStream(Path path)
LineByteStream(Path path, long startPosition)
LineByteStream(Path path, long startPosition, long endPosition)
-
Method Summary
Modifier and Type Method Description void
close()
String
getAbsolutePath()
long
getCurrentPosition()
byte[]
readLine()
ByteArray
readLine(ByteArray bar)
Optimize the readline by offering the abilty to reuse the same memory allocation.void
safeClose()
void
seekToBeforeLastLine()
Seeks and positions the pointer to to the last line in the file.void
seekToBeforePreviousLine(long posn)
Seeks and positions the pointer to line previous to the specified position.void
seekToFirstNewLine()
Seeks to the first new line after the current position in the rndAccFile.
-
-
-
Constructor Detail
-
LineByteStream
public LineByteStream(Path path) throws IOException
- Throws:
IOException
-
LineByteStream
public LineByteStream(Path path, long startPosition) throws IOException
- Throws:
IOException
-
LineByteStream
public LineByteStream(Path path, long startPosition, long endPosition) throws IOException
- Throws:
IOException
-
-
Method Detail
-
readLine
public byte[] readLine() throws IOException
- Throws:
IOException
-
readLine
public ByteArray readLine(ByteArray bar) throws IOException
Optimize the readline by offering the abilty to reuse the same memory allocation. This does not escape the bytes as it is reading the line. While this is optimal to do, it means for raw responses, we'll be redoing some of the work and raw responses are 90% of the requests. If in future, we determine that unescaping here is more optimal, this method has an unescape version in version control history. Returns the same byte array as the input.- Parameters:
bar
- ByteArray- Returns:
- bar ByteArray
- Throws:
IOException
-
-
seekToFirstNewLine
public void seekToFirstNewLine() throws IOException
Seeks to the first new line after the current position in the rndAccFile. The file pointer is located just after the first newline.- Throws:
IOException
-
-
seekToBeforeLastLine
public void seekToBeforeLastLine() throws IOException
Seeks and positions the pointer to to the last line in the file. The file pointer is located just before the last line so that readLine gets a valid line. About the only thing once can do after this is to read a line and stop...- Throws:
IOException
-
-
seekToBeforePreviousLine
public void seekToBeforePreviousLine(long posn) throws IOException
Seeks and positions the pointer to line previous to the specified position. The file pointer is located just so that one can do a readline. Note that this method is not efficient at all; so use with care.- Parameters:
posn
-- Throws:
IOException
-
-
getCurrentPosition
public long getCurrentPosition() throws IOException
- Throws:
IOException
-
safeClose
public void safeClose()
-
getAbsolutePath
public String getAbsolutePath()
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-