RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.fs.cache.Cache Class Reference

Represents a Sector and Index cache. More...

Public Member Functions

ByteBuffer get (final int indexId) throws IOException
 Gets a ByteBuffer of data within this cache for the specified index id.

Static Public Attributes

static final int INDEX_SIZE = 6
 Represents the size of a index file.
static final int SECTOR_HEADER_SIZE = 8
 Represents the size of a Sectors header.
static final int SECTOR_SIZE = 520
 Represents the size of a Sectors header.

Protected Member Functions

 Cache (SeekableByteChannel sectorChannel, SeekableByteChannel indexChannel, int id)
 Constructs a new Cache with the specified sector and index channels and id.

Private Member Functions

Index readIndex (int indexId) throws IOException
 Reads an Index for the specified indexId and returns the decoded data.
Sector readSector (int sectorId, byte[] data, int offset, int length) throws IOException
 Reads a Sector for the specified sectorId and returns the decoded data.

Private Attributes

final ByteBuffer buffer = ByteBuffer.allocate(SECTOR_SIZE)
 A ByteBuffer allocated to SECTOR_SIZE.
final int id
 Represents the id of this Cache.
final SeekableByteChannel indexChannel
 A byte channel that contains a series of variable-length bytes which represent a index.
final SeekableByteChannel sectorChannel
 A byte channel that contains a series of variable-length bytes which represent a sector.

Detailed Description

Represents a Sector and Index cache.

Author
Artem Batutin artem.nosp@m.batu.nosp@m.tin@g.nosp@m.mail.nosp@m..com
Ryley Kimmel ryley.nosp@m..kim.nosp@m.mel@l.nosp@m.ive..nosp@m.com

Definition at line 14 of file Cache.java.

Constructor & Destructor Documentation

◆ Cache()

com.runehive.fs.cache.Cache.Cache ( SeekableByteChannel sectorChannel,
SeekableByteChannel indexChannel,
int id )
protected

Constructs a new Cache with the specified sector and index channels and id.

Parameters
sectorChannelThe cache sectors byte channel.
indexChannelThe cache sectors index channel.
idThis caches id.

Definition at line 67 of file Cache.java.

67 {
68 this.sectorChannel = sectorChannel;
69 this.indexChannel = indexChannel;
70 this.id = ++id;
71 }

References id, indexChannel, and sectorChannel.

Member Function Documentation

◆ get()

ByteBuffer com.runehive.fs.cache.Cache.get ( final int indexId) throws IOException

Gets a ByteBuffer of data within this cache for the specified index id.

Parameters
indexIdThe file id to get.
Returns
A wrapped byte buffer of the specified files data, never null.
Exceptions
IOExceptionIf some I/O exception occurs.

Definition at line 81 of file Cache.java.

81 {
82 final Index index = readIndex(indexId);
83 if (!index.check()) {
84 return null;
85 }
86
87 long position = (long) index.getId() * SECTOR_HEADER_SIZE;
88
89 Preconditions.checkArgument(sectorChannel.size() >= position + SECTOR_SIZE);
90
91 byte[] data = new byte[index.getLength()];
92 int next = index.getId();
93 int offset = 0;
94
95 for (int chunk = 0; offset < index.getLength(); chunk++) {
96 int read = Math.min(index.getLength() - offset, 512);
97
98 Sector sector = readSector(next, data, offset, read);
99 sector.check(id, indexId, chunk);
100
101 next = sector.getNextIndexId();
102 offset += read;
103 }
104
105 return ByteBuffer.wrap(data);
106 }
val index

References com.runehive.fs.cache.Sector.check(), com.runehive.fs.cache.Sector.getNextIndexId(), readIndex(), readSector(), SECTOR_HEADER_SIZE, SECTOR_SIZE, and sectorChannel.

Referenced by com.runehive.fs.cache.decoder.RegionDecoder.load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readIndex()

Index com.runehive.fs.cache.Cache.readIndex ( int indexId) throws IOException
private

Reads an Index for the specified indexId and returns the decoded data.

Parameters
indexIdThe id of the index to read.
Returns
The decoded index.
Exceptions
IOExceptionIf some I/O exception occurs.

Definition at line 115 of file Cache.java.

115 {
116 long position = (long) indexId * INDEX_SIZE;
117
118 buffer.clear().limit(INDEX_SIZE);
119 indexChannel.position(position);
120 indexChannel.read(buffer);
121 buffer.flip();
122
123 return Index.decode(buffer);
124 }

References buffer, com.runehive.fs.cache.Index.decode(), INDEX_SIZE, and indexChannel.

Referenced by get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readSector()

Sector com.runehive.fs.cache.Cache.readSector ( int sectorId,
byte[] data,
int offset,
int length ) throws IOException
private

Reads a Sector for the specified sectorId and returns the decoded data.

Parameters
sectorIdThe id of the sector to read.
dataThe sectors data.
offsetThe sectors data offset.
lengthThe length of the sectors data.
Returns
The decoded sector.
Exceptions
IOExceptionIf some I/O exception occurs.

Definition at line 136 of file Cache.java.

136 {
137 long position = (long) sectorId * SECTOR_SIZE;
138
139 buffer.clear().limit(length + SECTOR_HEADER_SIZE);
140 sectorChannel.position(position);
141 sectorChannel.read(buffer);
142 buffer.flip();
143
144 return Sector.decode(buffer, data, offset, length);
145 }

References buffer, com.runehive.fs.cache.Sector.decode(), SECTOR_HEADER_SIZE, SECTOR_SIZE, and sectorChannel.

Referenced by get().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ buffer

final ByteBuffer com.runehive.fs.cache.Cache.buffer = ByteBuffer.allocate(SECTOR_SIZE)
private

A ByteBuffer allocated to SECTOR_SIZE.

This byte buffer is used to read index and sector data from their respective byte channels.

Definition at line 41 of file Cache.java.

Referenced by readIndex(), and readSector().

◆ id

final int com.runehive.fs.cache.Cache.id
private

Represents the id of this Cache.

Definition at line 58 of file Cache.java.

Referenced by Cache().

◆ INDEX_SIZE

final int com.runehive.fs.cache.Cache.INDEX_SIZE = 6
static

Represents the size of a index file.

Calculating the total size of a index file. the total size may be that of a long.

Definition at line 20 of file Cache.java.

Referenced by readIndex().

◆ indexChannel

final SeekableByteChannel com.runehive.fs.cache.Cache.indexChannel
private

A byte channel that contains a series of variable-length bytes which represent a index.

Definition at line 53 of file Cache.java.

Referenced by Cache(), and readIndex().

◆ SECTOR_HEADER_SIZE

final int com.runehive.fs.cache.Cache.SECTOR_HEADER_SIZE = 8
static

Represents the size of a Sectors header.

Calculating the total size of the sector header. the total size may be that of a long.

Definition at line 26 of file Cache.java.

Referenced by get(), and readSector().

◆ SECTOR_SIZE

final int com.runehive.fs.cache.Cache.SECTOR_SIZE = 520
static

Represents the size of a Sectors header.

Calculating the total size of the sector header. the total size may be that of a long

Definition at line 32 of file Cache.java.

Referenced by get(), and readSector().

◆ sectorChannel

final SeekableByteChannel com.runehive.fs.cache.Cache.sectorChannel
private

A byte channel that contains a series of variable-length bytes which represent a sector.

Definition at line 47 of file Cache.java.

Referenced by Cache(), get(), and readSector().


The documentation for this class was generated from the following file: