comparison src/org/dancres/util/MSBBytePacker.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.util;
2
3 /**
4 A class to pack and unpack longs or ints into a byte array.
5 In this case, longs and ints are packed MSB first
6 */
7 public class MSBBytePacker extends BytePacker {
8 private byte[] theBytes;
9
10 MSBBytePacker() {
11 }
12
13 MSBBytePacker(byte[] aBytes) {
14 theBytes = aBytes;
15 }
16
17 public void setData(byte[] aBytes) {
18 theBytes = aBytes;
19 }
20
21 public int getInt(int anOffset) {
22 int b1 = theBytes[anOffset] << 24;
23 int b2 = theBytes[anOffset + 1] << 16;
24 int b3 = theBytes[anOffset + 2] << 8;
25 int b4 = theBytes[anOffset + 3] <<0;
26
27 return ((b1 & 0xFF000000) | (b2 & 0x00FF0000) |
28 (b3 & 0X0000FF00) | (b4 & 0x000000FF));
29 }
30
31 public void putArray(byte[] anArray, int anOffset) {
32 System.arraycopy(anArray, 0, theBytes, anOffset, anArray.length);
33 }
34
35 public byte[] getArray(int anOffset, int aLength) {
36 byte[] myArray = new byte[aLength];
37
38 System.arraycopy(theBytes, anOffset, myArray, 0, aLength);
39
40 return myArray;
41 }
42
43 public long getLong(int anOffset) {
44 return (((long) getInt(anOffset)) << 32) +
45 ((getInt(anOffset + 4)) & 0xFFFFFFFFL);
46 }
47
48 public void putInt(int anInt, int anOffset) {
49 theBytes[anOffset] = (byte) ((anInt >>> 24) & 0xFF);
50 theBytes[anOffset + 1] = (byte) ((anInt >>> 16) & 0xFF);
51 theBytes[anOffset + 2] = (byte) ((anInt >>> 8) & 0xFF);
52 theBytes[anOffset + 3] = (byte) ((anInt >>> 0) & 0xFF);
53 }
54
55 public void putLong(long aLong, int anOffset) {
56 theBytes[anOffset] = (byte) ((aLong >>> 56) & 0xFF);
57 theBytes[anOffset + 1] = (byte) ((aLong >>> 48) & 0xFF);
58 theBytes[anOffset + 2] = (byte) ((aLong >>> 40) & 0xFF);
59 theBytes[anOffset + 3] = (byte) ((aLong >>> 32) & 0xFF);
60 theBytes[anOffset + 4] = (byte) ((aLong >>> 24) & 0xFF);
61 theBytes[anOffset + 5] = (byte) ((aLong >>> 16) & 0xFF);
62 theBytes[anOffset + 6] = (byte) ((aLong >>> 8) & 0xFF);
63 theBytes[anOffset + 7] = (byte) ((aLong >>> 0) & 0xFF);
64 }
65 }