datafile id should have been a parameter
This commit is contained in:
@@ -16,14 +16,12 @@ public class DataFile implements Closeable
|
|||||||
private static final int SECTOR_SIZE = 520;
|
private static final int SECTOR_SIZE = 520;
|
||||||
|
|
||||||
private final Store store;
|
private final Store store;
|
||||||
private final int datafileId;
|
|
||||||
private final RandomAccessFile dat;
|
private final RandomAccessFile dat;
|
||||||
private final byte[] readCachedBuffer = new byte[SECTOR_SIZE];
|
private final byte[] readCachedBuffer = new byte[SECTOR_SIZE];
|
||||||
|
|
||||||
public DataFile(Store store, int id, File file) throws FileNotFoundException
|
public DataFile(Store store, File file) throws FileNotFoundException
|
||||||
{
|
{
|
||||||
this.store = store;
|
this.store = store;
|
||||||
this.datafileId = id;
|
|
||||||
dat = new RandomAccessFile(file, "rw");
|
dat = new RandomAccessFile(file, "rw");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +39,7 @@ public class DataFile implements Closeable
|
|||||||
* @return
|
* @return
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public synchronized ByteBuffer read(int archiveId, int sector, int size) throws IOException
|
public synchronized ByteBuffer read(int indexId, int archiveId, int sector, int size) throws IOException
|
||||||
{
|
{
|
||||||
if (sector <= 0L || dat.length() / 520L < (long) sector)
|
if (sector <= 0L || dat.length() / 520L < (long) sector)
|
||||||
{
|
{
|
||||||
@@ -108,7 +106,7 @@ public class DataFile implements Closeable
|
|||||||
currentIndex = this.readCachedBuffer[7] & 255;
|
currentIndex = this.readCachedBuffer[7] & 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (archiveId != currentArchive || currentPart != part || this.datafileId != currentIndex)
|
if (archiveId != currentArchive || currentPart != part || indexId != currentIndex)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -134,7 +132,7 @@ public class DataFile implements Closeable
|
|||||||
* @return the sector the data starts at
|
* @return the sector the data starts at
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public synchronized int write(int archiveId, ByteBuffer data) throws IOException
|
public synchronized int write(int indexId, int archiveId, ByteBuffer data) throws IOException
|
||||||
{
|
{
|
||||||
int sector;
|
int sector;
|
||||||
int startSector;
|
int startSector;
|
||||||
@@ -182,7 +180,7 @@ public class DataFile implements Closeable
|
|||||||
this.readCachedBuffer[6] = (byte) (nextSector >> 16);
|
this.readCachedBuffer[6] = (byte) (nextSector >> 16);
|
||||||
this.readCachedBuffer[7] = (byte) (nextSector >> 8);
|
this.readCachedBuffer[7] = (byte) (nextSector >> 8);
|
||||||
this.readCachedBuffer[8] = (byte) nextSector;
|
this.readCachedBuffer[8] = (byte) nextSector;
|
||||||
this.readCachedBuffer[9] = (byte) this.datafileId;
|
this.readCachedBuffer[9] = (byte) indexId;
|
||||||
dat.seek(SECTOR_SIZE * sector);
|
dat.seek(SECTOR_SIZE * sector);
|
||||||
dat.write(this.readCachedBuffer, 0, 10);
|
dat.write(this.readCachedBuffer, 0, 10);
|
||||||
|
|
||||||
@@ -206,7 +204,7 @@ public class DataFile implements Closeable
|
|||||||
this.readCachedBuffer[4] = (byte) (nextSector >> 16);
|
this.readCachedBuffer[4] = (byte) (nextSector >> 16);
|
||||||
this.readCachedBuffer[5] = (byte) (nextSector >> 8);
|
this.readCachedBuffer[5] = (byte) (nextSector >> 8);
|
||||||
this.readCachedBuffer[6] = (byte) nextSector;
|
this.readCachedBuffer[6] = (byte) nextSector;
|
||||||
this.readCachedBuffer[7] = (byte) this.datafileId;
|
this.readCachedBuffer[7] = (byte) indexId;
|
||||||
dat.seek(SECTOR_SIZE * sector);
|
dat.seek(SECTOR_SIZE * sector);
|
||||||
dat.write(this.readCachedBuffer, 0, 8);
|
dat.write(this.readCachedBuffer, 0, 8);
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class Store implements Closeable
|
|||||||
|
|
||||||
public Store(File folder) throws IOException
|
public Store(File folder) throws IOException
|
||||||
{
|
{
|
||||||
data = new DataFile(this, -1/*wtfisthis*/, new File(folder, MAIN_FILE_CACHE_DAT));
|
data = new DataFile(this, new File(folder, MAIN_FILE_CACHE_DAT));
|
||||||
index255 = new IndexFile(this, 255, new File(folder, MAIN_FILE_CACHE_IDX + "255"));
|
index255 = new IndexFile(this, 255, new File(folder, MAIN_FILE_CACHE_IDX + "255"));
|
||||||
|
|
||||||
for (int i = 0; i < index255.getIndexCount(); ++i)
|
for (int i = 0; i < index255.getIndexCount(); ++i)
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ public class DataFileTest
|
|||||||
{
|
{
|
||||||
File file = folder.newFile();
|
File file = folder.newFile();
|
||||||
Store store = new Store(folder.getRoot());
|
Store store = new Store(folder.getRoot());
|
||||||
DataFile df = new DataFile(store, 42, file);
|
DataFile df = new DataFile(store, file);
|
||||||
int sector = df.write(3, ByteBuffer.wrap("test".getBytes()));
|
int sector = df.write(42, 3, ByteBuffer.wrap("test".getBytes()));
|
||||||
ByteBuffer buf = df.read(3, sector, 4);
|
ByteBuffer buf = df.read(42, 3, sector, 4);
|
||||||
String str = new String(buf.array());
|
String str = new String(buf.array());
|
||||||
Assert.assertEquals("test", str);
|
Assert.assertEquals("test", str);
|
||||||
file.delete();
|
file.delete();
|
||||||
@@ -34,9 +34,9 @@ public class DataFileTest
|
|||||||
|
|
||||||
File file = folder.newFile();
|
File file = folder.newFile();
|
||||||
Store store = new Store(folder.getRoot());
|
Store store = new Store(folder.getRoot());
|
||||||
DataFile df = new DataFile(store, 42, file);
|
DataFile df = new DataFile(store, file);
|
||||||
int sector = df.write(0x1FFFF, ByteBuffer.wrap(b));
|
int sector = df.write(42, 0x1FFFF, ByteBuffer.wrap(b));
|
||||||
ByteBuffer buf = df.read(0x1FFFF, sector, b.length);
|
ByteBuffer buf = df.read(42, 0x1FFFF, sector, b.length);
|
||||||
Assert.assertArrayEquals(b, buf.array());
|
Assert.assertArrayEquals(b, buf.array());
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user