25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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.PngInputStream;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.io.InputStream;
|
|
18 import org.eclipse.swt.internal.image.PngIdatChunk;
|
|
19 import org.eclipse.swt.internal.image.PngChunkReader;
|
|
20 import org.eclipse.swt.internal.image.PngChunk;
|
|
21
|
|
22
|
|
23 public class PngInputStream : InputStream {
|
|
24
|
|
25 alias InputStream.read read;
|
|
26
|
|
27 PngChunkReader reader;
|
|
28 PngChunk chunk;
|
|
29 int offset, length;
|
|
30
|
|
31 final static int DATA_OFFSET = 8;
|
|
32
|
|
33 public this(PngIdatChunk chunk, PngChunkReader reader) {
|
|
34 this.chunk = chunk;
|
|
35 this.reader = reader;
|
|
36 length = chunk.getLength();
|
|
37 offset = 0;
|
|
38 }
|
|
39
|
|
40 private bool checkChunk() {
|
|
41 while (offset is length) {
|
|
42 chunk = reader.readNextChunk();
|
|
43 if (chunk is null) throw new IOException("no data");
|
|
44 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false;
|
|
45 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException("");
|
|
46 length = chunk.getLength();
|
|
47 offset = 0;
|
|
48 }
|
|
49 return true;
|
|
50 }
|
|
51
|
|
52 public override void close() {
|
|
53 chunk = null;
|
|
54 }
|
|
55
|
|
56 public override int read() {
|
|
57 if (chunk is null) throw new IOException("");
|
|
58 if (offset is length && !checkChunk()) return -1;
|
|
59 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF;
|
|
60 offset++;
|
|
61 return b;
|
|
62 }
|
|
63
|
|
64 public override int read(byte[] b, int off, int len) {
|
|
65 if (chunk is null) throw new IOException("");
|
|
66 if (offset is length && !checkChunk()) return -1;
|
|
67 len = Math.min(len, length - offset);
|
|
68 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len);
|
|
69 offset += len;
|
|
70 return len;
|
|
71 }
|
|
72 }
|