Changed FTLDat to allow opening dats in read-only mode

This commit is contained in:
Vhati 2013-09-29 07:58:57 -04:00
parent 7b65cc5d73
commit b94440e0a0
7 changed files with 55 additions and 14 deletions

View file

@ -313,10 +313,12 @@ public class FTLDat {
public static class FolderPack extends AbstractPack {
private File rootDir;
public FolderPack( File rootDir ) {
this.rootDir = rootDir;
}
@Override
public String getName() {
return rootDir.getName();
@ -447,21 +449,57 @@ public class FTLDat {
private Map<String,Integer> pathToIndexMap = null;
private ByteBuffer byteBuffer = null;
public FTLPack( File datFile, boolean create ) throws IOException {
this( datFile, create, 2048 );
/**
* Opens or creates a dat in various modes.
* When creating, the initial index size will be 2048.
*
* @see FTLPack(File datFile, String mode, int indexSize)
*/
public FTLPack( File datFile, String mode ) throws IOException {
this( datFile, mode, 2048 );
}
public FTLPack( File datFile, boolean create, int indexSize ) throws IOException {
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "rw" );
/**
* Opens or creates a dat in various modes.
*
* The mode must be one of the following:
* r - opens an existing dat, read-only.
* r+ - opens an existing dat, read/write.
* w+ - creates a new empty dat, read/write.
*
* @param datFile a file to open/create
* @param mode see above
* @param indexSize size of the initial index if creating
*/
public FTLPack( File datFile, String mode, int indexSize ) throws IOException {
if ( mode.equals( "r" ) ) {
if ( !datFile.exists() )
throw new FileNotFoundException( String.format( "The datFile was not found: %s", datFile.getPath() ) );
if ( create ) {
createIndex( indexSize );
} else {
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "r" );
readIndex();
}
else if ( mode.equals( "r+" ) ) {
if ( !datFile.exists() )
throw new FileNotFoundException( String.format( "The datFile was not found: %s", datFile.getPath() ) );
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "rw" );
readIndex();
}
else if ( mode.equals( "w+" ) ) {
this.datFile = datFile;
raf = new RandomAccessFile( datFile, "rw" );
createIndex( indexSize );
}
else {
throw new IllegalArgumentException( String.format( "FTLPack constructor's mode arg was not 'r', 'r+', or 'w+' (%s).", mode ) );
}
}
/**
* Reads a little-endian unsigned int.
* Java doesn't have an unsigned int primitive,