/* * (c) Copyright 2009 Talis Information Ltd * All rights reserved. * [See end of file] */ package migrate.lib; import java.util.Iterator ; import java.util.NoSuchElementException ; import org.openjena.atlas.AtlasException ; /** An expanding byte array. * Like a byte[] but it can grow. */ public final class ByteArray //implements java.lang.Iterable { private int length ; private byte[] bytes ; /** Allocate size bytes of active space */ public ByteArray(int size) { this(size, size) ; } /** Allocate empty array (may have a few bytes for expansion) */ public ByteArray() { this(0, 8) ; } /** Allocate an array with a hint to sufficient space for growth */ public ByteArray(int size, int initialAllocation) { bytes = new byte[initialAllocation] ; length = size ; } /** Return a byte */ public byte get(int idx) { check(idx) ; return bytes[idx] ; } /** Set a byte */ public void set(int idx, byte b) { if ( idx == length) { add(b) ; return ; } check(idx) ; bytes[idx] = b ; } /** add a byte to the end of the array */ public void add(byte b) { if ( length == bytes.length ) realloc(8) ; bytes[length] = b ; length++ ; } /** add bytes to the end of the array */ public void add(byte[] b) { add(b, 0, b.length) ; } /** add bytes to the end of the array */ public void add(byte[] b, int start, int len) { if ( length+len >= bytes.length ) realloc(len) ; System.arraycopy(bytes, length, b, start, len) ; } /** add copy ByteArray bytes to the end of the array */ public void add(ByteArray b) { add(b.bytes, 0, b.length) ; } private void realloc(int minGrowth) { int currLen = bytes.length ; int newLen = currLen+currLen/2+1 ; if ( newLen-currLen < minGrowth ) newLen = currLen + minGrowth ; byte[] bytes2 = new byte[newLen] ; System.arraycopy(bytes, 0, bytes2, 0, currLen) ; // length still the same. } /** Current length */ public int length() { return length ; } /** Current max size without reallocation */ public int currentAlloc() { return bytes.length ; } public void ensure(int size) { if ( size < bytes.length ) realloc(bytes.length-size) ; } private final void check(int idx) { if ( idx < 0 || idx > length) throw new AtlasException(String.format("Out of bounds %d [0, %d]", idx, length)) ; } // Don't encourage iterators - boxing overhead. //@Override public Iterator iterator() { return new IteratorByteArray(bytes, 0, length) ; } static class IteratorByteArray implements Iterator { private int idx ; private byte[] bytes ; private int finishIdx ; public IteratorByteArray(byte[] bytes, int start, int finish) { if ( start < 0 ) throw new IllegalArgumentException("Start: "+start) ; if ( start > finish ) throw new IllegalArgumentException("Start >= finish: "+start+" >= "+finish) ; // Instead: truncate to array length // if ( finish > array.length ) // throw new IllegalArgumentException("Finish outside array") ; // // Instead: immediate end iterator // if ( start >= array.length ) // throw new IllegalArgumentException("Start outside array") ; this.bytes = bytes ; idx = start ; finishIdx = finish ; if ( idx < 0 ) idx = 0 ; if ( finishIdx > bytes.length ) finishIdx = bytes.length ; } //@Override @Override public boolean hasNext() { // if ( idx < 0 ) // return false ; if ( idx >= finishIdx ) return false ; return true ; } public byte current() { if ( idx >= finishIdx ) throw new NoSuchElementException() ; return bytes[idx] ; } //@Override @Override public Byte next() { if ( ! hasNext() ) throw new NoSuchElementException() ; return bytes[idx++] ; } //@Override @Override public void remove() { throw new UnsupportedOperationException("IterBytes") ; } } } /* * (c) Copyright 2009 Talis Information Ltd * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */