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