FileDocCategorySizeDatePackage
MappedFileTokenizer.javaAPI DocExample2793Sat Jan 24 10:44:28 GMT 2004je3.nio

MappedFileTokenizer

public class MappedFileTokenizer extends ByteBufferTokenizer
This class implements the Tokenizer interface for a FileChannel and Charset. It extends ByteBufferTokenizer and uses FileChannel.map() to memory-map the contents of the file into a ByteBuffer.

Fields Summary
static final int
DEFAULT_BUFFER_SIZE
FileChannel
channel
int
byteBufferSize
long
filesize
long
fileposition
Constructors Summary
public MappedFileTokenizer(FileChannel channel, Charset charset)

      // Starting position of the next chunk

    // Construct a tokenizer for the specified FileChannel, assuming the
    // file contains text encoded using the specified Charset.
        
	 
    
	this(channel, charset, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
    
public MappedFileTokenizer(FileChannel channel, Charset charset, int charBufferSize, int byteBufferSize)

	super(charset, charBufferSize); // Superclass handles charset and size
	this.channel = channel;
	this.byteBufferSize = byteBufferSize; 
	filesize = channel.size();      // Get the length of the file
	fileposition = 0;               // And start at the beginning
    
Methods Summary
protected java.nio.ByteBuffergetMoreBytes()

	// Return byteBufferSize bytes, or the number remaining in the file
	// if that is less
	long length = byteBufferSize;
	if (fileposition + length > filesize) length = filesize-fileposition;

	// Memory map the bytes into a buffer
	ByteBuffer buffer =
	    channel.map(FileChannel.MapMode.READ_ONLY, fileposition, length);
	// Store the position of the next chunk
	fileposition += length;
	// And return the memory-mapped buffer of bytes.
	return buffer;
    
protected booleanhasMoreBytes()

 return fileposition < filesize;