comparison dwt/internal/image/PngInputStream.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 *******************************************************************************/
11 module dwt.internal.image;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15
16 public class PngInputStream : InputStream {
17 PngChunkReader reader;
18 PngChunk chunk;
19 int offset, length;
20
21 final static int DATA_OFFSET = 8;
22
23 public PngInputStream(PngIdatChunk chunk, PngChunkReader reader) {
24 this.chunk = chunk;
25 this.reader = reader;
26 length = chunk.getLength();
27 offset = 0;
28 }
29
30 private bool checkChunk() throws IOException {
31 while (offset is length) {
32 chunk = reader.readNextChunk();
33 if (chunk is null) throw new IOException();
34 if (chunk.getChunkType() is PngChunk.CHUNK_IEND) return false;
35 if (chunk.getChunkType() !is PngChunk.CHUNK_IDAT) throw new IOException();
36 length = chunk.getLength();
37 offset = 0;
38 }
39 return true;
40 }
41
42 public void close() throws IOException {
43 chunk = null;
44 }
45
46 public int read() throws IOException {
47 if (chunk is null) throw new IOException();
48 if (offset is length && !checkChunk()) return -1;
49 int b = chunk.reference[DATA_OFFSET + offset] & 0xFF;
50 offset++;
51 return b;
52 }
53
54 public int read(byte[] b, int off, int len) throws IOException {
55 if (chunk is null) throw new IOException();
56 if (offset is length && !checkChunk()) return -1;
57 len = Math.min(len, length - offset);
58 System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off, len);
59 offset += len;
60 return len;
61 }
62 }