public final class BoundedLineInputStream extends Object implements Iterator<String>, Closeable
InputStream that limits the number of lines being
read from the file.
Uses a BufferedReader internally to read the data. If the number of lines in file is
fewer than the lines in file, it will return only the lines that can be possibly read.| Constructor and Description |
|---|
BoundedLineInputStream(Reader reader,
int lines)
Constructs an iterator of the lines for a
Reader. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the underlying
Reader quietly. |
static void |
closeQuietly(BoundedLineInputStream iterator)
Closes the iterator, handling null and ignoring exceptions.
|
boolean |
hasNext()
Indicates whether the
Reader has more lines. |
protected boolean |
isValidLine(String line)
Overridable method to validate each line that is returned.
|
static BoundedLineInputStream |
iterator(InputStream input,
Charset encoding,
int lines)
A static method to create instance of
BoundedLineInputStream. |
static BoundedLineInputStream |
iterator(InputStream input,
String encoding,
int lines) |
String |
next()
Returns the next line in the wrapped
Reader. |
String |
nextLine()
Returns the next line in the wrapped
Reader. |
void |
remove()
Unsupported.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEachRemainingpublic BoundedLineInputStream(Reader reader, int lines) throws IllegalArgumentException
Reader.reader - the Reader to read from, not nullIllegalArgumentException - if the reader is nullpublic boolean hasNext()
Reader has more lines.
If there is an IOException then close() will
be called on this instance.hasNext in interface Iterator<String>true if the Reader has more linesIllegalStateException - if an IO exception occursprotected boolean isValidLine(String line)
line - the line that is to be validatedpublic String next()
Reader.next in interface Iterator<String>NoSuchElementException - if there is no line to returnpublic String nextLine()
Reader.NoSuchElementException - if there is no line to returnpublic void close()
Reader quietly.
This method is useful if you only want to process the first few
lines of a larger file. If you do not close the iterator
then the Reader remains open.
This method can safely be called multiple times.close in interface Closeableclose in interface AutoCloseablepublic void remove()
remove in interface Iterator<String>UnsupportedOperationException - alwayspublic static void closeQuietly(BoundedLineInputStream iterator)
iterator - the iterator to closepublic static BoundedLineInputStream iterator(InputStream input, Charset encoding, int lines) throws IOException
BoundedLineInputStream. This method
creates the instance specifying the character encoding and number of lines to be read.
The application using this method should ensure that they call BoundedLineInputStream.close()
once the application has finished reading. When 'lines' is greater than number of the files in the file, it
will be terminated once the end of the file is reached.
input - Input stream.encoding - Type of encoding for the file.lines - number of lines to be read.IOExceptionpublic static BoundedLineInputStream iterator(InputStream input, String encoding, int lines) throws IOException
IOExceptionCopyright © 2020 CDAP Licensed under the Apache License, Version 2.0.