1 32 package com.imagero.uio.buffer; 33 34 import java.io.IOException ; 35 36 41 public class DefaultBufferManager implements MutableBufferManager { 42 43 MemoryAccessManager accessManager; 44 45 int[] startIndex; 46 int length; 47 48 public DefaultBufferManager(Buffer[] ds) { 49 accessManager = MemoryAccessManager.createMemoryAccessManager(MemoryAccessManager.DROP_NEVER); 50 51 for (int i = 0; i < ds.length; i++) { 52 accessManager.put(new Integer (i), ds[i]); 53 } 54 55 startIndex = new int[ds.length + 1]; 56 57 for (int i = 1; i < startIndex.length; i++) { 58 startIndex[i] = startIndex[i - 1] + ds[i - 1].length(); 59 } 60 61 if (ds.length > 0) { 62 length = startIndex[startIndex.length - 1]; 63 } 64 } 65 66 public long getDataStart(int i) { 67 return startIndex[i]; 68 } 69 70 77 public byte[] getData(int i) throws IOException { 78 try { 79 Buffer buffer = accessManager.get(new Integer (i)); 80 if (buffer == null) { 81 return empty; 82 } 83 return buffer.getData(); 84 } 85 catch (IOException ex) { 86 ex.printStackTrace(); 87 return empty; 88 } 89 } 90 91 96 public int getCount() { 97 return accessManager.getCount(); 98 } 99 100 106 public int getDataLength(int i) { 107 return startIndex[i + 1] - startIndex[i]; 108 } 109 110 117 public int getIndex(long pos) { 118 if (pos < 0) { 119 return -1; 120 } 121 for (int i = 0; i < startIndex.length; i++) { 122 if (startIndex[i] > pos) { 123 return i - 1; 124 } 125 } 126 return -1; 127 } 128 129 134 public long getLength() { 135 return length; 136 } 137 138 141 public void close() { 142 accessManager.clear(); 143 } 144 145 151 public void setDirty(long from, long to) { 152 153 } 154 155 160 public void setDirty(int index) { 161 162 } 163 164 169 public void flush() throws IOException { 170 171 } 172 173 176 public void clear() { 177 accessManager.clear(); 178 } 179 180 185 public void clear(long start, long end) { 186 int bs = getIndex(start); 188 long ps = getDataStart(bs); 189 if(ps < start) { 190 bs++; 191 } 192 int eb = getIndex(end); 194 long pe = getDataStart(eb); 195 int length = getDataLength(eb); 196 if(pe + length > end) { 197 eb--; 198 } 199 for(int i = bs; i <= eb; i++) { 200 Integer key = new Integer (i); 201 accessManager.drop(key); 202 } 203 } 204 205 208 public int getMaxCache() { 209 return getCount(); 210 } 211 212 215 public void setMaxCache(int max) { 216 } 217 } 218 | Popular Tags |