tikhomirov@10: /*
tikhomirov@397:  * Copyright (c) 2010-2012 TMate Software Ltd
tikhomirov@74:  *  
tikhomirov@74:  * This program is free software; you can redistribute it and/or modify
tikhomirov@74:  * it under the terms of the GNU General Public License as published by
tikhomirov@74:  * the Free Software Foundation; version 2 of the License.
tikhomirov@74:  *
tikhomirov@74:  * This program is distributed in the hope that it will be useful,
tikhomirov@74:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@74:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
tikhomirov@74:  * GNU General Public License for more details.
tikhomirov@74:  *
tikhomirov@74:  * For information on how to redistribute this software under
tikhomirov@74:  * the terms of a license other than GNU General Public License
tikhomirov@102:  * contact TMate Software at support@hg4j.com
tikhomirov@10:  */
tikhomirov@74: package org.tmatesoft.hg.internal;
tikhomirov@10: 
tikhomirov@10: import java.io.IOException;
tikhomirov@157: import java.nio.ByteBuffer;
tikhomirov@10: 
tikhomirov@10: /**
tikhomirov@10:  * relevant parts of DataInput, non-stream nature (seek operation), explicit check for end of data.
tikhomirov@10:  * convenient skip (+/- bytes)
tikhomirov@10:  * Primary goal - effective file read, so that clients don't need to care whether to call few 
tikhomirov@10:  * distinct getInt() or readBytes(totalForFewInts) and parse themselves instead in an attempt to optimize.  
tikhomirov@74:  * 
tikhomirov@74:  * @author Artem Tikhomirov
tikhomirov@74:  * @author TMate Software Ltd.
tikhomirov@10:  */
tikhomirov@10: public class DataAccess {
tikhomirov@10: 	public boolean isEmpty() {
tikhomirov@10: 		return true;
tikhomirov@10: 	}
tikhomirov@399: 	// TODO throws IOException (few subclasses have non-trivial length() operation)
tikhomirov@158: 	public int length() {
tikhomirov@51: 		return 0;
tikhomirov@51: 	}
tikhomirov@157: 	/**
tikhomirov@157: 	 * get this instance into initial state
tikhomirov@157: 	 * @throws IOException
tikhomirov@157: 	 * @return <code>this</code> for convenience
tikhomirov@157: 	 */
tikhomirov@157: 	public DataAccess reset() throws IOException {
tikhomirov@51: 		// nop, empty instance is always in the initial state
tikhomirov@157: 		return this;
tikhomirov@51: 	}
tikhomirov@10: 	// absolute positioning
tikhomirov@158: 	public void seek(int offset) throws IOException {
tikhomirov@397: 		if (offset == 0) {
tikhomirov@397: 			// perfectly OK for the "empty slice" instance
tikhomirov@397: 			return;
tikhomirov@397: 		}
tikhomirov@397: 		throw new IOException(String.format("No data, can't seek %d bytes", offset));
tikhomirov@10: 	}
tikhomirov@10: 	// relative positioning
tikhomirov@10: 	public void skip(int bytes) throws IOException {
tikhomirov@397: 		if (bytes == 0) {
tikhomirov@397: 			return;
tikhomirov@397: 		}
tikhomirov@397: 		throw new IOException(String.format("No data, can't skip %d bytes", bytes));
tikhomirov@10: 	}
tikhomirov@10: 	// shall be called once this object no longer needed
tikhomirov@10: 	public void done() {
tikhomirov@10: 		// no-op in this empty implementation
tikhomirov@10: 	}
tikhomirov@10: 	public int readInt() throws IOException {
tikhomirov@10: 		byte[] b = new byte[4];
tikhomirov@10: 		readBytes(b, 0, 4);
tikhomirov@10: 		return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
tikhomirov@10: 	}
tikhomirov@399: 
tikhomirov@399: 	/**
tikhomirov@399: 	 * Read 8 bytes as long value, big-endian.
tikhomirov@399: 	 */
tikhomirov@10: 	public long readLong() throws IOException {
tikhomirov@10: 		byte[] b = new byte[8];
tikhomirov@10: 		readBytes(b, 0, 8);
tikhomirov@10: 		int i1 = b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
tikhomirov@10: 		int i2 = b[4] << 24 | (b[5] & 0xFF) << 16 | (b[6] & 0xFF) << 8 | (b[7] & 0xFF);
tikhomirov@122: 		return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl);
tikhomirov@10: 	}
tikhomirov@10: 	public void readBytes(byte[] buf, int offset, int length) throws IOException {
tikhomirov@397: 		if (length == 0) {
tikhomirov@397: 			return;
tikhomirov@397: 		}
tikhomirov@397: 		throw new IOException(String.format("No data, can't read %d bytes", length));
tikhomirov@10: 	}
tikhomirov@157: 	// reads bytes into ByteBuffer, up to its limit or total data length, whichever smaller
tikhomirov@157: 	// FIXME perhaps, in DataAccess paradigm (when we read known number of bytes, we shall pass specific byte count to read) 
tikhomirov@157: 	public void readBytes(ByteBuffer buf) throws IOException {
tikhomirov@157: //		int toRead = Math.min(buf.remaining(), (int) length());
tikhomirov@157: //		if (buf.hasArray()) {
tikhomirov@157: //			readBytes(buf.array(), buf.arrayOffset(), toRead);
tikhomirov@157: //		} else {
tikhomirov@157: //			byte[] bb = new byte[toRead];
tikhomirov@157: //			readBytes(bb, 0, bb.length);
tikhomirov@157: //			buf.put(bb);
tikhomirov@157: //		}
tikhomirov@157: 		// FIXME optimize to read as much as possible at once
tikhomirov@157: 		while (!isEmpty() && buf.hasRemaining()) {
tikhomirov@157: 			buf.put(readByte());
tikhomirov@157: 		}
tikhomirov@157: 	}
tikhomirov@10: 	public byte readByte() throws IOException {
tikhomirov@10: 		throw new UnsupportedOperationException();
tikhomirov@10: 	}
tikhomirov@51: 
tikhomirov@51: 	// XXX decide whether may or may not change position in the DataAccess
tikhomirov@51: 	// FIXME exception handling is not right, just for the sake of quick test
tikhomirov@157: 	public byte[] byteArray() throws IOException {
tikhomirov@157: 		reset();
tikhomirov@158: 		byte[] rv = new byte[length()];
tikhomirov@157: 		readBytes(rv, 0, rv.length);
tikhomirov@51: 		return rv;
tikhomirov@51: 	}
tikhomirov@157: }