|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.javaranch.common.ByteVector
Like the Vector object, except rather than tracking dynamic array of pointers to different objects, this is simply a dynamic array of bytes.
The advantage is speed and memory savings.
- - - - - - - - - - - - - - - - -
Copyright (c) 1998-2004 Paul Wheaton
You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...
My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.
The original source can be found at JavaRanch
- - - - - - - - - - - - - - - - -
Constructor Summary | |
ByteVector()
The object has memory allocated to store a moderate number of bytes. |
|
ByteVector(byte[] b)
The object is inititialized to contain a copy of a byte array. |
|
ByteVector(ByteVector b)
The object is inititialized to contain a copy of another ByteVector. |
|
ByteVector(int initialSize)
Create a new ByteVector with a specific initial capacity. |
|
ByteVector(int initialSize,
int initialExtra)
Create a new ByteVector with a specific initial capacity and to be able to grow by a specific increment. |
Method Summary | |
void |
append(byte b)
Add one byte to the end of the vector. |
void |
append(byte[] b)
Add an array of bytes to the end of this object. |
void |
append(byte[] b,
int numBytes)
Add an array of bytes to the end of this object. |
void |
append(ByteVector b)
Add all of the bytes in another ByteVector to the end of this object. |
void |
delete(int index,
int howMany)
Remove bytes from your object. |
byte |
get(int index)
Get a copy of one byte. |
ByteVector |
get(int index,
int length)
Get a copy of a "sub-vector". |
int |
getCapacity()
Get the number of bytes this object can hold without doing a reallocation. |
int |
indexOf(byte b)
Find the position within your object of a particular character. |
int |
indexOf(byte[] b)
Find the position within your object of a particular sequence of bytes. |
int |
indexOf(byte[] b,
int startPos)
Find the position within your object of a particular sequence of bytes. |
int |
indexOf(byte b,
int startPos)
Find the position within your object of a particular byte. |
void |
insert(byte[] b,
int index)
Insert an array of bytes immediately before a particular byte in the object. |
void |
insert(byte b,
int index)
Insert a byte immediately before a particular byte in the object. |
byte |
last()
Get a copy of the last byte. |
int |
length()
Get the number of bytes currently being used. |
byte |
pop()
If you wish to treat your object like a stack, you can push and pop bytes. |
void |
push(byte b)
If you wish to treat your object like a stack, you can push and pop bytes. |
void |
replace(byte[] b1,
byte b2)
Search for a particular sequence of bytes and replace them with one byte. |
void |
replace(byte[] b1,
byte[] b2)
Search for a particular sequence of bytes and replace it with a different sequence of bytes. |
void |
replace(byte b1,
byte b2)
Replace all instances of one byte with another byte. |
void |
replace(byte b1,
byte[] b2)
Replace all instances of one byte with several bytes. |
void |
set(int index,
byte b)
Set one byte in the object. |
void |
setCapacity(int howMuch)
Force this object to be able to hold a specific number of bytes without reallocation. |
void |
setExtra(int howMuch)
Set how much to grow when growth is needed. |
void |
setLength(int newLength)
Force the number of bytes to be currently used. |
boolean |
startsWith(byte[] b)
A fast way to test the first few bytes of your object. |
byte[] |
toByteArray()
Copy the existing object into a byte array. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ByteVector(int initialSize)
initialSize
- The initial capacity. public ByteVector(int initialSize, int initialExtra)
initialSize
- The initial capacity. initialExtra
- How many bytes the object will grow by when more memory is needed. public ByteVector()
public ByteVector(byte[] b)
Enough memory is allocated for some growth.
b
- A byte array to copy. public ByteVector(ByteVector b)
Enough memory is allocated for some growth.
b
- A ByteVector to copy. Method Detail |
public int length()
Not the same as the number of bytes currently allocated.
public void setLength(int newLength)
If you want to empty your ByteVector, use setLength(0);
If the new length is longer than the existing length, the previously unused bytes will be set to zero.
newLength
- The new length. public void setExtra(int howMuch)
Smaller values will usually save memory, but frequent reallocation may take a lot of time.
howMuch
- The number of extra bytes to allocate when memory reallocation is required. Values must be greater than zero. public int getCapacity()
public void setCapacity(int howMuch)
Any attempt to make the object grow bigger than this size will succeed.
Setting the capacity to be smaller than the current object size will result in trailing bytes being clipped.
howMuch
- How many bytes will be the new capacity. public byte get(int index)
index
- Which byte (0 is the first byte).
public byte last()
public ByteVector get(int index, int length)
index
- Where to start copying. length
- How many bytes to copy.
public byte[] toByteArray()
public void set(int index, byte b)
index
- Which byte to set. If index references a byte beyond the last byte of the object, the object will be lengthened and the last byte will be set appropriately (undefined bytes will be set to zero).b
- The byte to be placed at index.public void append(byte b)
b
- The byte to append. public void append(byte[] b, int numBytes)
b
- The array containing the bytes to be appended. numBytes
- The number of bytes to append from bpublic void append(byte[] b)
b
- The byte array to be appended. public void append(ByteVector b)
b
- The ByteVector to be appended. public void insert(byte b, int index)
b
- The byte to be inserted. index
- Where to insert b. The byte currently at index will me moved to the right. public void insert(byte[] b, int index)
b
- The array of bytes to be inserted. index
- Where to insert b. The byte currently at index will me moved to the right. public void delete(int index, int howMany)
index
- Referencing the first byte to be removed. howMany
- The number of bytes to remove. public void push(byte b)
b
- The byte that will be appended to the end of your object. public byte pop()
Remember the old assembly programming pearl: May all your pushes be popped.
public boolean startsWith(byte[] b)
b
- An array of bytes to compare to.
public int indexOf(byte b, int startPos)
b
- The byte to search for. startPos
- The byte position in your object to start looking for b.
public int indexOf(byte[] b, int startPos)
b
- A byte array representing the byte sequence to search for. startPos
- The byte position in your object to start looking for b.
public int indexOf(byte b)
Searching begins with the first byte in your object.
b
- The byte to search for.
public int indexOf(byte[] b)
Searching begins with the first byte in your object.
b
- A byte array representing the byte sequence to search for.
public void replace(byte b1, byte b2)
All occurances are replaced.
Searching for the "next instance" of a byte will begin immediately after the last replacement so that there is no chance of an infinite loop.
b1
- The byte to search for. b2
- The byte that is to replace b1. public void replace(byte b1, byte[] b2)
All occurances are replaced.
Searching for the "next instance" of a byte will begin immediately after the last replacement so that there is no chance of an infinite loop.
b1
- The byte to search for. b2
- The array of bytes that is to replace b1. public void replace(byte[] b1, byte b2)
All occurances are replaced.
Searching for the "next instance" of a byte sequence will begin immediately after the last replacement so that there is no chance of an infinite loop.
b1
- A byte array representing a byte sequence to search for. b2
- The byte that is to replace b1. public void replace(byte[] b1, byte[] b2)
All occurances are replaced.
Searching for the "next instance" of a byte sequence will begin immediately after the last replacement so that there is no chance of an infinite loop.
b1
- A byte array representing a byte sequence to search for. b2
- A byte array representing a byte sequence that is to replace b1.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |