public class ByteArrayGenerator extends Object
Instances of this class are created via the factory methods create(java.lang.String)
and createFromHex(java.lang.String)
. Once you have an instance, each call
to generate()
will create a newly instantiated byte array
initialized randomly as per the construction specification.
Given an instance, you can ask it to create a byte array with
the lowest()
or highest()
values defined by the specification,
as well as ask how many
possible arrays could
be generated. You can also obtain an iterator()
which will iterate
from the lowest to highest values defined by the specification.
Given two instances, you can see whether one is a
superset
of the other, or if the
two intersect
.
Modifier and Type | Method and Description |
---|---|
boolean |
contains(byte[] b)
Returns
true if the specified byte array can be generated
by this byte array generator. |
static ByteArrayGenerator |
create(String spec)
Creates an instance of
ByteArrayGenerator that will generate
byte arrays within the constraints defined by the given specification. |
static ByteArrayGenerator |
createFromHex(String spec)
Creates an instance of
ByteArrayGenerator that will generate
byte arrays within the constraints defined by the given specification. |
boolean |
equals(Object o) |
byte[] |
generate()
Creates and returns a new byte array populated with random data as
per the specification.
|
String |
getNormalizedSpec()
Returns a normalized specification string; that is to say, the
bytes are expressed in lowercase hex, and instances of
00-ff
are replaced with '*' . |
String |
getSpec()
Returns the specification string with which this generator
was initialized.
|
int |
hashCode() |
byte[] |
highest()
Returns an array with the byte values set to their highest values.
|
boolean |
intersects(ByteArrayGenerator other)
Returns
true if this byte array generator intersects
the other byte array generator; false otherwise. |
boolean |
isSuperset(ByteArrayGenerator other)
Returns
true if this byte array generator is a
superset of the other byte array generator; false otherwise. |
Iterator<byte[]> |
iterator()
Returns an iterator that starts with the lowest specified value for
the byte array and iterates through to the highest specified value,
returning
resultSpaceSize byte arrays in total. |
byte[] |
lowest()
Returns an array with the byte values set to their lowest values.
|
BigInteger |
resultSpaceSize()
Returns the total number of possible arrays that could be generated
by this array generator.
|
int |
size()
Returns the "size" of this generator; that is, how many bytes in each
generated array.
|
String |
toDebugString()
Returns a multi-line string representation with information
suitable for debugging.
|
String |
toString() |
public int size()
public String getSpec()
public String getNormalizedSpec()
00-ff
are replaced with '*'
. For example, the generators created
with the following:
create("254:12:0-255:0-255") createFromHex("FE:0C:00-FF:00-FF") createFromHex("fe:c:*:*")all return the same (normalized) byte spec:
"fe:0c:*:*"
public byte[] generate()
public byte[] lowest()
public byte[] highest()
public BigInteger resultSpaceSize()
public Iterator<byte[]> iterator()
resultSpaceSize
byte arrays in total.public String toDebugString()
public boolean isSuperset(ByteArrayGenerator other)
true
if this byte array generator is a
superset of the other byte array generator; false otherwise.
Put another way, the set of all possible byte arrays produced by the
other generator is a subset of the set of all possible byte
arrays produced by this generator.other
- the other generator to compare withIllegalArgumentException
- if other generator is not the same
size as this oneNullPointerException
- if other is nullpublic boolean contains(byte[] b)
true
if the specified byte array can be generated
by this byte array generator.b
- the byte arraypublic boolean intersects(ByteArrayGenerator other)
true
if this byte array generator intersects
the other byte array generator; false otherwise.
Put another way, this predicate returns true if the set of all possible
byte arrays produced by this generator contains any byte
array that could possibly be generated by the other generator.other
- the other generator to compare withIllegalArgumentException
- if the other generator is not the
same size as this oneNullPointerException
- if other is nullpublic static ByteArrayGenerator create(String spec)
ByteArrayGenerator
that will generate
byte arrays within the constraints defined by the given specification.
Specifications are declared with decimal values.
spec is a colon delimited string of the following form:
{byteSpec} [ : {byteSpec} [ ... ] ]where byteSpec represents a single byte in the array, and is of the form:
{n} | {n}-{m} | *where n and m are integers in the range 0 to 255, m (when specified) is > n, and * represents 0-255.
For example, the specification "15:1:0-30:100-255"
declares that
the generator should return byte arrays 4 bytes in length, where the
first byte (index = 0) is always 15, the second byte (index = 1) is
always 1, the third byte is drawn randomly from the range 0 .. 30, and
the last byte is drawn randomly from the range 100 .. 255.
Another example, the specification "0-255:0-255"
declares that
the generator should return byte arrays 2 bytes in length, with both
bytes being any randomly chosen value. This specification could also be
written "*:*"
spec
- the specificationIllegalArgumentException
- if spec is ill-formedpublic static ByteArrayGenerator createFromHex(String spec)
ByteArrayGenerator
that will generate
byte arrays within the constraints defined by the given specification.
Specifications are declared with hex values.
Spec is a colon delimited string of the following form:
{byteSpec} [ : {byteSpec} [ ... ] ]where byteSpec represents a single byte in the array, and is of the form:
{n} | {n}-{m} | *where n and m are hex values in the range 0 to FF, m (when specified) is > n, and * represents
0-FF
. Upper- or lower- case hex digits are allowed.
For example, the specification "F:1:0-1E:C8-FF"
declares that
the generator should return byte arrays 4 bytes in length, where the
first byte (index = 0) is always F (15), the second byte (index = 1)
is always 1, the third byte is drawn randomly from the range 0 .. 1E
(0 .. 30), and the last byte is drawn randomly from the range
C8 .. FF (200 .. 255).
Another example, the specification "00-FF:00-FF"
declares that
the generator should return byte arrays 2 bytes in length, with both
bytes being any randomly chosen value. This specification could also
be written "*:*"
spec
- the specificationIllegalArgumentException
- if spec is ill-formedCopyright © 2015. All Rights Reserved.