package RwsReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

public class FileReader {

	private RandomAccessFile in;

	public FileReader(File file) throws FileNotFoundException {
		in = new RandomAccessFile(file, "r");
	}

	int readInt() throws IOException {
		return (
			in.readUnsignedByte() |
			in.readUnsignedByte() << 8 |
			in.readUnsignedByte() << 16 |
			in.readUnsignedByte() << 24
		);
	}

	int readShort() throws IOException {
		return  (
			in.readUnsignedByte() |
			in.readUnsignedByte() << 8
		);
	}

	float readFloat() throws IOException {
		byte buf[] = new byte[4];
		for (int i=3; i>=0; i--) {
			buf[i] = in.readByte();
		}
		ByteBuffer buffer = ByteBuffer.wrap(buf);
		return buffer.getFloat();
	}

	String readString(int length) throws IOException {
		String s = "";
		while (s.length() < length) {
			int b = in.readUnsignedByte();
			if (b == 0) {
				in.skipBytes(length - s.length() - 1);
				return s;
			}
			s += (char) b;
		}
		return s;
	}

	public void skipBytes(int numBytes) throws IOException {
		in.skipBytes(numBytes);
	}

	public long getFilePointer() throws IOException {
		return in.getFilePointer();
	}

	public void seek(long pos) throws IOException {
		in.seek(pos);
	}

	public void close() throws IOException {
		in.close();
	}

	public long length() throws IOException {
		return in.length();
	}
}
