org.knime.base.node.util
Class BufferedFileReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
          extended by org.knime.base.node.util.BufferedFileReader
All Implemented Interfaces:
Closeable, Readable

public final class BufferedFileReader
extends BufferedReader

A reader that counts the number of bytes read.

Author:
ohl, University of Konstanz

Nested Class Summary
(package private) static class BufferedFileReader.ByteCountingStream
          Wraps an input stream and counts the number of bytes read from the stream.
 
Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Method Summary
 void close()
          
static BufferedFileReader createNewReader(InputStream in)
          Same as the method above (createNewReader(URL)), but with an input stream as argument.
static BufferedFileReader createNewReader(URL dataLocation)
          Creates a new reader from the specified location with the default character set from the Java VM.
static BufferedFileReader createNewReader(URL dataLocation, String charsetName)
          Creates a new reader from the specified location with the default character set from the Java VM.
protected  void finalize()
          
 String getCurrentLine()
          Returns the line currently read (if the \n char was already read, it still returns the current/last line).
 long getCurrentLineNumber()
           
 long getFileSize()
           
 long getNumberOfBytesRead()
           
 String getZipEntryName()
          If the underlying source is a ZIP archive this method returns the name of the entry read.
 boolean hasMoreZipEntries()
           
 boolean isZippedSource()
           
 void mark(int readAheadLimit)
          
 boolean markSupported()
          
 int read()
          
 int read(char[] cbuf, int off, int len)
          
 String readLine()
          
 boolean ready()
          
 void reset()
          
 long skip(long n)
          
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

finalize

protected void finalize()
                 throws Throwable

Overrides:
finalize in class Object
Throws:
Throwable

getNumberOfBytesRead

public long getNumberOfBytesRead()
Returns:
the number of bytes read from the inner stream. If the input stream is a zipped stream, it returns the number of bytes read from this zipped stream. It the EOF of the stream has been read once, the returned number might not be accurate anymore.

getFileSize

public long getFileSize()
Returns:
the total byte count of the stream, if this stream was created from a file (and the file delivered its file size). Zero otherwise!

getCurrentLine

public String getCurrentLine()
                      throws IOException
Returns the line currently read (if the \n char was already read, it still returns the current/last line). If no characters were read so far, the empty string is returned.
In contrast to readLine() this method doesn't modify the stream (doesn't read from it!) and it returns the entire line (also the characters at the beginning of the line if they were read already).

Returns:
the line that is currently read. Returns the empty string, if no character was read as of yet, and null if the reader was closed.
Throws:
IOException - if the stream has been closed

getCurrentLineNumber

public long getCurrentLineNumber()
                          throws IOException
Returns:
the number of the line currently read. It returns zero if no character was read yet.
Throws:
IOException - if the stream has been closed

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class BufferedReader
Throws:
IOException

mark

public void mark(int readAheadLimit)
          throws IOException

Overrides:
mark in class BufferedReader
Throws:
IOException

markSupported

public boolean markSupported()

Overrides:
markSupported in class BufferedReader

read

public int read()
         throws IOException

Overrides:
read in class BufferedReader
Throws:
IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException

Overrides:
read in class BufferedReader
Throws:
IOException

readLine

public String readLine()
                throws IOException

Overrides:
readLine in class BufferedReader
Throws:
IOException

ready

public boolean ready()
              throws IOException

Overrides:
ready in class BufferedReader
Throws:
IOException

reset

public void reset()
           throws IOException

Overrides:
reset in class BufferedReader
Throws:
IOException

skip

public long skip(long n)
          throws IOException

Overrides:
skip in class BufferedReader
Throws:
IOException

createNewReader

public static BufferedFileReader createNewReader(URL dataLocation)
                                          throws IOException
Creates a new reader from the specified location with the default character set from the Java VM. The returned reader can be asked for the number of bytes read from the stream (getNumberOfBytesRead()), and, if the location specifies a local file - and the size of it can be retrieved - the overall byte count in the stream (getFileSize()).
If the specified file is compressed, it will try to create a ZIP stream (and the byte counts refer both to the compressed file).
In addition this reader can be asked for the current line it is reading from (getCurrentLine())and the current line number (getCurrentLineNumber()).

Parameters:
dataLocation - the URL of the source to read from. If it is zipped it will try to open a ZIP stream on it.
Returns:
reader reading from the specified location.
Throws:
IOException - if something went wrong when opening the stream.

createNewReader

public static BufferedFileReader createNewReader(URL dataLocation,
                                                 String charsetName)
                                          throws IOException
Creates a new reader from the specified location with the default character set from the Java VM. The returned reader can be asked for the number of bytes read from the stream (getNumberOfBytesRead()), and, if the location specifies a local file - and the size of it can be retrieved - the overall byte count in the stream (getFileSize()).
If the specified file is compressed, it will try to create a ZIP stream (and the byte counts refer both to the compressed file).
In addition this reader can be asked for the current line it is reading from (getCurrentLine())and the current line number (getCurrentLineNumber()).

Parameters:
dataLocation - the URL of the source to read from. If it is zipped it will try to open a ZIP stream on it.
charsetName - the character set to use. Must be supported by the VM
Returns:
reader reading from the specified location.
Throws:
IOException - if something went wrong when opening the stream.
IllegalCharsetNameException - If the given charset name is illegal
UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine

createNewReader

public static BufferedFileReader createNewReader(InputStream in)
Same as the method above (createNewReader(URL)), but with an input stream as argument. The getFileSize() method of the created reader always returns zero.

Parameters:
in - the stream to read from
Returns:
a new buffered reader with some extra functionality (compared to the BufferedReader), but no file size (even if the stream reads from a file).

getZipEntryName

public String getZipEntryName()
If the underlying source is a ZIP archive this method returns the name of the entry read. Otherwise null.

Returns:
the entry read, if the source is a ZIP archive, or null.

isZippedSource

public boolean isZippedSource()
Returns:
true, if the underlying source is a ZIP archive (not gzip).

hasMoreZipEntries

public boolean hasMoreZipEntries()
Returns:
true, if the underlying source is a ZIP archive, if the EOF of the first entry was read and if there are more entries in the archive. False, otherwise, especially if the (first) EOF was not read!


Copyright, 2003 - 2010. All rights reserved.
University of Konstanz, Germany.
Chair for Bioinformatics and Information Mining, Prof. Dr. Michael R. Berthold.
You may not modify, publish, transmit, transfer or sell, reproduce, create derivative works from, distribute, perform, display, or in any way exploit any of the content, in whole or in part, except as otherwise expressly permitted in writing by the copyright owner or as specified in the license file distributed with this product.