comparison dwt/internal/image/TIFFRandomFileAccess.d @ 13:3d9bbe0a83a0

FileFormats
author Frank Benoit <benoit@tionex.de>
date Sun, 06 Jan 2008 22:54:14 +0100
parents
children fc2b263b8a3f
comparison
equal deleted inserted replaced
12:0c78fa47d476 13:3d9bbe0a83a0
1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.internal.image.TIFFRandomFileAccess;
12
13 import dwt.internal.image.LEDataInputStream;
14 import Math = tango.math.Math;
15 import tango.core.Exception;
16
17 final class TIFFRandomFileAccess {
18
19 LEDataInputStream inputStream;
20 int start, current, next;
21 byte[][] buffers;
22
23 static final int CHUNK_SIZE = 8192;
24 static final int LIST_SIZE = 128;
25
26 public this(LEDataInputStream stream) {
27 inputStream = stream;
28 start = current = next = inputStream.getPosition();
29 buffers = new byte[][](LIST_SIZE);
30 }
31
32 void seek(int pos) {
33 if (pos == current) return;
34 if (pos < start) throw new IOException( "pos < start" );
35 current = pos;
36 if (current > next) {
37 int n = current - next;
38 /* store required bytes */
39 int index = next / CHUNK_SIZE;
40 int offset = next % CHUNK_SIZE;
41 while (n > 0) {
42 if (index >= buffers.length) {
43 byte[][] oldBuffers = buffers;
44 buffers = new byte[][]( Math.max(index + 1, oldBuffers.length + LIST_SIZE) );
45 buffers[ 0 .. oldBuffers.length ] = oldBuffers[];
46 }
47 if (buffers[index] == null) buffers[index] = new byte[CHUNK_SIZE];
48 int cnt = inputStream.read(buffers[index], offset, Math.min(n, CHUNK_SIZE - offset));
49 n -= cnt;
50 next += cnt;
51 index++;
52 offset = 0;
53 }
54 }
55 }
56
57 void read(byte b[]) {
58 int size = b.length;
59 int nCached = Math.min(size, next - current);
60 int nMissing = size - next + current;
61 int destNext = 0;
62 if (nCached > 0) {
63 /* Get cached bytes */
64 int index = current / CHUNK_SIZE;
65 int offset = current % CHUNK_SIZE;
66 while (nCached > 0) {
67 int cnt = Math.min(nCached, CHUNK_SIZE - offset);
68 b[ destNext .. destNext+cnt ] = buffers[index][ offset .. offset+cnt ];
69 nCached -= cnt;
70 destNext += cnt;
71 index++;
72 offset = 0;
73 }
74 }
75 if (nMissing > 0) {
76 /* Read required bytes */
77 int index = next / CHUNK_SIZE;
78 int offset = next % CHUNK_SIZE;
79 while (nMissing > 0) {
80 if (index >= buffers.length) {
81 byte[][] oldBuffers = buffers;
82 buffers = new byte[][](Math.max(index, oldBuffers.length + LIST_SIZE));
83 buffers[ 0 .. oldBuffers.length ] = oldBuffers[];
84 }
85 if (buffers[index] == null) buffers[index] = new byte[CHUNK_SIZE];
86 int cnt = inputStream.read(buffers[index], offset, Math.min(nMissing, CHUNK_SIZE - offset));
87 b[ destNext .. destNext+cnt ] = buffers[index][ offset .. offset+cnt ];
88 nMissing -= cnt;
89 next += cnt;
90 destNext += cnt;
91 index++;
92 offset = 0;
93 }
94 }
95 current += size;
96 }
97
98 }