1 32 package com.imagero.uio; 33 34 import java.io.EOFException ; 35 import java.io.IOException ; 36 37 42 public class RandomAccessByteArrayRO extends AbstractRandomAccessRO { 43 44 int fp; 45 46 byte[] buf; 47 int length; 48 int offset; 49 50 55 public RandomAccessByteArrayRO(byte[] data, int byteOrder) throws IOException { 56 this(data, 0, data.length, byteOrder); 57 } 58 59 66 public RandomAccessByteArrayRO(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 public int read(byte[] b) throws IOException { 98 return read(b, 0, b.length); 99 } 100 101 public int read(byte[] b, int off, int length) throws IOException { 102 int len = Math.min(length, this.length - (fp + offset)); 103 if(len <= 0) { 104 return -1; 105 } 106 System.arraycopy(buf, fp + offset, b, off, len); 107 fp += len; 108 return len; 109 } 110 111 114 public void close() { 115 } 116 117 protected int getOffset() { 118 return offset; 119 } 120 121 protected void setOffset(int offset) { 122 this.offset = offset; 123 } 124 125 public int skip(int n) throws IOException { 126 return skipBytes(n); 127 } 128 129 public long getFilePointer() throws IOException { 130 return fp + offset; 131 } 132 133 140 public void seek(long pos) { 141 if((pos > length + offset) || pos < offset) { 142 throw new ArrayIndexOutOfBoundsException ("" + pos); 143 } 144 this.fp = (int)pos; 145 } 146 147 152 public long length() { 153 return length; 154 } 155 } 156 | Popular Tags |