1 11 package org.eclipse.swt.internal.image; 12 13 import java.io.*; 14 15 final class TIFFRandomFileAccess { 16 17 LEDataInputStream inputStream; 18 int start, current, next; 19 byte[][] buffers; 20 21 static final int CHUNK_SIZE = 8192; 22 static final int LIST_SIZE = 128; 23 24 public TIFFRandomFileAccess(LEDataInputStream stream) { 25 inputStream = stream; 26 start = current = next = inputStream.getPosition(); 27 buffers = new byte[LIST_SIZE][]; 28 } 29 30 void seek(int pos) throws IOException { 31 if (pos == current) return; 32 if (pos < start) throw new IOException(); 33 current = pos; 34 if (current > next) { 35 int n = current - next; 36 37 int index = next / CHUNK_SIZE; 38 int offset = next % CHUNK_SIZE; 39 while (n > 0) { 40 if (index >= buffers.length) { 41 byte[][] oldBuffers = buffers; 42 buffers = new byte[Math.max(index + 1, oldBuffers.length + LIST_SIZE)][]; 43 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); 44 } 45 if (buffers[index] == null) buffers[index] = new byte[CHUNK_SIZE]; 46 int cnt = inputStream.read(buffers[index], offset, Math.min(n, CHUNK_SIZE - offset)); 47 n -= cnt; 48 next += cnt; 49 index++; 50 offset = 0; 51 } 52 } 53 } 54 55 void read(byte b[]) throws IOException { 56 int size = b.length; 57 int nCached = Math.min(size, next - current); 58 int nMissing = size - next + current; 59 int destNext = 0; 60 if (nCached > 0) { 61 62 int index = current / CHUNK_SIZE; 63 int offset = current % CHUNK_SIZE; 64 while (nCached > 0) { 65 int cnt = Math.min(nCached, CHUNK_SIZE - offset); 66 System.arraycopy(buffers[index], offset, b, destNext, cnt); 67 nCached -= cnt; 68 destNext += cnt; 69 index++; 70 offset = 0; 71 } 72 } 73 if (nMissing > 0) { 74 75 int index = next / CHUNK_SIZE; 76 int offset = next % CHUNK_SIZE; 77 while (nMissing > 0) { 78 if (index >= buffers.length) { 79 byte[][] oldBuffers = buffers; 80 buffers = new byte[Math.max(index, oldBuffers.length + LIST_SIZE)][]; 81 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); 82 } 83 if (buffers[index] == null) buffers[index] = new byte[CHUNK_SIZE]; 84 int cnt = inputStream.read(buffers[index], offset, Math.min(nMissing, CHUNK_SIZE - offset)); 85 System.arraycopy(buffers[index], offset, b, destNext, cnt); 86 nMissing -= cnt; 87 next += cnt; 88 destNext += cnt; 89 index++; 90 offset = 0; 91 } 92 } 93 current += size; 94 } 95 96 } 97 | Popular Tags |