-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.java
86 lines (81 loc) · 2.33 KB
/
Buffer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* A Buffer that stores bytes in a byte array. The array and individual shorts
* can be retrieved and set.
* @author Joshua Rush (jdrush89)
* @author Benjamin Roble (broble)
* @version Nov 2, 2011
*/
public class Buffer
{
//the byte array that stores the bytes in this buffer
private byte[] data;
/**
* Create a new Buffer, initializing the data array.
*/
public Buffer()
{
data = new byte[BufferPool.BUFFER_SIZE];
}
// ----------------------------------------------------------
/**
* Return the byte array storing data.
* @return the byte array
*/
public byte[] getData()
{
return data;
}
// ----------------------------------------------------------
/**
* Set the data byte array.
* @param data the data to set
*/
public void setData(byte[] data)
{
this.data = data;
}
/**
* Set the bytes in the data array to be the bytes in the newData array,
* starting at the indicated position. pos + newData.length should not be
* greater than data.length.
* @param pos the position to start overwriting bytes at
* @param newData the byte array containing the bytes to be copied
*/
public void setRecord(int pos, byte[] newData)
{
for(int i = 0; i < newData.length; i++)
{
data[i + pos] = newData[i];
}
}
/**
* Return a record made up of a 2 byte key and 2 byte value.
* @param pos the byte position where the record starts
* @return the record
*/
public byte[] getRecord(int pos)
{
byte[] record = new byte[4];
System.arraycopy(data, pos, record, 0, 4);
return record;
}
/**
* Returns a short made up of the bytes in the array at position pos and
* pos + 1.
* @param pos the position in the data array to get the short from
* @return a short from the data array
*/
public short getShort(int pos)
{
ByteBuffer bb = ByteBuffer.allocate(2);
bb.order(ByteOrder.BIG_ENDIAN);
bb.put(data[pos]);
bb.put(data[pos+1]);
short shortVal = bb.getShort(0);
if (shortVal < 0)
System.out.println("Short was negative: " + shortVal);
return shortVal;
}
}