01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.swt.internal.image;
11:
12: import java.io.*;
13:
14: public class PngInputStream extends InputStream {
15: PngChunkReader reader;
16: PngChunk chunk;
17: int offset, length;
18:
19: final static int DATA_OFFSET = 8;
20:
21: public PngInputStream(PngIdatChunk chunk, PngChunkReader reader) {
22: this .chunk = chunk;
23: this .reader = reader;
24: length = chunk.getLength();
25: offset = 0;
26: }
27:
28: private boolean checkChunk() throws IOException {
29: while (offset == length) {
30: chunk = reader.readNextChunk();
31: if (chunk == null)
32: throw new IOException();
33: if (chunk.getChunkType() == PngChunk.CHUNK_IEND)
34: return false;
35: if (chunk.getChunkType() != PngChunk.CHUNK_IDAT)
36: throw new IOException();
37: length = chunk.getLength();
38: offset = 0;
39: }
40: return true;
41: }
42:
43: public void close() throws IOException {
44: chunk = null;
45: }
46:
47: public int read() throws IOException {
48: if (chunk == null)
49: throw new IOException();
50: if (offset == length && !checkChunk())
51: return -1;
52: int b = chunk.reference[DATA_OFFSET + offset] & 0xFF;
53: offset++;
54: return b;
55: }
56:
57: public int read(byte[] b, int off, int len) throws IOException {
58: if (chunk == null)
59: throw new IOException();
60: if (offset == length && !checkChunk())
61: return -1;
62: len = Math.min(len, length - offset);
63: System.arraycopy(chunk.reference, DATA_OFFSET + offset, b, off,
64: len);
65: offset += len;
66: return len;
67: }
68: }
|