net.pandoragames.util.file
Class FileBuffer

java.lang.Object
  extended by net.pandoragames.util.file.FileBuffer
All Implemented Interfaces:
java.lang.CharSequence

public class FileBuffer
extends java.lang.Object
implements java.lang.CharSequence

Implementation of the CharSequence interface over a FileInputStream. This class may be used when very large portions of text are to be handled, too large to be kept in memory (i.e. as String or StringBuffer).
Despite its name, the object may as well be created with a URL as resource.

Author:
Olivier Wehner

Constructor Summary
FileBuffer(java.io.File textFile)
          Creates the buffer with the specified file and the default character set.
FileBuffer(java.io.File textFile, java.nio.charset.Charset charset)
          Creates the buffer with the specified file and character set.
FileBuffer(java.net.URL textFile)
          Creates the buffer from the specified URL and the default character set.
FileBuffer(java.net.URL textFile, java.nio.charset.Charset charset)
          Creates the buffer from the specified URL using the indicated character set.
 
Method Summary
 char charAt(int index)
          
 void close()
          Closes this buffer.
protected  void finalize()
          Calls the close() method.
 int getBufferSizeKb()
          Returns the internal buffer size in kilo byte.
 int length()
          
 void setBufferSizeKb(int k)
          Sets the internal buffer size in kilo byte.
 java.lang.CharSequence subSequence(int start, int end)
          
 java.lang.String toString()
          Returns a string containing the characters in this sequence in the same order as this sequence.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileBuffer

public FileBuffer(java.io.File textFile)
           throws java.io.IOException
Creates the buffer with the specified file and the default character set.

Parameters:
textFile - to be read into this buffer
Throws:
java.io.IOException - if the file can not be opend

FileBuffer

public FileBuffer(java.net.URL textFile)
           throws java.io.IOException
Creates the buffer from the specified URL and the default character set.

Parameters:
textFile - resource for this buffer
Throws:
java.io.IOException - if the URL can not be opend

FileBuffer

public FileBuffer(java.io.File textFile,
                  java.nio.charset.Charset charset)
           throws java.io.IOException
Creates the buffer with the specified file and character set.

Parameters:
textFile - to be read into this buffer
charset - to be used to decode the file
Throws:
java.io.IOException - if the file can not be opend

FileBuffer

public FileBuffer(java.net.URL textFile,
                  java.nio.charset.Charset charset)
           throws java.io.IOException
Creates the buffer from the specified URL using the indicated character set.

Parameters:
textFile - resource for this buffer
charset - to be used to decode the resource
Throws:
java.io.IOException - if the URL can not be opend
Method Detail

length

public int length()

Specified by:
length in interface java.lang.CharSequence

charAt

public char charAt(int index)

Specified by:
charAt in interface java.lang.CharSequence

subSequence

public java.lang.CharSequence subSequence(int start,
                                          int end)

Specified by:
subSequence in interface java.lang.CharSequence

toString

public java.lang.String toString()
Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence.

Specified by:
toString in interface java.lang.CharSequence
Overrides:
toString in class java.lang.Object

close

public void close()
Closes this buffer. Should be called before disposing this object, otherwise the underlying resource might be locked.


finalize

protected void finalize()
Calls the close() method.

Overrides:
finalize in class java.lang.Object

setBufferSizeKb

public void setBufferSizeKb(int k)
Sets the internal buffer size in kilo byte.

Parameters:
k - kilo byte

getBufferSizeKb

public int getBufferSizeKb()
Returns the internal buffer size in kilo byte. The default is 8.

Returns:
internal buffer size in kilo byte