kitaev@213: /* kitaev@213: * Copyright (c) 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: kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class ByteArrayDataAccess extends DataAccess { kitaev@213: kitaev@213: private final byte[] data; kitaev@213: private final int offset; kitaev@213: private final int length; kitaev@213: private int pos; kitaev@213: kitaev@213: public ByteArrayDataAccess(byte[] data) { kitaev@213: this(data, 0, data.length); kitaev@213: } kitaev@213: kitaev@213: public ByteArrayDataAccess(byte[] data, int offset, int length) { kitaev@213: this.data = data; kitaev@213: this.offset = offset; kitaev@213: this.length = length; kitaev@213: pos = 0; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public byte readByte() throws IOException { kitaev@213: if (pos >= length) { kitaev@213: throw new IOException(); kitaev@213: } kitaev@213: return data[offset + pos++]; kitaev@213: } kitaev@213: @Override kitaev@213: public void readBytes(byte[] buf, int off, int len) throws IOException { kitaev@213: if (len > (this.length - pos)) { kitaev@213: throw new IOException(); kitaev@213: } kitaev@213: System.arraycopy(data, pos, buf, off, len); kitaev@213: pos += len; kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public ByteArrayDataAccess reset() { kitaev@213: pos = 0; kitaev@213: return this; kitaev@213: } kitaev@213: @Override kitaev@213: public int length() { kitaev@213: return length; kitaev@213: } kitaev@213: @Override kitaev@213: public void seek(int offset) { kitaev@213: pos = (int) offset; kitaev@213: } kitaev@213: @Override kitaev@213: public void skip(int bytes) throws IOException { kitaev@213: seek(pos + bytes); kitaev@213: } kitaev@213: @Override kitaev@213: public boolean isEmpty() { kitaev@213: return pos >= length; kitaev@213: } kitaev@213: kitaev@213: // kitaev@213: kitaev@213: // when byte[] needed from DA, we may save few cycles and some memory giving this (otherwise unsafe) access to underlying data kitaev@213: @Override kitaev@213: public byte[] byteArray() { kitaev@213: return data; kitaev@213: } kitaev@213: }