1
2
3
4
5
6
7 package net.sf.dexterim.oscar;
8
9 import net.sf.dexterim.oscar.entity.ByteBased;
10 import net.sf.dexterim.oscar.entity.DWord;
11 import net.sf.dexterim.oscar.entity.Word;
12
13
14 /***
15 * @author christoph
16 */
17 class AllocatedByteBuffer extends OscarByteBuffer {
18 private int position;
19 private byte[] data;
20 private int fill;
21
22 public AllocatedByteBuffer(byte[] data) {
23 this.data = data;
24 this.position = 0;
25 this.fill = data.length;
26 }
27
28 public int position(int position) {
29 int oldPosition = this.position;
30 this.position = position;
31
32 return oldPosition;
33 }
34
35 public int position() {
36 return this.position;
37 }
38
39 public int size() {
40 return data.length;
41 }
42
43 /***
44 * @see net.sf.dexterim.oscar.OscarByteBuffer#fill()
45 */
46 public int fill() {
47 return fill;
48 }
49
50 /***
51 * @see net.sf.dexterim.oscar.OscarByteBuffer#strip()
52 */
53 public int strip() {
54 return this.fill = position;
55 }
56
57 /***
58 * @see net.sf.dexterim.oscar.OscarByteBuffer#getWord()
59 */
60 public Word getWord() {
61 Word returnValue = new Word(data[position], data[position+1]);
62
63 position+=returnValue.getLength();
64
65 return returnValue;
66 }
67
68 /***
69 * @see net.sf.dexterim.oscar.OscarByteBuffer#getDWord()
70 */
71 public DWord getDWord() {
72 byte[] targetArray = new byte[4];
73 System.arraycopy(data, position, targetArray, 0, targetArray.length);
74
75 position+=targetArray.length;
76 return new DWord(targetArray);
77 }
78
79 public OscarByteBuffer write(ByteBased source) {
80 byte[] sourceArray = source.toByteArray();
81 System.arraycopy(sourceArray, 0, data, position, sourceArray.length);
82
83 position+=sourceArray.length;
84
85 return this;
86 }
87
88 /*** Writes a OscarByteBuffer. Uses a single System.arraycopy.
89 * @see net.sf.dexterim.oscar.OscarByteBuffer#write(net.sf.dexterim.oscar.OscarByteBuffer)
90 */
91 public OscarByteBuffer write(OscarByteBuffer buffer) {
92 byte[] sourceArray = buffer.slice();
93 System.arraycopy(sourceArray, 0, data, position, sourceArray.length);
94
95 position+=sourceArray.length;
96
97 return this;
98 }
99
100 public OscarByteBuffer write(byte source) {
101 data[position++] = source;
102
103 return this;
104 }
105
106 public int getByte() {
107 return data[position++];
108 }
109
110 /***
111 * @return
112 */
113 public byte[] array() {
114 return data;
115 }
116
117 /***
118 * Allocates a new Array with position entries and returns this new
119 * Array. Modifications done to return value will not be reflected in
120 * AllocatedByteBuffer instance.
121 * @return new allocated array containing data up to position
122 */
123 public byte[] slice() {
124 byte slice[] = new byte[fill];
125
126 System.arraycopy(data, 0, slice, 0, fill);
127
128 return slice;
129 }
130 }