DEADSOFTWARE

35dc7656fd7adcb6493696aa7ebba897f666a1e0
[LongFlight.git] / ByteArrayOutputStream.java
1 package code.kalter.longflight;
3 /**
4 * Удобная запись разных типов в байтовый массив
5 *
6 * @author KalterFive
7 */
8 public class ByteArrayOutputStream {
10 private final byte[] array;
11 private int position;
13 public ByteArrayOutputStream(int size) {
14 array = new byte[size];
15 position = 0;
16 }
18 public void writeByte(byte b) {
19 array[position++] = b;
20 }
22 public void writeShort(short s) {
23 writeByte((byte) (s >> 8));
24 writeByte((byte) s);
25 }
27 public void writeInt(int i) {
28 writeShort((short) (i >> 16));
29 writeShort((short) i);
30 }
32 public void writeLong(long l) {
33 writeInt((int) (l >> 32));
34 writeInt((int) l);
35 }
37 public void writeByteArray(byte[] array, int length) {
38 for (int i = 0; i < length; i++) {
39 writeByte(array[i]);
40 }
41 }
43 public void writeShortArray(short[] array, int length) {
44 for (int i = 0; i < length; i++) {
45 writeShort(array[i]);
46 }
47 }
49 public void writeIntArray(int[] array, int length) {
50 for (int i = 0; i < length; i++) {
51 writeInt(array[i]);
52 }
53 }
55 public void writeLongArray(long[] array, int length) {
56 for (int i = 0; i < length; i++) {
57 writeLong(array[i]);
58 }
59 }
61 public byte[] toArray() {
62 return array;
63 }
64 }