1 32 package com.imagero.uio; 33 34 import java.io.EOFException ; 35 import java.io.IOException ; 36 37 42 public class RandomAccessByteArray extends AbstractRandomAccess { 43 44 int fp; 45 46 byte[] buf; 47 int length; 48 int offset; 49 50 55 public RandomAccessByteArray(byte[] data, int byteOrder) throws IOException { 56 this(data, 0, data.length, byteOrder); 57 } 58 59 66 public RandomAccessByteArray(byte[] data, int off, int length, int byteOrder) throws IOException { 67 this.buf = data; 68 this.length = length; 69 this.offset = off; 70 _setByteOrder(byteOrder); 71 } 72 73 79 public int read() { 80 if(fp < length + offset) { 81 return buf[offset + fp++] & 0xFF; 82 } 83 else { 84 return -1; 85 } 86 } 87 88 protected int _read() throws EOFException { 89 if(fp < length + offset) { 90 return buf[offset + fp++] & 0xFF; 91 } 92 else { 93 throw new EOFException (); 94 } 95 } 96 97 103 public void setLength(long newLength) throws IOException { 104 if(newLength < 0) { 105 throw new ArrayIndexOutOfBoundsException ("" + newLength); 106 } 107 if(newLength == length) { 108 return; 109 } 110 else if(newLength < buf.length - offset) { 111 length = (int)newLength; 112 return; 113 } 114 else { 115 throw new IOException ("length too big"); 119 } 120 } 121 122 123 public int read(byte[] b) throws IOException { 124 return read(b, 0, b.length); 125 } 126 127 public int read(byte[] b, int off, int length) throws IOException { 128 int len = Math.min(length, this.length - (fp + offset)); 129 if(len <= 0) { 130 return -1; 131 } 132 System.arraycopy(buf, fp + offset, b, off, len); 133 fp += len; 134 return len; 135 } 136 137 138 147 public void write(byte[] b) throws IOException { 148 write(b, 0, b.length); 149 } 150 151 163 public void write(byte[] b, int off, int length) { 164 int len = Math.min(length, this.length - (fp + offset)); 165 System.arraycopy(b, off, buf, fp + offset, len); 166 fp += len; 167 } 168 169 174 public void write(int b) throws IOException { 175 buf[offset + fp++] = (byte)b; 176 } 177 178 181 public void close() { 182 } 183 184 public int getOffset() { 185 return offset; 186 } 187 188 public void setOffset(int offset) { 189 this.offset = offset; 190 } 191 192 public int skip(int n) throws IOException { 193 return skipBytes(n); 194 } 195 196 public long getFilePointer() throws IOException { 197 return fp + offset; 198 } 199 200 207 public void seek(long pos) { 208 if((pos > length + offset) || pos < offset) { 209 throw new ArrayIndexOutOfBoundsException ("" + pos); 210 } 211 this.fp = (int)pos; 212 } 213 214 219 public long length() { 220 return length; 221 } 222 } 223 | Popular Tags |