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