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

org.freehep.util.io.DecodingInputStream Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
// Copyright FreeHEP, 2009
package org.freehep.util.io;

import java.io.IOException;
import java.io.InputStream;

/**
 * Handles read by always throwing IOExceptions even in the middle of a read of
 * an array. IMPORTANT: inherits from InputStream rather than FilterInputStream
 * so that the correct read(byte[], int, int) method is used.
 * 
 * @author Mark Donszelmann ([email protected])
 */
public abstract class DecodingInputStream extends InputStream {

	/**
	 * Overridden to make sure it ALWAYS throws an IOException while a problem
	 * occurs in read().
	 */
	@Override
	public int read(byte b[], int off, int len) throws IOException {
		if (b == null) {
			throw new NullPointerException();
		} else if (off < 0 || len < 0 || len > b.length - off) {
			throw new IndexOutOfBoundsException();
		} else if (len == 0) {
			return 0;
		}

		int c = read();
		if (c == -1) {
			return -1;
		}
		b[off] = (byte) c;

		int i = 1;
		for (; i < len; i++) {
			c = read();
			if (c == -1) {
				break;
			}
			b[off + i] = (byte) c;
		}
		return i;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy