comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/image/PngInputStream.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children 6bf2837c50fe
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
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.io.InputStream;
16 import java.lang.System;
17 import org.eclipse.swt.internal.image.PngIdatChunk;
18 import org.eclipse.swt.internal.image.PngChunkReader;
19 import org.eclipse.swt.internal.image.PngChunk;
20
21 import tango.core.Exception;
22 import Math = tango.math.Math;
23
24 public class PngInputStream : InputStream {
25
26 alias InputStream.read read;
27
28 PngChunkReader reader;
29 PngChunk chunk;
30 int offset, length;
31
32 final static int DATA_OFFSET = 8;
33
34 public this(PngIdatChunk chunk, PngChunkReader reader) {
35 this.chunk = chunk;
36 this.reader = reader;
37 length = chunk.getLength();
38 offset = 0;
39 }
40
41 private bool checkChunk() {
42 while (offset is length) {
43 chunk = reader.readNextChunk();
44 if (chunk is null) throw new IOException("no data");
45 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false;
46 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException("");
47 length = chunk.getLength();
48 offset = 0;
49 }
50 return true;
51 }
52
53 public override void close() {
54 chunk = null;
55 }
56
57 public override int read() {
58 if (chunk is null) throw new IOException("");
59 if (offset is length && !checkChunk()) return -1;
60 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF;
61 offset++;
62 return b;
63 }
64
65 public override int read(byte[] b, int off, int len) {
66 if (chunk is null) throw new IOException("");
67 if (offset is length && !checkChunk()) return -1;
68 len = Math.min(len, length - offset);
69 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len);
70 offset += len;
71 return len;
72 }
73 }