tikhomirov@51: /* tikhomirov@157: * Copyright (c) 2011 TMate Software Ltd tikhomirov@157: * tikhomirov@157: * This program is free software; you can redistribute it and/or modify tikhomirov@157: * it under the terms of the GNU General Public License as published by tikhomirov@157: * the Free Software Foundation; version 2 of the License. tikhomirov@157: * tikhomirov@157: * This program is distributed in the hope that it will be useful, tikhomirov@157: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@157: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@157: * GNU General Public License for more details. tikhomirov@157: * tikhomirov@157: * For information on how to redistribute this software under tikhomirov@157: * the terms of a license other than GNU General Public License tikhomirov@157: * contact TMate Software at support@hg4j.com tikhomirov@51: */ tikhomirov@157: package org.tmatesoft.hg.internal; tikhomirov@51: tikhomirov@51: import java.io.IOException; tikhomirov@51: tikhomirov@157: tikhomirov@51: /** tikhomirov@157: * tikhomirov@157: * @author Artem Tikhomirov tikhomirov@157: * @author TMate Software Ltd. tikhomirov@51: */ tikhomirov@51: public class ByteArrayDataAccess extends DataAccess { tikhomirov@51: tikhomirov@51: private final byte[] data; tikhomirov@51: private final int offset; tikhomirov@51: private final int length; tikhomirov@51: private int pos; tikhomirov@51: tikhomirov@51: public ByteArrayDataAccess(byte[] data) { tikhomirov@51: this(data, 0, data.length); tikhomirov@51: } tikhomirov@51: tikhomirov@51: public ByteArrayDataAccess(byte[] data, int offset, int length) { tikhomirov@51: this.data = data; tikhomirov@51: this.offset = offset; tikhomirov@51: this.length = length; tikhomirov@51: pos = 0; tikhomirov@51: } tikhomirov@51: tikhomirov@51: @Override tikhomirov@51: public byte readByte() throws IOException { tikhomirov@51: if (pos >= length) { tikhomirov@51: throw new IOException(); tikhomirov@51: } tikhomirov@51: return data[offset + pos++]; tikhomirov@51: } tikhomirov@51: @Override tikhomirov@51: public void readBytes(byte[] buf, int off, int len) throws IOException { tikhomirov@51: if (len > (this.length - pos)) { tikhomirov@51: throw new IOException(); tikhomirov@51: } tikhomirov@51: System.arraycopy(data, pos, buf, off, len); tikhomirov@51: pos += len; tikhomirov@51: } tikhomirov@51: tikhomirov@51: @Override tikhomirov@157: public ByteArrayDataAccess reset() { tikhomirov@51: pos = 0; tikhomirov@157: return this; tikhomirov@51: } tikhomirov@51: @Override tikhomirov@51: public long length() { tikhomirov@51: return length; tikhomirov@51: } tikhomirov@51: @Override tikhomirov@51: public void seek(long offset) { tikhomirov@51: pos = (int) offset; tikhomirov@51: } tikhomirov@51: @Override tikhomirov@51: public void skip(int bytes) throws IOException { tikhomirov@51: seek(pos + bytes); tikhomirov@51: } tikhomirov@51: @Override tikhomirov@51: public boolean isEmpty() { tikhomirov@51: return pos >= length; tikhomirov@51: } tikhomirov@51: tikhomirov@51: // tikhomirov@51: tikhomirov@51: // when byte[] needed from DA, we may save few cycles and some memory giving this (otherwise unsafe) access to underlying data tikhomirov@51: @Override tikhomirov@51: public byte[] byteArray() { tikhomirov@51: return data; tikhomirov@51: } tikhomirov@51: }