RuneHive-Game
Loading...
Searching...
No Matches
Sector.java
Go to the documentation of this file.
1package com.runehive.fs.cache;
2
3import com.google.common.base.Preconditions;
4import com.runehive.fs.util.ByteBufferUtil;
5
6import java.nio.ByteBuffer;
7
8/**
9 * Represents a sector within some {@link Cache}.
10 *
11 * @author Ryley Kimmel <ryley.kimmel@live.com>
12 */
13public final class Sector {
14
15 /** The id of the index this sector is within. */
16 private final int indexId;
17
18 /** This sectors chunk. */
19 private final int chunk;
20
21 /** The next index within this sector. */
22 private final int nextIndexId;
23
24 /** The id of the cache this sector is in. */
25 private final int cacheId;
26
27 /**
28 * Constructs a new {@link Sector} with the expected index id, chunk, next
29 * index id and cache id. This constructor is marked {@code private} and
30 * should not be modified to be invoked directly, use {@link
31 * Sector#decode(ByteBuffer, byte[], int, int)} instead.
32 *
33 * @param indexId The id of the index this sector is within.
34 * @param chunk This sectors chunk.
35 * @param nextIndexId The next index within this sector.
36 * @param cacheId The id of the cache this sector is in.
37 */
38 private Sector(int indexId, int chunk, int nextIndexId, int cacheId) {
39 this.indexId = indexId;
40 this.chunk = chunk;
41 this.nextIndexId = nextIndexId;
42 this.cacheId = cacheId;
43 }
44
45 /**
46 * Decodes a {@link Sector} from the specified {@link ByteBuffer}.
47 *
48 * @param buffer The {@link ByteBuffer} to get the sector from.
49 * @param data The expected data within the sector.
50 * @param offset The expected offset of the sector.
51 * @param length The expected length of the sector.
52 * @return The decoded sector.
53 */
54 public static Sector decode(ByteBuffer buffer, byte[] data, int offset, int length) {
55 int indexId = buffer.getShort() & 0xFFFF;
56 int chunk = buffer.getShort() & 0xFFFF;
58 int cacheId = buffer.get() & 0xFF;
59 buffer.get(data, offset, length);
60 return new Sector(indexId, chunk, nextIndexId, cacheId);
61 }
62
63 /**
64 * Tests whether or not this sector is valid.
65 *
66 * @param cacheId The cache id to test.
67 * @param indexId The index id to test.
68 * @param chunk The chunk id to test.
69 */
70 public void check(int cacheId, int indexId, int chunk) {
71 Preconditions.checkArgument(this.cacheId == cacheId);
72 Preconditions.checkArgument(this.indexId == indexId);
73 Preconditions.checkArgument(this.chunk == chunk);
74 }
75
76 /** Returns the id of the index this sector is within. */
77 public int getIndexId() {
78 return indexId;
79 }
80
81 /** Returns this sectors chunk. */
82 public int getChunk() {
83 return chunk;
84 }
85
86 /** Returns the next index within this sector. */
87 public int getNextIndexId() {
88 return nextIndexId;
89 }
90
91 /** Returns the id of the cache this sector is in. */
92 public int getCacheId() {
93 return cacheId;
94 }
95
96}
void check(int cacheId, int indexId, int chunk)
Tests whether or not this sector is valid.
Definition Sector.java:70
final int indexId
The id of the index this sector is within.
Definition Sector.java:16
Sector(int indexId, int chunk, int nextIndexId, int cacheId)
Constructs a new Sector with the expected index id, chunk, next index id and cache id.
Definition Sector.java:38
final int cacheId
The id of the cache this sector is in.
Definition Sector.java:25
int getCacheId()
Returns the id of the cache this sector is in.
Definition Sector.java:92
int getNextIndexId()
Returns the next index within this sector.
Definition Sector.java:87
int getChunk()
Returns this sectors chunk.
Definition Sector.java:82
final int nextIndexId
The next index within this sector.
Definition Sector.java:22
int getIndexId()
Returns the id of the index this sector is within.
Definition Sector.java:77
final int chunk
This sectors chunk.
Definition Sector.java:19
static Sector decode(ByteBuffer buffer, byte[] data, int offset, int length)
Decodes a Sector from the specified ByteBuffer.
Definition Sector.java:54
A static-utility class containing extension or helper methods for ByteBuffers.
static int getMedium(ByteBuffer buffer)
Gets a 24-bit medium integer from the specified ByteBuffer, this method does not mark the ByteBuffers...