Package org.passay.dictionary
Class AbstractFileWordList
- java.lang.Object
-
- org.passay.dictionary.AbstractWordList
-
- org.passay.dictionary.AbstractFileWordList
-
- All Implemented Interfaces:
WordList
- Direct Known Subclasses:
FileWordList
,MemoryMappedFileWordList
public abstract class AbstractFileWordList extends AbstractWordList
Common implementation for file based word lists.- Author:
- Middleware Services
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractFileWordList.FileWord
Data structure containing word and byte offset into file where word begins in backing file.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CACHE_PERCENT
Default cache percent.protected RandomAccessFile
file
File containing words.protected int
size
Number of words in the file.-
Fields inherited from class org.passay.dictionary.AbstractWordList
comparator
-
-
Constructor Summary
Constructors Constructor Description AbstractFileWordList(RandomAccessFile raf, boolean caseSensitive, CharsetDecoder decoder)
Creates a new abstract file word list from the supplied file.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract ByteBuffer
buffer()
Returns the buffer providing the backing file data.void
close()
Closes the underlying file and make the cache available for garbage collection.protected abstract void
fill()
Fills the buffer from the backing file.String
get(int index)
Returns the word at the given 0-based index.RandomAccessFile
getFile()
Returns the file backing this list.protected void
initialize(int cachePercent, boolean allocateDirect)
Reads words from the backing file to initialize the word list.protected String
readWord(int index)
Reads the word from the file at the given index of the word list.protected abstract void
seek(long offset)
Positions the read head of the backing file at the given byte offset.int
size()
Returns the number of words in the list.String
toString()
-
Methods inherited from class org.passay.dictionary.AbstractWordList
checkIsString, checkRange, getComparator, iterator, medianIterator
-
-
-
-
Field Detail
-
DEFAULT_CACHE_PERCENT
public static final int DEFAULT_CACHE_PERCENT
Default cache percent.- See Also:
- Constant Field Values
-
file
protected final RandomAccessFile file
File containing words.
-
size
protected int size
Number of words in the file.
-
-
Constructor Detail
-
AbstractFileWordList
public AbstractFileWordList(RandomAccessFile raf, boolean caseSensitive, CharsetDecoder decoder)
Creates a new abstract file word list from the supplied file.- Parameters:
raf
- File containing words, one per line.caseSensitive
- Set to true to create case-sensitive word list, false otherwise.decoder
- Charset decoder for converting file bytes to characters
-
-
Method Detail
-
get
public String get(int index)
Description copied from interface:WordList
Returns the word at the given 0-based index.- Parameters:
index
- 0-based index.- Returns:
- word at given index.
-
size
public int size()
Description copied from interface:WordList
Returns the number of words in the list.- Returns:
- total number of words in list.
-
getFile
public RandomAccessFile getFile()
Returns the file backing this list.- Returns:
- random access file that is backing this list
-
close
public void close() throws IOException
Closes the underlying file and make the cache available for garbage collection.- Throws:
IOException
- if an error occurs closing the file
-
initialize
protected void initialize(int cachePercent, boolean allocateDirect) throws IOException
Reads words from the backing file to initialize the word list.- Parameters:
cachePercent
- Percent of file in bytes to use for cache.allocateDirect
- whether buffers should be allocated withByteBuffer.allocateDirect(int)
- Throws:
IllegalArgumentException
- if cachePercent is out of range or the words are not sorted correctly according to the comparatorIOException
- on I/O errors reading file data.
-
readWord
protected String readWord(int index) throws IOException
Reads the word from the file at the given index of the word list.- Parameters:
index
- ith word in the word list- Returns:
- word at the supplied index
- Throws:
IOException
- on I/O errors
-
seek
protected abstract void seek(long offset) throws IOException
Positions the read head of the backing file at the given byte offset.- Parameters:
offset
- byte offset into file.- Throws:
IOException
- on I/O errors seeking.
-
buffer
protected abstract ByteBuffer buffer()
Returns the buffer providing the backing file data.- Returns:
- Buffer around backing file.
-
fill
protected abstract void fill() throws IOException
Fills the buffer from the backing file. This method may be a no-op if the buffer contains all file contents.- Throws:
IOException
- on I/O errors filling buffer.
-
-