1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.michaelo.tomcat.pac;
17
18 import java.math.BigInteger;
19 import java.nio.Buffer;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22
23
24
25
26
27
28
29 public class PacDataBuffer {
30
31 private static final BigInteger TWO_COMPL_REF = BigInteger.ONE.shiftLeft(64);
32
33 private final ByteBuffer buf;
34
35
36
37
38
39
40
41 public PacDataBuffer(byte[] pacDataBytes) {
42 buf = ByteBuffer.wrap(pacDataBytes);
43 buf.order(ByteOrder.LITTLE_ENDIAN);
44 }
45
46 public int position() {
47 return ((Buffer) buf).position();
48 }
49
50 public PacDataBuffer position(int newPosition) {
51 ((Buffer) buf).position(newPosition);
52 return this;
53 }
54
55 public PacDataBuffer skip(int bytes) {
56 ((Buffer) buf).position(buf.position() + bytes);
57 return this;
58 }
59
60 protected PacDataBuffer align(int bytes) {
61 int shift = ((Buffer) buf).position() & bytes - 1;
62 if (bytes != 0 && shift != 0)
63 skip(bytes - shift);
64 return this;
65 }
66
67 public PacDataBuffer get(byte[] dst) {
68 buf.get(dst);
69 return this;
70 }
71
72 public int getInt() {
73 align(4);
74 return buf.getInt();
75 }
76
77 public int getUnsignedShort() {
78 align(2);
79 return buf.getShort() & 0xffff;
80 }
81
82 public long getUnsignedInt() {
83 align(4);
84 return buf.getInt() & 0xffffffffL;
85 }
86
87 public BigInteger getUnsignedLong() {
88 align(8);
89 long temp = buf.getLong();
90 BigInteger value = BigInteger.valueOf(temp);
91 if (value.compareTo(BigInteger.ZERO) < 0)
92 value = value.add(TWO_COMPL_REF);
93 return value;
94 }
95
96 }