// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: ByteArray.jlc,v 1.5 2003/09/27 08:10:29 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/lang/Integer.h" #include "jakelib2/util/ByteArray.h" #include using namespace jakelib::lang; using namespace jakelib::util; JAKELIB_IMPLEMENT_CLASS("jakelib.util.ByteArray", ByteArray, Object) /*****************************************************************************\ * ByteArray | *****************************************************************************/ ByteArray::ByteArray(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException(`"Illegal capacity: "` .. initialCapacity .. JAKELIB_AT2("jakelib.util.ByteArray.ByteArray")); elementData = (jbyte*) malloc(initialCapacity * sizeof(jbyte)); elementCount = 0; capacity = initialCapacity; } ByteArray::ByteArray(jbyte* bytes, int size) { elementData = (jbyte*) malloc(size * sizeof(jbyte)); memcpy(elementData, bytes, size * sizeof(jbyte)); elementCount = size; capacity = size; } /*****************************************************************************\ * ~ByteArray | *****************************************************************************/ ByteArray::~ByteArray() { destroy(); } /*****************************************************************************\ * destroy | *****************************************************************************/ void ByteArray::destroy() { elementCount = 0; free(elementData); elementData = null; capacity = 0; } /*****************************************************************************\ * add | *****************************************************************************/ int ByteArray::add(jbyte b) { ensureCapacity(elementCount + 1); elementData[elementCount++] = b; return elementCount - 1; } /*****************************************************************************\ * ensureCapacity | *****************************************************************************/ void ByteArray::ensureCapacity(int minCapacity) { if (minCapacity > capacity) { int newCapacity = capacity * 2; if (newCapacity < minCapacity) newCapacity = minCapacity; jbyte* newElementData = (jbyte*) realloc(elementData, sizeof(jbyte) * newCapacity); if (newElementData == null) throw new MemoryException(`"Allocating "` .. (jlong) (sizeof(Object*) * newCapacity) .. `" bytes of memory"` .. JAKELIB_AT2("jakelib.util.ByteArray.ensureCapacity")); elementData = newElementData; capacity = newCapacity; } } /*****************************************************************************\ * get | *****************************************************************************/ jbyte ByteArray::get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" >= "` .. elementCount .. JAKELIB_AT2("jakelib.util.ByteArray.get")); if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.ByteArray.get")); return elementData[index]; } /*****************************************************************************\ * size | *****************************************************************************/ int ByteArray::size() { return elementCount; } /*****************************************************************************\ * set | *****************************************************************************/ void ByteArray::set(int index, jbyte b) { if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.ByteArray.set")); ensureCapacity(index +1); elementData[index] = b; } /*****************************************************************************\ * insert | *****************************************************************************/ void ByteArray::insert(int index, jbyte b) { if (index < 0) throw new IllegalArgumentException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.ByteArray.insert")); if (index > elementCount) throw new IllegalArgumentException(`""` .. index .. `" > "` .. elementCount .. JAKELIB_AT2("jakelib.util.ByteArray.insert")); ensureCapacity(elementCount +1); for (int idx = elementCount; idx > index; idx--) { elementData[idx] = elementData[idx - 1]; } elementData[index] = b; elementCount ++; } /*****************************************************************************\ * clear | *****************************************************************************/ void ByteArray::clear() { elementCount = 0; } /*****************************************************************************\ * removeLastElement | *****************************************************************************/ void ByteArray::removeLastElement() { if (elementCount == 0) return; elementCount --; } /*****************************************************************************\ * remove | *****************************************************************************/ void ByteArray::remove(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" >= "` .. elementCount .. JAKELIB_AT2("jakelib.util.ByteArray.remove")); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(`""` .. index .. `" < 0"` .. JAKELIB_AT2("jakelib.util.ByteArray.remove")); } int j = elementCount - index - 1; if (j > 0) { //memmove(elementData[index], elementData[index + 1], sizeof(void*) * j); for (int idx = index; idx < elementCount - 1; idx++) elementData[idx] = elementData[idx + 1]; } elementCount--; } /*****************************************************************************\ * isEmpty | *****************************************************************************/ jboolean ByteArray::isEmpty() { return (elementCount == 0); } /*****************************************************************************\ * toString | *****************************************************************************/ String* ByteArray::toString() { StringBuffer buf; buf.append('['); for (int idx = 0; idx < elementCount; idx++) { buf.append(Integer::toHexString(elementData[idx] & 0xff)); if (idx < elementCount -1) buf.append(' '); } buf.append(']'); return buf.toString(); } /*****************************************************************************\ * copyTo | *****************************************************************************/ void ByteArray::copyTo(jbyte* dest) { memcpy(dest, elementData, sizeof(jbyte) * elementCount); } /*****************************************************************************\ * getBytes | *****************************************************************************/ jbyte* ByteArray::getBytes() { return elementData; }