1 package com.tapina.robe.runtime; 2 3 import java.awt.*; 4 5 /*** 6 * This class provides abstracted access to a virtual byte array. 7 * The byte array in question will actually be a subset of an existing byte array. 8 */ 9 public final class ByteArray extends BinaryDataSource { 10 private final byte[] array; 11 private final int offset; 12 private final int length; 13 14 public ByteArray(byte[] array, int offset, int length) { 15 this.array = array; 16 this.offset = offset; 17 this.length = length; 18 } 19 20 public final byte getByte(int offset) { 21 checkOffset(offset); 22 return array[offset + this.offset]; 23 } 24 25 private final void checkOffset(int offset) { 26 if (offset > this.length) { 27 throw new ArrayIndexOutOfBoundsException(offset); 28 } 29 } 30 31 public final void set(int offset, byte b) { 32 checkOffset(offset); 33 array[offset + this.offset] = b; 34 } 35 36 public final int getWord(int offset) { 37 checkOffset(offset + 3); 38 return ByteArrayUtils.getInt(array, offset + this.offset); 39 } 40 41 public final void setInt(int offset, int i) { 42 checkOffset(offset + 3); 43 ByteArrayUtils.putInt(array, offset + this.offset, i); 44 } 45 46 public final void setString0(int offset, String s) { 47 checkOffset(offset + s.length()); 48 ByteArrayUtils.putString0(array, offset + this.offset, s); 49 } 50 51 52 public void setString0(int offset, String s, int length) { 53 checkOffset(offset + s.length() - 1); 54 ByteArrayUtils.putString0(array, offset + this.offset, s, length); 55 } 56 57 public final Rectangle getRectangle(int offset) { 58 checkOffset(offset + 15); 59 return ByteArrayUtils.getRectangle(array, offset + this.offset); 60 } 61 62 public final void setRectangle(int offset, Rectangle r) { 63 checkOffset(offset + 15); 64 ByteArrayUtils.putRectangle(array, offset + this.offset, r); 65 } 66 67 public final Point getPoint(int offset) { 68 checkOffset(offset + 7); 69 return ByteArrayUtils.getPoint(array, offset + this.offset); 70 } 71 72 public final void setPoint(int offset, Point p) { 73 checkOffset(offset + 7); 74 ByteArrayUtils.putPoint(array, offset + this.offset, p); 75 } 76 77 public final int getLength() { 78 return length; 79 } 80 81 public byte[] getArray() { 82 return array; 83 } 84 85 public int getOffset() { 86 return offset; 87 } 88 89 String getTerminatedString(int offset, StringTerminator stringTerminator, int maxLength) { 90 checkOffset(offset); 91 return ByteArrayUtils.getTerminatedString(array, offset + this.offset, stringTerminator, maxLength); 92 } 93 94 public ByteArray getByteArray(int offset, int count, boolean create) { 95 checkOffset(this.offset); 96 try { 97 checkOffset(this.offset + count - 1); 98 } catch (ArrayIndexOutOfBoundsException e) { 99 if (!create) { 100 return null; 101 } else { 102 throw e; 103 } 104 } 105 return new ByteArray(array, offset + this.offset, count); 106 } 107 108 public byte[] getBytes(int offset, int count) { 109 checkOffset(offset + count - 1); 110 final byte[] data = new byte[count]; 111 System.arraycopy(array, offset + this.offset, data, 0, count); 112 return data; 113 } 114 115 public long get5ByteValue(int offset) { 116 checkOffset(offset + 4); 117 return ByteArrayUtils.get5ByteValue(array, offset + this.offset); 118 } 119 120 public int[] getWords(int offset, int count) { 121 int[] words = new int[count]; 122 for (count--; count >= 0; count--) { 123 words[count] = ByteArrayUtils.getInt(array, offset + this.offset + (count * 4)); 124 } 125 return words; 126 } 127 }