ImageScience 3.0.0

imagescience.array
Class ByteArray

java.lang.Object
  extended by imagescience.array.ByteArray

public class ByteArray
extends java.lang.Object

A dynamic array of byte values. Provides more flexibility than byte[] objects and more efficiency than java.util.Vector<Byte> objects.


Constructor Summary
ByteArray()
          Default constructor.
ByteArray(byte[] array)
          Array constructor.
ByteArray(int capacity)
          Capacity constructor.
ByteArray(int capacity, int increment)
          Capacity constructor.
 
Method Summary
 void add(byte b)
          Appends the array with the given byte.
 void append(byte b)
          Appends the array with the given byte.
 byte[] array()
          Returns a handle to the internal array.
 int capacity()
          Returns the capacity of the array.
 void clear()
          Removes all elements.
 ByteArray duplicate()
          Duplicates the array.
 boolean empty()
          Indicates whether this array has no elements.
 void ensure(int capacity)
          Ensures that the capacity of the array is at least the given capacity.
 boolean equals(ByteArray array)
          Indicates whether this array contains the same data as the given array.
 byte first()
          Returns the first element of the array.
 byte[] get()
          Returns a handle to the internal array.
 byte get(int index)
          Returns the element at the given index in the array.
 int increment()
          Returns the capacity increment of the array.
 void increment(int increment)
          Sets the capacity increment of the array.
 void insert(byte b, int index)
          Inserts the given byte at the given index in the array.
 byte last()
          Returns the last element of the array.
 int length()
          Returns the number of elements in the array.
 void length(int length)
          Sets the length of the array to the given length.
 void remove(int index)
          Removes the element at the given index from the array.
 void reset()
          Removes all elements.
 void set(byte[] array)
          Sets the internal array to the given array.
 void set(byte b, int index)
          Replaces the element at the given index in the array by the given byte.
 int size()
          Returns the number of elements in the array.
 void size(int length)
          Sets the length of the array to the given length.
 void trim()
          Trims the capacity of the array down to the length of the array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteArray

public ByteArray()
Default constructor. Results in a new array of length 0 but with an initial capacity of 10 elements and a capacity increment of 0.


ByteArray

public ByteArray(int capacity)
Capacity constructor. Results in a new array of length 0 but with given initial capacity and a capacity increment of 0.

Parameters:
capacity - The capacity of the new array.
Throws:
java.lang.IllegalArgumentException - If capacity is less than 0.

ByteArray

public ByteArray(int capacity,
                 int increment)
Capacity constructor. Results in a new array of length 0 but with given initial capacity and capacity increment.

Parameters:
capacity - The capacity of the new array.
increment - The capacity increment of the new array. A value less than or equal to 0 means the capacity of the array is doubled each time it needs to grow.
Throws:
java.lang.IllegalArgumentException - If capacity is less than 0.

ByteArray

public ByteArray(byte[] array)
Array constructor. Results in a new array that uses the given array as initial internal array. The length and capacity of the new array are both set to the length of the given array. The capacity increment is 0.

Parameters:
array - The array initially used as internal array.
Throws:
java.lang.NullPointerException - If array is null.
Method Detail

array

public byte[] array()
Returns a handle to the internal array. The same as method get().

Returns:
A handle to the internal array. The length of the returned array is equal to the value returned by capacity(). By first calling trim(), the length of the returned array will be equal to the actual number of elements in the array, that is the value returned by length().

length

public int length()
Returns the number of elements in the array. The same as method size().

Returns:
The number of elements in the array.

length

public void length(int length)
Sets the length of the array to the given length. The same as method size(int).

Parameters:
length - The new length of the array. If the value of this parameter is less than the current length, the current length is simply set to the given length, without changing the capacity of the array. If it is larger than the current length, the capacity of the array is adjusted as necessary. The capacity increment is retained.
Throws:
java.lang.IllegalArgumentException - If length is less than 0.

size

public int size()
Returns the number of elements in the array. The same as method length().

Returns:
The number of elements in the array.

size

public void size(int length)
Sets the length of the array to the given length. The same as method length(int).

Parameters:
length - The new length of the array. If the value of this parameter is less than the current length, the current length is simply set to the given length, without changing the capacity of the array. If it is larger than the current length, the capacity of the array is adjusted as necessary. The capacity increment is retained.
Throws:
java.lang.IllegalArgumentException - If length is less than 0.

empty

public boolean empty()
Indicates whether this array has no elements.

Returns:
Value true if the array has no elements, that is if the length of the array is 0, or false otherwise.

capacity

public int capacity()
Returns the capacity of the array.

Returns:
The capacity of the array.

increment

public int increment()
Returns the capacity increment of the array.

Returns:
The capacity increment of the array.

increment

public void increment(int increment)
Sets the capacity increment of the array.

Parameters:
increment - The new capacity increment of the array. A value less than or equal to 0 means the capacity of the array is doubled each time it needs to grow.

get

public byte[] get()
Returns a handle to the internal array. The same as method array().

Returns:
A handle to the internal array. The length of the returned array is equal to the value returned by capacity(). By first calling trim(), the length of the returned array will be equal to the actual number of elements in the array, that is the value returned by length().

get

public byte get(int index)
Returns the element at the given index in the array.

Parameters:
index - The index.
Returns:
The element at the given index in the array.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If index is less than 0 or larger than or equal to the length of the array.

first

public byte first()
Returns the first element of the array.

Returns:
The first element of the array.
Throws:
java.util.NoSuchElementException - If the length of the array is 0.

last

public byte last()
Returns the last element of the array.

Returns:
The last element of the array.
Throws:
java.util.NoSuchElementException - If the length of the array is 0.

add

public void add(byte b)
Appends the array with the given byte. The same as method append(byte).

Parameters:
b - The byte to be appended to the array.

append

public void append(byte b)
Appends the array with the given byte. The same as method add(byte).

Parameters:
b - The byte to be appended to the array.

insert

public void insert(byte b,
                   int index)
Inserts the given byte at the given index in the array.

Parameters:
b - The byte to be inserted in the array.
index - The index at which b is inserted. The indices of the elements originally at this index and higher are increased by 1.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If index is less than 0 or larger than or equal to the length of the array.

set

public void set(byte b,
                int index)
Replaces the element at the given index in the array by the given byte.

Parameters:
b - The byte to be placed in the array.
index - The index at which b is to be placed.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If index is less than 0 or larger than or equal to the length of the array.

set

public void set(byte[] array)
Sets the internal array to the given array.

Parameters:
array - The array to which the internal array is to be set. The length and capacity of the array are both set to the length of the given array. The capacity increment is retained.
Throws:
java.lang.NullPointerException - If array is null.

reset

public void reset()
Removes all elements. The same as method clear(). The length of the array is set to 0 but the capacity and capacity increment are retained.


clear

public void clear()
Removes all elements. The same as method reset(). The length of the array is set to 0 but the capacity and capacity increment are retained.


trim

public void trim()
Trims the capacity of the array down to the length of the array.


remove

public void remove(int index)
Removes the element at the given index from the array.

Parameters:
index - The index whose element is to be removed from the array. The indices of the elements at the next index and higher are decreased by 1.
Throws:
java.lang.ArrayIndexOutOfBoundsException - If index is less than 0 or larger than or equal to the length of the array.

duplicate

public ByteArray duplicate()
Duplicates the array.

Returns:
A new ByteArray object that is an exact copy of this object. All information is copied and no memory is shared between this and the returned object.

ensure

public void ensure(int capacity)
Ensures that the capacity of the array is at least the given capacity.

Parameters:
capacity - The minimum capacity that the array is ensured to have.

equals

public boolean equals(ByteArray array)
Indicates whether this array contains the same data as the given array.

Parameters:
array - The array to compare this array with.
Returns:
Value true if array is not null, has the same length as this array, and each element of array has the exact same value as the corresponding element of this array, or false if this is not the case.

ImageScience 3.0.0

Copyright (C) Erik Meijering. Permission to use this software and corresponding documentation for educational, research, and not-for-profit purposes, without a fee and without a signed licensing agreement, is granted, subject to the following terms and conditions.

IT IS NOT ALLOWED TO REDISTRIBUTE, SELL, OR LEASE THIS SOFTWARE, OR DERIVATIVE WORKS THEREOF, WITHOUT PERMISSION IN WRITING FROM THE COPYRIGHT HOLDER. THE COPYRIGHT HOLDER IS FREE TO MAKE VERSIONS OF THE SOFTWARE AVAILABLE FOR A FEE OR COMMERCIALLY ONLY.

IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OF ANY KIND WHATSOEVER, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.

THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE EXPRESS OR IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND CORRESPONDING DOCUMENTATION IS PROVIDED "AS IS". THE COPYRIGHT HOLDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.