📄 arraybytelist.java
字号:
/**
* Checks if the given index is in range.
*/
private void checkIndex(int index) {
if (index >= size || index < 0)
throw new IndexOutOfBoundsException("index: " + index
+ ", size: " + size);
}
/**
* Checks if the given range is within the contained array's bounds.
*/
private void checkRange(int from, int to) {
if (from < 0 || from > to || to > size)
throw new IndexOutOfBoundsException("from: " + from + ", to: "
+ to + ", size: " + size);
}
/**
* Checks if the given size is within bounds.
*/
private void checkSize(int newSize) {
if (newSize < 0)
throw new IndexOutOfBoundsException("newSize: " + newSize);
}
/**
* Returns the default charset if no charset is specified.
*/
private static Charset getCharset(Charset charset) {
return charset == null ? DEFAULT_CHARSET : charset;
}
/** efficient Serializable support. Example:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("/tmp/test"));
out.writeObject(new ArrayByteList("hello world", null));
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("/tmp/test"));
ArrayByteList list = (ArrayByteList) in.readObject();
in.close();
System.out.println(list.toString(null));
*/
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(elements.length);
out.write(elements, 0, size);
}
/** efficient Serializable support */
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
elements = new byte[in.readInt()];
in.readFully(elements, 0, size);
}
// ****************************************************************************
// following are some methods not deemed important enough or complex enough
// to warrant interface bloat:
// ****************************************************************************
// /**
// * Appends the specified elements to the end of this list.
// * <p>
// * If you need to append <code>elems[from..to)</code>, use
// * <code>list.add(new ArrayByteList(elems).subList(from, to))</code>
// * or <code>list.add(ByteBuffer.wrap(elems, from, to-from))</code>
// * or similar.
// *
// * @param elems
// * elements to be appended.
// * @return <code>this</code> (for chaining convenience only)
// */
// public ArrayByteList add(byte[] elems) {
// replace(size, size, elems);
// return this;
// }
// /**
// * Lexicographically compares this object with the specified other object for order. Returns a
// * negative integer, zero, or a positive integer as this object is less
// * than, equal to, or greater than the specified other object.
// * <p>
// * If a negative integer <code>i</code> is returned, the index of the
// * first differing element is <code>-(i+1)</code>. If a positive integer
// * <code>i</code> is returned, the index of the first differing element is
// * <code>i-1</code>.
// *
// * @param otherObj
// * the Object to be compared.
// * @return a negative integer, zero, or a positive integer as this object is
// * less than, equal to, or greater than the specified other object.
// *
// * @throws ClassCastException
// * if the specified object's type prevents it from being
// * compared to this Object.
// * @see Comparable
// * @see String#compareTo(String)
// */
// public int compareTo(Object otherObj) {
// if (this == otherObj) return 0;
// if (otherObj == null) throw new NullPointerException();
// if (!(otherObj instanceof ArrayByteList)) throw new ClassCastException();
// ArrayByteList other = (ArrayByteList) otherObj;
//
// int minSize = Math.min(size, other.size);
// byte[] elems = elements;
// byte[] otherElems = other.elements;
// int i = 0;
// for (; i < minSize; i++) {
// if (elems[i] < otherElems[i]) return (-i) - 1; // this < other
// else if (elems[i] > otherElems[i]) return i + 1; // this > other
// }
// if (other.size > minSize) return (-i) - 1; // this < other
// if (size > minSize) return i + 1; // this > other
// return 0; // equal
// }
// /**
// * Inserts the specified element before the specified index. Shifts the
// * element currently at that index (if any) and any subsequent elements
// * to the right. This is equivalent to <code>replace(index, index, elem, 1)</code>.
// *
// * @param index
// * index before which the specified element is to be inserted
// * @param elem
// * element to insert.
// * @throws IndexOutOfBoundsException if index is out of range.
// */
// private void insert(int index, byte elem) {
// replace(index, index, elem, 1);
// }
//
// /**
// * Inserts the specified elements before the specified index. Shifts the
// * element currently at that index (if any) and any subsequent elements
// * to the right.
// *
// * @param index
// * index before which the specified elements are to be inserted
// * @param elems
// * elements to insert.
// * @throws IndexOutOfBoundsException if index is out of range.
// */
// private void insert(int index, byte[] elems) {
// replace(index, index, elems);
// }
//
// /**
// * Inserts the specified elements before the specified index. Shifts the
// * element currently at that index (if any) and any subsequent elements
// * to the right.
// *
// * @param index
// * index before which the specified elements are to be inserted
// * @param elems
// * elements to insert.
// * @throws IndexOutOfBoundsException if index is out of range.
// */
// private void insert(int index, ArrayByteList elems) {
// replace(index, index, elems);
// }
//
// /**
// * Inserts the remaining buffer elements before the specified index.
// * Shifts the element currently at that index (if any) and any subsequent
// * elements to the right.
// *
// * @param index
// * index before which the specified elements are to be inserted
// * @param elems
// * elements to insert.
// * @throws IndexOutOfBoundsException if index is out of range.
// */
// private void insert(int index, ByteBuffer elems) {
// replace(index, index, elems);
// }
// /**
// * Returns the index of the last occurrence of the specified element within
// * the range <code>[from..to)</code>. Returns <code>-1</code> if the
// * receiver does not contain such an element.
// *
// * @param from
// * the leftmost search index, inclusive.
// * @param to
// * the rightmost search index, exclusive.
// * @param elem
// * element to search for.
// * @return the index of the last occurrence of the element in the receiver;
// * returns <code>-1</code> if the element is not found.
// * @throws IndexOutOfBoundsException
// * if indexes are out of range.
// */
// public int lastIndexOf(int from, int to, byte elem) {
// checkRange(from, to);
// byte[] elems = elements;
// for (int i = to; --i >= from; ) {
// if (elem == elems[i]) return i; //found
// }
// return -1; //not found
// }
// /**
// * Removes the element at the specified index. Shifts any subsequent
// * elements to the left. Keeps the current capacity.
// *
// * @param index
// * the index of the element to removed.
// * @throws IndexOutOfBoundsException
// * if index is out of range
// */
// public void remove(int index) {
// remove(index, index+1);
// }
// /**
// * Replaces all elements in the range <code>[from..to)</code> with the
// * elements <code>replacement[offset..offset+length)</code>. The replacement can have any length.
// * Increases (or decreases) the receiver's size by <code>length - (to - from)</code>.
// * Use <code>from==to</code> to perform pure insertion.
// *
// * @param from the index of the first element to replace (inclusive)
// * @param to the index of the last element to replace (exclusive).
// * @param replacement the elements to replace the replaced elements
// * @param offset the offset of the first replacing element (inclusive)
// * @param length the number of replacing elements
// * @throws IndexOutOfBoundsException if indexes are out of range.
// */
// public void replace(int from, int to, byte[] replacement, int offset, int length) {
// if (offset < 0 || length < 0 || offset + length > replacement.length)
// throw new IndexOutOfBoundsException("offset: " + offset + ", length: " + length + ", replacement.length: " + replacement.length);
// //if (elements == replacement && length - (to - from) != 0) replacement = (byte[]) replacement.clone();
// shrinkOrExpand(from, to, length);
// System.arraycopy(replacement, offset, this.elements, from, length);
// }
// /**
// * Reverses the order of elements in the range <code>[from..to)</code>.
// * Last becomes first, second last becomes second first, and so on.
// * <p>
// * Example: <code>[a,b,c,d].reverse(0,4) --> [d,c,b,a]</code>
// *
// * @param from the index of the first element (inclusive)
// * @param to the index of the last element (exclusive).
// * @throws IndexOutOfBoundsException if indexes are out of range.
// */
// public void reverse(int from, int to) {
// checkRange(from, to);
// byte[] elems = elements;
// int middle = from + (to - from) / 2;
// to--;
// for ( ; from < middle; from++, to--) { // swap
// byte tmp = elems[from];
// elems[from] = elems[to];
// elems[to] = tmp;
// }
// }
// /**
// * Sets the size to the given new size, expanding the list capacity if
// * necessary.
// * <p>
// * Capacity expansion introduces a new backing array. If the new
// * size is greater than the current size the elements between the current
// * size and the new size will become legally accessible but have undefined
// * values. An application will typically want to set these elements to defined
// * values immediately after this method returns. Note that
// * <code>setSize(0)</code> effectively clears the list (as does
// * <code>remove(0, list.size()</code>).
// *
// * @param newSize the new size.
// * @return <code>this</code> (for chaining convenience only)
// * @throws IndexOutOfBoundsException if new size is less than zero.
// */
// public ArrayByteList setSize(int newSize) {
// checkSize(newSize);
// ensureCapacity(newSize);
// // equivalent impl: shrinkOrExpand(0, size, newSize);
// size = newSize;
// return this;
// }
// /**
// * Returns a <code>java.util.ArrayList</code> containing a copy of all elements.
// */
// public java.util.ArrayList toList() {
// java.util.ArrayList list = new java.util.ArrayList(size);
// for (int i = 0; i < size; i++)
// list.add(new Byte(elements[i]));
// return list;
// }
// /**
// * Creates and returns an unsynchronized input stream that reads from this
// * SHARED backing byte list. Useful if legacy code requires adapting to a
// * stream based interface. Note: This is much more efficient and
// * straighforward than using a {@link java.io.ByteArrayInputStream}.
// * <p>
// * Reading from the stream increments an internal counter that keeps track
// * of the next byte to be supplied by the stream's read method. Reading
// * starts at list index 0; end-of-stream is reached on list index size().
// * Reading from the stream leaves the list unmodified (it does not remove
// * elements).
// * <p>
// * Closing the stream has no effect. The stream's methods can be called
// * after the stream has been closed without generating an IOException. In
// * fact the stream implementation never ever throws an IOException.
// *
// * @return the stream
// */
// public InputStream asInputStream() {
// return new ListInputStream();
// }
//
// /**
// * private class; implements/overrides methods of base stream class;
// * well behaved even under weird interleaved list add() and/or remove(),
// * but not under concurrent access, since unsynchronized.
// */
// private class ListInputStream extends InputStream {
//
// private int pos = 0;
// private int mark = 0;
//
// public int read() {
// return pos < size ? (elements[pos++] & 0xff) : -1;
// }
//
// public int read(byte b[], int off, int len) {
// if (off < 0 || len < 0 || off + len > b.length) throw new IndexOutOfBoundsException();
// if (pos >= size) return -1;
// if (pos + len > size) {
// len = size - pos;
// }
// if (len <= 0) return 0;
// System.arraycopy(elements, pos, b, off, len);
// pos += len;
// return len;
// }
//
// public long skip(long n) {
// if (pos + n > size) {
// n = size - pos;
// }
// if (n < 0) return 0;
// pos += n;
// return n;
// }
//
// public int available() {
// return Math.max(0, size - pos); // safe even with list add() and/or remove()
// }
//
// public boolean markSupported() {
// return true;
// }
//
// public void mark(int readlimit) {
// mark = pos;
// }
//
// public void reset() {
// pos = mark;
// }
//
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -