diff --git a/src/main/java/net/runelite/cache/fs/IndexEntry.java b/src/main/java/net/runelite/cache/fs/IndexEntry.java new file mode 100644 index 0000000000..56215c7919 --- /dev/null +++ b/src/main/java/net/runelite/cache/fs/IndexEntry.java @@ -0,0 +1,79 @@ +package net.runelite.cache.fs; + +import java.util.Objects; + +public class IndexEntry +{ + private IndexFile indexFile; + private int id, sector, length; + + public IndexEntry(IndexFile indexFile, int id, int sector, int length) + { + this.indexFile = indexFile; + this.id = id; + this.sector = sector; + this.length = length; + } + + public IndexFile getIndexFile() + { + return indexFile; + } + + public int getId() + { + return id; + } + + public int getSector() + { + return sector; + } + + public int getLength() + { + return length; + } + + @Override + public int hashCode() + { + int hash = 7; + hash = 19 * hash + Objects.hashCode(this.indexFile); + hash = 19 * hash + this.id; + hash = 19 * hash + this.sector; + hash = 19 * hash + this.length; + return hash; + } + + @Override + public boolean equals(Object obj) + { + if (obj == null) + { + return false; + } + if (getClass() != obj.getClass()) + { + return false; + } + final IndexEntry other = (IndexEntry) obj; + if (!Objects.equals(this.indexFile, other.indexFile)) + { + return false; + } + if (this.id != other.id) + { + return false; + } + if (this.sector != other.sector) + { + return false; + } + if (this.length != other.length) + { + return false; + } + return true; + } +} diff --git a/src/main/java/net/runelite/cache/fs/IndexFile.java b/src/main/java/net/runelite/cache/fs/IndexFile.java new file mode 100644 index 0000000000..4bbb6d6890 --- /dev/null +++ b/src/main/java/net/runelite/cache/fs/IndexFile.java @@ -0,0 +1,57 @@ +package net.runelite.cache.fs; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IndexFile +{ + private static final Logger logger = LoggerFactory.getLogger(IndexFile.class); + private static final int INDEX_ENTRY_LEN = 6; + + private int indexFileId; + private RandomAccessFile idx; + private final byte[] buffer = new byte[6]; + + public IndexFile(int indexFileId, File file) throws FileNotFoundException + { + this.indexFileId = indexFileId; + this.idx = new RandomAccessFile(file, "rw"); + } + + public synchronized void write(IndexEntry entry) throws IOException + { + idx.seek(entry.getId() * INDEX_ENTRY_LEN); + + buffer[0] = (byte) (entry.getLength() >> 16); + buffer[1] = (byte) (entry.getLength() >> 8); + buffer[2] = (byte) entry.getLength(); + + buffer[3] = (byte) (entry.getSector() >> 16); + buffer[4] = (byte) (entry.getSector() >> 8); + buffer[5] = (byte) entry.getSector(); + + idx.write(buffer); + } + + public synchronized IndexEntry read(int id) throws IOException + { + idx.seek(id * INDEX_ENTRY_LEN); + int i = idx.read(buffer); + if (i != INDEX_ENTRY_LEN) + logger.warn("short read"); + + int length = ((buffer[0] & 0xFF) << 16) | ((buffer[1] & 0xFF) << 8) | (buffer[2] & 0xFF); + int sector = ((buffer[3] & 0xFF) << 16) | ((buffer[4] & 0xFF) << 8) | (buffer[5] & 0xFF); + + return new IndexEntry(this, id, sector, length); + } + + public synchronized int getIndexCount() throws IOException + { + return (int)(idx.length() / INDEX_ENTRY_LEN); + } +} diff --git a/src/test/java/net/runelite/cache/fs/IndexFileTest.java b/src/test/java/net/runelite/cache/fs/IndexFileTest.java new file mode 100644 index 0000000000..5cce282d6c --- /dev/null +++ b/src/test/java/net/runelite/cache/fs/IndexFileTest.java @@ -0,0 +1,20 @@ +package net.runelite.cache.fs; + +import java.io.File; +import java.io.IOException; +import org.junit.Assert; +import org.junit.Test; + +public class IndexFileTest +{ + @Test + public void test1() throws IOException + { + File file = new File("d:/rs/07/test/test.dat"); + IndexFile index = new IndexFile(5, file); + IndexEntry entry = new IndexEntry(index, 7, 8, 9); + index.write(entry); + IndexEntry entry2 = index.read(7); + Assert.assertEquals(entry, entry2); + } +}