Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/internal/image/TIFFRandomFileAccess.d @ 49:7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 12:59:54 +0100 |
parents | ddbfe84d86df |
children | 536e43f63c81 |
rev | line source |
---|---|
25 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.internal.image.TIFFRandomFileAccess; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.swt.internal.image.LEDataInputStream; | |
18 | |
19 final class TIFFRandomFileAccess { | |
20 | |
21 LEDataInputStream inputStream; | |
22 int start, current, next; | |
23 byte[][] buffers; | |
24 | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
25 static const int CHUNK_SIZE = 8192; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
26 static const int LIST_SIZE = 128; |
25 | 27 |
28 public this(LEDataInputStream stream) { | |
29 inputStream = stream; | |
30 start = current = next = inputStream.getPosition(); | |
31 buffers = new byte[][](LIST_SIZE); | |
32 } | |
33 | |
34 void seek(int pos) { | |
35 if (pos is current) return; | |
36 if (pos < start) throw new IOException( "pos < start" ); | |
37 current = pos; | |
38 if (current > next) { | |
39 int n = current - next; | |
40 /* store required bytes */ | |
41 int index = next / CHUNK_SIZE; | |
42 int offset = next % CHUNK_SIZE; | |
43 while (n > 0) { | |
44 if (index >= buffers.length) { | |
45 byte[][] oldBuffers = buffers; | |
46 buffers = new byte[][]( Math.max(index + 1, oldBuffers.length + LIST_SIZE) ); | |
47 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); | |
48 } | |
49 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE]; | |
50 int cnt = inputStream.read(buffers[index], offset, Math.min(n, CHUNK_SIZE - offset)); | |
51 n -= cnt; | |
52 next += cnt; | |
53 index++; | |
54 offset = 0; | |
55 } | |
56 } | |
57 } | |
58 | |
59 void read(byte b[]) { | |
60 int size = b.length; | |
61 int nCached = Math.min(size, next - current); | |
62 int nMissing = size - next + current; | |
63 int destNext = 0; | |
64 if (nCached > 0) { | |
65 /* Get cached bytes */ | |
66 int index = current / CHUNK_SIZE; | |
67 int offset = current % CHUNK_SIZE; | |
68 while (nCached > 0) { | |
69 int cnt = Math.min(nCached, CHUNK_SIZE - offset); | |
70 System.arraycopy(buffers[index], offset, b, destNext, cnt); | |
71 nCached -= cnt; | |
72 destNext += cnt; | |
73 index++; | |
74 offset = 0; | |
75 } | |
76 } | |
77 if (nMissing > 0) { | |
78 /* Read required bytes */ | |
79 int index = next / CHUNK_SIZE; | |
80 int offset = next % CHUNK_SIZE; | |
81 while (nMissing > 0) { | |
82 if (index >= buffers.length) { | |
83 byte[][] oldBuffers = buffers; | |
84 buffers = new byte[][](Math.max(index, oldBuffers.length + LIST_SIZE)); | |
85 System.arraycopy(oldBuffers, 0, buffers, 0, oldBuffers.length); | |
86 } | |
87 if (buffers[index] is null) buffers[index] = new byte[CHUNK_SIZE]; | |
88 int cnt = inputStream.read(buffers[index], offset, Math.min(nMissing, CHUNK_SIZE - offset)); | |
89 System.arraycopy(buffers[index], offset, b, destNext, cnt); | |
90 nMissing -= cnt; | |
91 next += cnt; | |
92 destNext += cnt; | |
93 index++; | |
94 offset = 0; | |
95 } | |
96 } | |
97 current += size; | |
98 } | |
99 | |
100 } |