All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.sbtqa.monte.media.io.UncachedImageInputStream Maven / Gradle / Ivy

There is a newer version: 1.1.0-JAVA7
Show newest version
/* @(#)UncachedImageInputStream.java
 * Copyright © 2011 Werner Randelshofer, Switzerland. 
 * You may only use this software in accordance with the license terms.
 */
package ru.sbtqa.monte.media.io;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
import static java.nio.ByteOrder.BIG_ENDIAN;

/**
 * An implementation of {@code ImageInputStream} that gets its input from a
 * regular {@code InputStream}. No caching is used and thus backward seeking is
 * not supported.
 *
 * @author Werner Randelshofer
 * @version $Id: UncachedImageInputStream.java 364 2016-11-09 19:54:25Z werner $
 */
public class UncachedImageInputStream extends ImageInputStreamImpl2 {

    private InputStream in;

    public UncachedImageInputStream(InputStream in) {
        this(in, BIG_ENDIAN);
    }

    public UncachedImageInputStream(InputStream in, ByteOrder bo) {
        this.in = in;
        this.byteOrder = bo;
    }

    @Override
    public int read() throws IOException {
        int b = in.read();
        if (b >= 0) {
            streamPos++;
        }
        return b;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int count = in.read(b, off, len);
        if (count > 0) {
            streamPos += count;
        }
        return count;
    }

    @Override
    public void seek(long pos) throws IOException {
        checkClosed();

        // This test also covers pos < 0
        if (pos < flushedPos) {
            throw new IndexOutOfBoundsException("pos < flushedPos!");
        }
        if (pos < streamPos) {
            throw new IndexOutOfBoundsException("pos < streamPos!");
        }

        this.bitOffset = 0;

        while (streamPos < pos) {
            long skipped = in.skip(pos - streamPos);
            if (skipped < 0) {
                throw new EOFException("EOF reached while trying to seek to " + pos);
            }
            streamPos += skipped;
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy