1 32 package com.imagero.uio.buffer; 33 34 import com.imagero.uio.RandomAccessRO; 35 import com.imagero.uio.io.IOutils; 36 37 import java.io.IOException ; 38 39 44 public class RABufferManager extends AbstractBufferManager { 45 46 Integer maxKey = new Integer (0); 47 48 53 public RABufferManager(RandomAccessRO ro, long offset, int length) { 54 this(ro, offset, length, defaultBufferSize); 55 } 56 57 64 public RABufferManager(RandomAccessRO ro, long offset, int length, int dsLength) { 65 this.bufferSize = dsLength; 66 this.ro = ro; 67 this.offset = offset; 68 this.length = length; 69 70 RABufferRO rads = createBuffer(ro, offset, dsLength); 71 accessManager.add(rads); 72 } 73 74 protected RABufferRO createBuffer(RandomAccessRO ro, long offset, int dsLength) { 75 return new RABufferRO(ro, offset, dsLength); 76 } 77 78 87 public byte[] getData(int i) throws IOException { 88 try { 89 return getDataImpl(i); 90 } 91 catch(ArrayIndexOutOfBoundsException ex) { 92 throw ex; 93 } 94 } 95 96 105 protected byte[] getDataImpl(int i) throws IOException { 106 Integer key = new Integer (i); 107 RABufferRO uds = ((RABufferRO) (accessManager.get(key))); 108 if(uds == null) { 109 uds = createBuffer(ro, offset + bufferSize * i, bufferSize); 110 accessManager.put(key, uds); 111 if(key.intValue() > maxKey.intValue()) { 112 maxKey = key; 113 } 114 } 115 return uds.getData(); 116 } 117 118 125 public int getDataLength(int i) { 126 return bufferSize; 127 } 128 129 136 public int getIndex(long pos) { 137 if(pos < 0) { 138 return -1; 139 } 140 return (int) (pos / bufferSize); 141 } 142 143 146 public long getLength() { 147 return length; 148 } 149 150 156 public long getDataStart(int i) { 157 return bufferSize * i; 161 } 162 163 public void close() { 164 IOutils.closeStream(ro); 165 accessManager.clear(); 166 maxKey = new Integer (0); 167 } 168 169 public void clear() { 170 accessManager.clear(); 171 maxKey = new Integer (0); 172 } 173 174 public int getByteOrder() { 175 return ro.getByteOrder(); 176 } 177 } 178 | Popular Tags |