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
Common implementation for file based word lists.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCache of word indices to byte offsets where word starts in backing file.protected static classData structure containing word and byte offset into file where word begins in backing file. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AbstractFileWordList.CacheCache of indexes to file positions.private final CharBufferBuffer to hold decoded word read from file.private final CharsetDecoderCharset decoder.protected static final intDefault cache percent.protected final RandomAccessFileFile containing words.private final ObjectUsed to synchronize access to the underlying cache.private longCurrent position into backing file.protected intNumber of words in the file.private final ByteBufferBuffer to hold word read from file.Fields inherited from class org.passay.dictionary.AbstractWordList
comparator -
Constructor Summary
ConstructorsConstructorDescriptionAbstractFileWordList(RandomAccessFile file, boolean caseSensitive, CharsetDecoder decoder) Creates a new abstract file word list from the supplied file. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ByteBufferbuffer()Returns the buffer providing the backing file data.voidclose()Closes the underlying file and make the cache available for garbage collection.protected abstract voidfill()Fills the buffer from the backing file.get(int index) Returns the word at the given 0-based index.getFile()Returns the file backing this list.private booleanDetermines whether the backing buffer has any more data to read.protected voidinitialize(int cachePercent, boolean allocateDirect) Reads words from the backing file to initialize the word list.private AbstractFileWordList.FileWordReads the next word from the current position in the backing file.protected StringreadWord(int index) Reads the word from the file at the given index of the word list.protected abstract voidseek(long offset) Positions the read head of the backing file at the given byte offset.intsize()Returns the number of words in the list.toString()Methods inherited from class org.passay.dictionary.AbstractWordList
checkRange, getComparator, iterator, medianIterator
-
Field Details
-
DEFAULT_CACHE_PERCENT
protected static final int DEFAULT_CACHE_PERCENTDefault cache percent.- See Also:
-
file
File containing words. -
size
protected int sizeNumber of words in the file. -
lock
Used to synchronize access to the underlying cache. -
cache
Cache of indexes to file positions. -
charsetDecoder
Charset decoder. -
wordBuf
Buffer to hold word read from file. -
charBuf
Buffer to hold decoded word read from file. -
position
private long positionCurrent position into backing file.
-
-
Constructor Details
-
AbstractFileWordList
Creates a new abstract file word list from the supplied file.- Parameters:
file- 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 Details
-
get
Description copied from interface:WordListReturns 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:WordListReturns the number of words in the list.- Returns:
- total number of words in list.
-
getFile
Returns the file backing this list.- Returns:
- random access file that is backing this list
-
close
Closes the underlying file and make the cache available for garbage collection.- Throws:
IOException- if an error occurs closing the file
-
initialize
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
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
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
Returns the buffer providing the backing file data.- Returns:
- Buffer around backing file.
-
fill
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.
-
readNextWord
Reads the next word from the current position in the backing file.- Returns:
- Data structure containing word and byte offset into file where word begins.
- Throws:
IOException- on I/O errors reading file data.
-
hasRemaining
Determines whether the backing buffer has any more data to read. If the buffer is empty, it attempts to read from the underlying file and then checks the buffer again.- Returns:
- True if there is any more data to read from the buffer, false otherwise.
- Throws:
IOException- on I/O errors reading file data.
-
toString
-