1 32 package com.imagero.uio.buffer; 33 34 import com.imagero.uio.RandomAccessRO; 35 36 import java.io.IOException ; 37 38 45 public class RABufferRO implements Buffer { 46 47 RandomAccessRO ro; 48 49 long offset; 50 int length; 51 byte[] data; 52 53 public RABufferRO(RandomAccessRO ro, long offset, int length) { 54 this.ro = ro; 55 this.offset = offset; 56 this.length = length; 57 } 58 59 64 public byte[] getData() throws IOException { 65 if(data == null) { 66 readData(); 67 } 68 return data; 69 } 70 71 public byte[] getData(byte[] d) throws IOException { 72 if(data == null) { 73 readData(); 74 } 75 System.arraycopy(data, 0, d, 0, Math.min(data.length, d.length)); 76 return d; 77 } 78 79 public int length() { 80 return length; 81 } 82 83 private void readData() throws IOException { 84 long currentOffset = ro.getFilePointer(); 85 ro.seek(offset); 86 long len = ro.length(); 87 int maxlen = (int) Math.min(length, len - offset); 88 if(maxlen < 0) { 89 maxlen = 0; 90 } 91 data = new byte[length]; 92 ro.readFully(data, 0, maxlen); 93 ro.seek(currentOffset); 94 } 95 96 public boolean isDirty() { 97 return false; 98 } 99 } 100 | Popular Tags |