1 32 package com.imagero.uio.buffer.arrays; 33 34 import com.imagero.uio.RandomAccessFactory; 35 import com.imagero.uio.Sys; 36 import com.imagero.uio.buffer.Buffer; 37 import com.imagero.uio.buffer.MemoryAccessManager; 38 import com.imagero.uio.buffer.MutableBuffer; 39 import com.imagero.uio.buffer.MutableBufferManager; 40 import com.imagero.uio.buffer.MutableByteBuffer; 41 42 import java.io.IOException ; 43 import java.util.Enumeration ; 44 45 50 public abstract class AbstractArrayBufferManager implements MutableBufferManager { 51 int tileSize; 52 protected int unitSize; 53 54 MemoryAccessManager accessManager; 55 56 private int byteOrder = RandomAccessFactory.BIG_ENDIAN; 57 int offset; 58 int length; 59 protected static int TILE_SIZE = 4096; 60 61 62 63 69 protected void readLE(byte[] b, int index) { 70 int start = (int) getDataStart(index); 71 int c = 0; 72 long length = getLength() / getUnitSize(); 73 long max = Math.min(length - start, tileSize); 74 for (int i = 0; i < max; i++) { 75 c = readUnitLE(start + i, b, c); 76 } 77 } 78 79 public long getDataStart(int i) { 80 return offset + i * tileSize * unitSize; 81 } 82 83 public int getIndex(long pos) { 84 return (int) (pos / (tileSize * unitSize)); 85 } 86 87 93 public void setDirty(long from, long to) { 94 int start = getIndex(from); 95 int end = getIndex(to); 96 for (int i = start; i <= end; i++) { 97 MutableBuffer buffer = (MutableBuffer) accessManager.get(i); 98 if (buffer != null) { 99 buffer.setDirty(); 100 } 101 } 102 } 103 104 109 public void setDirty(int index) { 110 MutableBuffer buffer = (MutableBuffer) accessManager.get(index); 111 if(buffer != null) { 112 buffer.setDirty(); 113 } 114 } 115 116 public void flush() { 117 flush(false); 118 } 119 120 126 protected void readBE(byte[] b, int index) { 127 int start = (int) getDataStart(index); 128 int c = 0; 129 long length = getLength() / getUnitSize(); 130 long max = Math.min(length - start, tileSize); 131 for (int i = 0; i < max; i++) { 132 c = readUnitBE(start + i, b, c); 133 } 134 } 135 136 144 protected abstract int readUnitBE(int offset, byte[] dest, int destOffset); 145 146 154 protected abstract int readUnitLE(int offset, byte[] dest, int destOffset); 155 156 public int getCount() { 157 return accessManager.getCount(); 158 } 159 160 166 public int getDataLength(int i) { 167 int count = accessManager.getCount(); 168 if (i < count - 1 || length % tileSize == 0) { 169 return tileSize * unitSize; 170 } 171 else { 172 return (length % tileSize) * unitSize; 173 } 174 } 175 176 182 protected void read(byte[] b, int index) { 183 if (byteOrder == RandomAccessFactory.LITTLE_ENDIAN) { 184 readLE(b, index); 185 } 186 else if (byteOrder == RandomAccessFactory.BIG_ENDIAN) { 187 readBE(b, index); 188 } 189 else { 190 throw new RuntimeException ("unknown byte order:" + byteOrder); 191 } 192 } 193 194 public byte[] getData(int i) throws IOException { 195 Buffer buffer = accessManager.get(i); 196 if (buffer == null) { 197 byte[] b = new byte[getDataLength(i)]; 198 read(b, i); 199 accessManager.put(new Integer (i), new MutableByteBuffer(b)); 200 } 201 return accessManager.get(i).getData(); 202 } 203 204 210 protected void write(byte[] b, int index) { 211 if (byteOrder == RandomAccessFactory.LITTLE_ENDIAN) { 212 writeLE(b, index); 213 } 214 else if (byteOrder == RandomAccessFactory.BIG_ENDIAN) { 215 writeBE(b, index); 216 } 217 else { 218 throw new RuntimeException ("unknown byte order:" + byteOrder); 219 } 220 } 221 222 227 public void flush(boolean free) { 228 Enumeration keys = accessManager.keys(); 229 while (keys.hasMoreElements()) { 230 Object key = keys.nextElement(); 231 if (accessManager.isDirty(key)) { 232 try { 233 write(accessManager.get(key).getData(), ((Integer )key).intValue()); 234 } 235 catch (IOException ex) { 236 ex.printStackTrace(); 237 } 238 } 239 } 240 if (free) { 241 accessManager.clear(); 242 } 243 } 244 245 248 public void close() { 249 Enumeration keys = accessManager.keys(); 250 while (keys.hasMoreElements()) { 251 Object key = keys.nextElement(); 252 if (accessManager.isDirty(key)) { 253 try { 254 write(accessManager.get(key).getData(), ((Integer )key).intValue()); 255 } 256 catch (IOException ex) { 257 ex.printStackTrace(); 258 } 259 } 260 } 261 accessManager.clear(); 262 } 263 264 public void clear() { 265 flush(true); 266 } 267 268 public void clear(long start, long end) { 269 int bs = getIndex(start); 271 long ps = getDataStart(bs); 272 if(ps < start) { 273 bs++; 274 } 275 int eb = getIndex(end); 277 long pe = getDataStart(eb); 278 int length = getDataLength(eb); 279 if(pe + length > end) { 280 eb--; 281 } 282 for(int i = bs; i <= eb; i++) { 283 Integer key = new Integer (i); 284 if (accessManager.isDirty(key)) { 285 try { 286 write(accessManager.get(key).getData(), key.intValue()); 287 } 288 catch (IOException ex) { 289 ex.printStackTrace(); 290 } 291 } 292 accessManager.drop(key); 293 } 294 } 295 296 protected void writeLE(byte[] b, int index) { 297 int start = (int) getDataStart(index); 298 int c = 0; 299 final int len = b.length / unitSize; 300 for (int i = 0; i < len; i++) { 301 c = writeUnitLE(b, c, start + i); 302 } 303 } 304 305 protected abstract int writeUnitLE(byte[] b, int c, int offset); 306 307 protected void writeBE(byte[] b, int index) { 308 int start = (int) getDataStart(index); 309 int c = 0; 310 final int len = b.length / unitSize; 311 for (int i = 0; i < len; i++) { 312 c = writeUnitBE(b, c, start + i); 313 } 314 } 315 316 protected abstract int writeUnitBE(byte[] b, int c, int offset); 317 318 323 public long getLength() { 324 return length * unitSize; 325 } 326 327 332 public int getUnitSize() { 333 return unitSize; 334 } 335 336 public int getByteOrder() { 337 return byteOrder; 338 } 339 340 public void setByteOrder(int byteOrder) { 341 this.byteOrder = byteOrder; 342 } 343 344 345 static void printHex(long[] vl) { 346 for (int i = 0; i < vl.length; i++) { 347 Sys.out.print(Long.toHexString(vl[i])); 348 Sys.out.print(" "); 349 } 350 Sys.out.println("\n*************************"); 351 } 352 353 static void printHex(int[] vi) { 354 for (int i = 0; i < vi.length; i++) { 355 Sys.out.print(Integer.toHexString(vi[i])); 356 Sys.out.print(" "); 357 } 358 Sys.out.println("\n*************************"); 359 } 360 361 static void printHex(float[] vf) { 362 for (int i = 0; i < vf.length; i++) { 363 Sys.out.print(vf[i]); 364 Sys.out.print(" "); 365 } 366 Sys.out.println(); 367 368 for (int i = 0; i < vf.length; i++) { 369 Sys.out.print(Integer.toHexString(Float.floatToIntBits(vf[i]))); 370 Sys.out.print(" "); 371 } 372 Sys.out.println("\n*************************"); 373 } 374 375 static void printHex(double[] vd) { 376 for (int i = 0; i < vd.length; i++) { 377 Sys.out.print(vd[i]); 378 Sys.out.print(" "); 379 } 380 Sys.out.println(); 381 for (int i = 0; i < vd.length; i++) { 382 Sys.out.print(Long.toHexString(Double.doubleToLongBits(vd[i]))); 383 Sys.out.print(" "); 384 } 385 Sys.out.println("\n*************************"); 386 } 387 388 static void printHex(short[] vs) { 389 for (int i = 0; i < vs.length; i++) { 390 Sys.out.print(Integer.toHexString(vs[i] & 0xFFFF)); 391 Sys.out.print(" "); 392 } 393 Sys.out.println("\n*************************"); 394 } 395 396 static void printHex(char[] vs) { 397 for (int i = 0; i < vs.length; i++) { 398 Sys.out.print(Integer.toHexString(vs[i])); 399 Sys.out.print(" "); 400 } 401 Sys.out.println("\n*************************"); 402 } 403 404 static void printHex(byte[] vb) { 405 for (int i = 0; i < vb.length; i++) { 406 if (vb[i] == 0) { 407 Sys.out.print("00"); 408 } 409 else { 410 Sys.out.print(Integer.toHexString(vb[i] & 0xFF)); 411 } 412 Sys.out.print(" "); 413 } 414 Sys.out.println("\n*************************"); 415 } 416 417 418 public int getMaxCache() { 419 return accessManager.getMaxBufferCount(); 420 } 421 422 public void setMaxCache(int max) { 423 accessManager.setMaxBufferCount(max); 424 } 425 } 426 | Popular Tags |