Skip to content
Snippets Groups Projects
ByteReader.java 3.84 KiB
Newer Older
Dmitry Shamrikov's avatar
Dmitry Shamrikov committed
package io;

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

public class ByteReader extends InputStream {
	
	private InputStream stream;
	private Endianness end = Endianness.LITTLE_ENDIAN;
Dmitry Shamrikov's avatar
Dmitry Shamrikov committed
	private int pos = 0;
	
	
	public ByteReader(InputStream in) {
		stream = in;
	}
	
	public ByteReader setBigEndian() {
		end = Endianness.BIG_ENDIAN;
		return this;
	}
	
	public ByteReader setLittleEndian() {
		end = Endianness.LITTLE_ENDIAN;
		return this;
	}
	
	public ByteReader changeEndiannes() {
		if (isLitteEndian())
			setBigEndian();
		else 
			setLittleEndian();
		return this;
	}
	
	public boolean isBigEndian() {
		return end == Endianness.BIG_ENDIAN;
	}
	
	public boolean isLitteEndian() {
		return end == Endianness.LITTLE_ENDIAN;
	}
	
Dmitry Shamrikov's avatar
Dmitry Shamrikov committed
	public int getPosition() {
		return pos;
	}
	
	public boolean seek(int n) throws IOException {
		if (n == 0) return false;
		for (int i=0; i<n; i++)
			read();		
		return true;
	}
	
	@Override
	public int read() throws IOException {
Dmitry Shamrikov's avatar
Dmitry Shamrikov committed
		pos++;
		return stream.read();
	}
	
	public long read2bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8);
		else 
			return b1 | (b0 << 8);
	}
	
	public long read3bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16);
		else 
			return b2 | (b1 << 8) | (b0 << 16);
	}
	
	public long read4bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		long b3 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
		else 
			return b3 | (b2 << 8) | (b1 << 16) | (b0 << 24);
	}
	
	public long read5bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		long b3 = read();
		long b4 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32);
		else 
			return b4 | (b3 << 8) | (b2 << 16) | (b1 << 24) | (b0 << 32);
	}
	
	public long read6bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		long b3 = read();
		long b4 = read();
		long b5 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40);
		else 
			return b5 | (b4 << 8) | (b3 << 16) | (b2 << 24) | (b1 << 32) | (b0 << 40);
	}
	
	public long read7bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		long b3 = read();
		long b4 = read();
		long b5 = read();
		long b6 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48);
		else 
			return b6 | (b5 << 8) | (b4 << 16) | (b3 << 24) | (b2 << 32) | (b1 << 40) | (b0 << 48);
	}
	
	public long read8bytes() throws IOException {
		long b0 = read();
		long b1 = read();
		long b2 = read();
		long b3 = read();
		long b4 = read();
		long b5 = read();
		long b6 = read();
		long b7 = read();
		if (isLitteEndian())
			return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48) | (b7 << 56);
		else 
			return b7 | (b6 << 8) | (b5 << 16) | (b4 << 24) | (b3 << 32) | (b2 << 40) | (b1 << 48) | (b0 << 56);
	}
	
	public String readString(int start, int size) throws IOException {
		int sz = size + start - getPosition();
		if (sz == 0) return null;
		byte[] buf = new byte[sz];
		int i = 0;
		for (;; i++) {
			int b = read();
			if (b == 0) break;
			buf[i] = (byte)b;
		}
		if (i == 0) return null;
		return new String(buf, 0, i);
	}
	
	public String readUnicodeString(int start, int size) throws IOException {
		int sz = (size + start - getPosition())>>1;
		if (sz == 0) return null;
		char[] buf = new char[sz];		
		int i = 0;
		for (;; i++) {
			char c = (char)read2bytes();
			if (c == 0) break;
			buf[i] = c;
		}
		if (i == 0) return null;
		return new String(buf, 0, i);
	}
}

enum Endianness {
	BIG_ENDIAN, LITTLE_ENDIAN
}