public final class Binary extends AbstractByteArrayReadBuffer implements Comparable, Externalizable, ExternalizableLite
AbstractByteArrayReadBuffer.ByteArrayBufferInput
AbstractReadBuffer.AbstractBufferInput
Base.LoggingWriter, Base.StackFrame
ReadBuffer.BufferInput
m_ab, m_cb, m_of
NO_BINARY, NO_BYTES
LOG_ALWAYS, LOG_DEBUG, LOG_ERR, LOG_INFO, LOG_MAX, LOG_MIN, LOG_QUIET, LOG_WARN, POWER_0, POWER_G, POWER_K, POWER_M, POWER_T, UNIT_D, UNIT_H, UNIT_M, UNIT_MS, UNIT_NS, UNIT_S, UNIT_US
UNENCODED
Constructor and Description |
---|
Binary()
Default constructor for a binary object.
|
Binary(Binary that)
Construct a Binary object from a Binary object.
|
Binary(byte[] ab)
Construct a binary object from a byte array.
|
Binary(byte[] ab,
int of,
int cb)
Construct a binary object from a portion of a byte array.
|
Binary(ByteArrayOutputStream stream)
Construct a Binary object from the contents of a ByteArrayOutputStream.
|
Binary(DataInput stream)
Construct a binary object from a DataInput interface.
|
Modifier and Type | Method and Description |
---|---|
int |
calculateNaturalPartition(int cPartitions)
Calculate the partition ID to which the specified Binary should be
naturally assigned.
|
Object |
clone()
Create a clone of this Binary object.
|
static byte[] |
clone(byte[] ab,
int of,
int cb)
Create a clone of the specified binary region.
|
int |
compareTo(Object o)
Compares this object with the specified object for order.
|
Binary |
concat(Binary bin)
Concatenate the passed Binary onto this Binary.
|
boolean |
endsWith(Binary bin)
Tests if this Binary object ends with the specified suffix.
|
static boolean |
equals(byte[] ab1,
int of1,
byte[] ab2,
int of2,
int cb)
Compare two binary regions, testing for equality.
|
boolean |
equals(Object o)
Compares this Binary object with another object for equality.
|
InputStream |
getInputStream()
Get an InputStream to read the Binary object's contents from.
|
int |
hashCode()
Returns a hash code value for the object.
|
int |
indexOf(Binary bin)
Returns the offset within this Binary object of the first occurrence of
the specified Binary.
|
int |
indexOf(Binary bin,
int ofFrom)
Returns the offset within this Binary object of the first occurrence of
the specified Binary, starting the search at the specified offset.
|
int |
indexOf(byte b)
Returns the offset within this Binary object of the first occurrence of
the specified byte.
|
int |
indexOf(byte b,
int ofFrom)
Returns the offset within this Binary object of the first occurrence of
the specified byte, starting the search at the specified offset.
|
protected ReadBuffer |
instantiateReadBuffer(int of,
int cb)
Factory method: Instantiate a ReadBuffer for a portion of this
ReadBuffer.
|
protected boolean |
isByteArrayPrivate()
Determine if the underlying byte[] should be treated as private data.
|
static Binary |
join(Binary[] aBinaries)
Join an array of Binaries into a single Binary.
|
int |
lastIndexOf(Binary bin)
Returns the offset within this Binary object of the last occurrence of
the specified Binary.
|
int |
lastIndexOf(Binary bin,
int ofFrom)
Returns the offset within this Binary object of the last occurrence of
the specified Binary, starting the search at the specified offset.
|
int |
lastIndexOf(byte b)
Returns the offset within this Binary object of the last occurrence of
the specified byte.
|
int |
lastIndexOf(byte b,
int ofFrom)
Returns the offset within this Binary object of the last occurrence of
the specified byte, starting the search at the specified offset and
searching backwards.
|
static int |
memchr(byte[] abHaystack,
int ofHaystack,
int cbHaystack,
byte bNeedle)
Find the specified byte (a "needle") in the specified binary region
("the haystack").
|
static int |
memchr(byte[] abHaystack,
int ofHaystack,
int cbHaystack,
byte bNeedle,
boolean fBackwards)
Find the specified byte (a "needle") in the specified binary region
("the haystack").
|
static int |
memcmp(byte[] ab1,
int of1,
int cb1,
byte[] ab2,
int of2,
int cb2)
Compare two binary regions.
|
static void |
memcpy(byte[] abSrc,
int ofSrc,
byte[] abDest,
int ofDest,
int cbCopy)
Copy binary data from one binary region to another.
|
static int |
memmem(byte[] abHaystack,
int ofHaystack,
int cbHaystack,
byte[] abNeedle,
int ofNeedle,
int cbNeedle)
Find the second binary region (a "needle") in the first binary region
("the haystack").
|
static int |
memmem(byte[] abHaystack,
int ofHaystack,
int cbHaystack,
byte[] abNeedle,
int ofNeedle,
int cbNeedle,
boolean fBackwards)
Find the second binary region (a "needle") in the first binary region
("the haystack").
|
static Binary |
readBinary(DataInput input)
Read a Binary from the specified DataInput.
|
static Binary |
readBinary(DataInputStream in)
Read a Binary from the specified DataInputStream.
|
static Binary |
readBinary(File file)
Read a Binary from the specified File.
|
static Binary |
readBinary(FileChannel channel,
long of,
int cb,
byte[] ab2)
Read a Binary of the specified length at the specified offset from the
specified FileChannel.
|
static Binary |
readBinary(InputStream in)
Read a Binary from the specified InputStream.
|
static Binary |
readBinary(InputStream in,
int cb)
Read a Binary from the specified InputStream.
|
static Binary |
readBinary(URL url)
Read a Binary from the specified URL.
|
void |
readExternal(DataInput in)
Restore the contents of this object by loading the object's state from
the passed DataInput object.
|
void |
readExternal(ObjectInput in)
The object implements the readExternal method to restore its
contents by calling the methods of DataInput for primitive
types and readObject for objects, strings and arrays.
|
boolean |
regionMatches(int ofThis,
Binary that,
int ofThat,
int cb)
Tests if two Binary regions are equal.
|
Binary |
replace(Binary binOld,
Binary binNew)
Replace all occurrences of one specified Binary with another in this
Binary object.
|
Binary |
replace(byte bOld,
byte bNew)
Replace all occurrences of one specified byte with another in this
Binary object.
|
Binary |
replaceRegion(int of,
int cb,
Binary binNew)
Replace a region within this Binary with another Binary.
|
Binary |
reverse()
Reverse this Binary object's byte sequence such that the byte at offset
zero of this sequence occurs at offset length()-1 in the
resulting sequence, the byte at offset one of this sequence occurs at
offset length()-2 in the resulting sequence, and so on.
|
Binary[] |
split(int nSize)
Split a Binary into an array of Binaries each of which does not exceed
the specified size.
|
boolean |
startsWith(Binary bin)
Tests if this Binary object starts with the specified prefix.
|
boolean |
startsWith(Binary bin,
int ofFrom)
Tests if this Binary object starts with the specified prefix beginning
a specified offset.
|
Binary |
toBinary()
Return a new Binary object that holds the complete contents of this
ReadBuffer.
|
Binary |
toBinary(int of,
int cb)
Return a Binary object that holds the specified portion of this
ReadBuffer.
|
String |
toString()
Provide a human-readable representation of the Binary object.
|
static String |
toString(byte[] ab)
For debugging purposes, convert the passed byte array into a string
that contains the information regarding whether the reference is null,
and if it is not null, what the length of the byte array is.
|
void |
writeExternal(DataOutput out)
Save the contents of this object by storing the object's state into
the passed DataOutput object.
|
static void |
writeExternal(DataOutput out,
ReadBuffer buf)
Write the contents of the specified ReadBuffer to the specified DataOutput
stream in a format that can be restored as a Binary via
readExternal(java.io.ObjectInput) |
void |
writeExternal(ObjectOutput out)
The object implements the writeExternal method to save its contents
by calling the methods of DataOutput for its primitive values or
calling the writeObject method of ObjectOutput for objects, strings,
and arrays.
|
byteAt, copyBytes, instantiateBufferInput, length, resetRange, toByteArray, toByteBuffer, toByteBuffer, writeTo, writeTo, writeTo, writeTo, writeTo, writeTo
checkBounds, getBufferInput, getEncodedHash, getReadBuffer, readPackedInt, readUnsignedByte, sizeofPackedInt, subSequence, toByteArray
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
public Binary()
public Binary(byte[] ab)
ab
- an array of bytespublic Binary(byte[] ab, int of, int cb)
ab
- an array of bytesof
- the offset into the byte arraycb
- the number of bytes to extractpublic Binary(Binary that)
that
- another Binary objectString.String(String)
public Binary(ByteArrayOutputStream stream)
stream
- the ByteArrayOutputStream that holds the value that
this Binary object will representpublic Binary(DataInput stream) throws IOException
stream
- the object implementing DataInput from which this
Binary object will load its dataIOException
- if an I/O error occurs reading the Binarypublic boolean regionMatches(int ofThis, Binary that, int ofThat, int cb)
ofThis
- the starting offset of the subregion in this Binary
objectthat
- the Binary object containing the subregion to compare toofThat
- the starting offset of the subregion in the passed
Binary object that
cb
- the number of bytes to comparetrue
if the specified subregion of this Binary
object exactly matches the specified subregion of
the that
Binary object; false
otherwiseNullPointerException
- if that
is
null
String.regionMatches(int, java.lang.String, int, int)
public boolean startsWith(Binary bin, int ofFrom)
bin
- the prefixofFrom
- the offset to begin looking in this Binary objecttrue
if the byte sequence represented by the
bin
argument is a prefix of the substring of
this Binary object starting at offset ofFrom
;
false
otherwiseNullPointerException
- if bin
is
null
.String.startsWith(java.lang.String, int)
public boolean startsWith(Binary bin)
bin
- the prefixtrue
if the byte sequence represented by the
bin
argument is a prefix of this Binary object;
false
otherwiseNullPointerException
- if bin
is
null
.String.startsWith(java.lang.String, int)
public boolean endsWith(Binary bin)
bin
- the suffixtrue
if the byte sequence represented by the
bin
argument is a suffix of this Binary object;
false
otherwiseNullPointerException
- if bin
is
null
.String.endsWith(java.lang.String)
public int indexOf(byte b)
This method is identical in its functionality to the corresponding method in String.
b
- the byte to search for-1
if the byte does not occur in the sequenceString.indexOf(int)
public int indexOf(byte b, int ofFrom)
This method is identical in its functionality to the corresponding method in String.
b
- the byte to search forofFrom
- the offset to search fromofFrom
, or -1
if the
byte does not occur from that offset to the end of the sequenceString.indexOf(int, int)
public int indexOf(Binary bin)
This method is identical in its functionality to the corresponding method in String.
bin
- the Binary to search for-1
if the
byte sequence does not occurString.indexOf(String)
public int indexOf(Binary bin, int ofFrom)
This method is identical in its functionality to the corresponding method in String. Note that one side-effect of maintaining compatibility with the String behavior is that zero-length Strings can be found even when the from-index is out-of-bounds. Given any Binary value "value" and a zero-length binary value "empty", the following will always hold true:
int rnd = new Random().nextInt(Integer.MAX_VALUE - value.length()); assert value.indexOf(empty, value.length() + rnd) == value.length();
bin
- the Binary to search forofFrom
- the offset to search fromofFrom
, or -1
if the byte sequence
does not occur from that offset to the end of this BinaryString.indexOf(String, int)
public int lastIndexOf(byte b)
This method is identical in its functionality to the corresponding method in String.
b
- the byte to search for-1
if the byte does not occur in the sequenceString.lastIndexOf(int)
public int lastIndexOf(byte b, int ofFrom)
This method is identical in its functionality to the corresponding method in String.
b
- the byte to search forofFrom
- the offset to search backwards from (inclusive)ofFrom
, or -1
if the
byte does not occur from that offset to the start of the
sequenceString.lastIndexOf(int, int)
public int lastIndexOf(Binary bin)
This method is identical in its functionality to the corresponding method in String.
bin
- the Binary to search for-1
if the
byte sequence does not occurString.lastIndexOf(String)
public int lastIndexOf(Binary bin, int ofFrom)
This method is identical in its functionality to the corresponding method in String.
bin
- the Binary to search forofFrom
- the offset to search fromofFrom
, or -1
if the byte sequence
does not occur from that offset to the beginning of this BinaryString.lastIndexOf(String, int)
public Binary replace(byte bOld, byte bNew)
bOld
- the byte value to replacebNew
- the byte value to replace bOld withpublic Binary replace(Binary binOld, Binary binNew)
binOld
- the Binary value to replacebinNew
- the Binary value to replace binOld withpublic Binary replaceRegion(int of, int cb, Binary binNew)
This method does not alter the state of this Binary; it creates a new Binary instance instead.
of
- the offset of the range to replace within this Binarycb
- the length of the range to replace in bytesbinNew
- the Binary value to replace the range withpublic Binary concat(Binary bin)
bin
- the Binary to concatenate to this Binarypublic Binary reverse()
public Binary[] split(int nSize)
nSize
- the size of result Binariespublic Binary toBinary()
This is the equivalent of toBinary(0, length())
.
toBinary
in interface ReadBuffer
toBinary
in interface ByteSequence
toBinary
in class AbstractByteArrayReadBuffer
public Binary toBinary(int of, int cb)
This method is an equivalent of
getReadBuffer(of, cb).toBinary()
.
toBinary
in interface ReadBuffer
toBinary
in class AbstractByteArrayReadBuffer
of
- the beginning index, inclusivecb
- the number of bytes to include in the Binary objectprotected ReadBuffer instantiateReadBuffer(int of, int cb)
instantiateReadBuffer
in class AbstractReadBuffer
of
- the beginning index, inclusivecb
- the number of bytes to include in the resulting ReadBufferprotected boolean isByteArrayPrivate()
isByteArrayPrivate
in class AbstractByteArrayReadBuffer
public Object clone()
clone
in interface ReadBuffer
clone
in class AbstractReadBuffer
public String toString()
public int hashCode()
The Binary object uses a CRC32 algorithm to determine the hash code.
hashCode
in class Object
Base.toCrc(byte[], int, int)
public boolean equals(Object o)
equals
in interface ReadBuffer
equals
in class AbstractByteArrayReadBuffer
o
- an object reference or nullpublic int calculateNaturalPartition(int cPartitions)
decorated
with
artificially assigned partitions.
The resulting partition ID will be in the range [0..cPartitions).
Note: if the specified partition count is zero, this method will return a hash code fo the binary, thus allowing clients that don't have a concept of partitioning or knowledge of the partition count to defer the partition id calculation until the partition count is known.
cPartitions
- the partition countpublic int compareTo(Object o)
compareTo
in interface Comparable
o
- the Object to be compared.ClassCastException
- if the specified object's type prevents it
from being compared to this ObjectNullPointerException
- if the specified object is
null
public void readExternal(ObjectInput in) throws IOException
readExternal
in interface Externalizable
in
- the stream to read data from in order to restore the objectIOException
- if an I/O exception occurspublic void writeExternal(ObjectOutput out) throws IOException
writeExternal
in interface Externalizable
out
- the stream to write the object toIOException
- if an I/O exception occurspublic void readExternal(DataInput in) throws IOException
readExternal
in interface ExternalizableLite
in
- the DataInput stream to read data from in order to restore
the state of this objectIOException
- if an I/O exception occursNotActiveException
- if the object is not in its initial
state, and therefore cannot be deserialized intopublic void writeExternal(DataOutput out) throws IOException
writeExternal
in interface ExternalizableLite
out
- the DataOutput stream to write the state of this object toIOException
- if an I/O exception occurspublic static void writeExternal(DataOutput out, ReadBuffer buf) throws IOException
readExternal(java.io.ObjectInput)
out
- the DataOutput stream to write tobuf
- the ReadBuffer to write the contents ofIOException
- if an I/O exception occurspublic InputStream getInputStream()
public static Binary readBinary(FileChannel channel, long of, int cb, byte[] ab2) throws IOException
channel
- the FileChannel to read fromof
- the offset within the FileChannel to read fromcb
- the number of bytes to read from the FileChannel, which
will be the length of the Binaryab2
- reserved; pass in nullIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(InputStream in) throws IOException
in
- the InputStream to read fromIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(InputStream in, int cb) throws IOException
in
- the InputStream to read fromcb
- the exact number of bytes to read from the streamcb
bytesIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(File file) throws IOException
file
- the File to read fromIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(URL url) throws IOException
url
- the URL to read fromIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(DataInput input) throws IOException
input
- the DataInput to read fromIOException
- if an I/O exception or unexpected EOF occurspublic static Binary readBinary(DataInputStream in) throws IOException
in
- the DataInputStream to read fromIOException
- if an I/O exception or unexpected EOF occurspublic static int memchr(byte[] abHaystack, int ofHaystack, int cbHaystack, byte bNeedle)
abHaystack
- the byte array containing the binary region to
search within (the "haystack")ofHaystack
- the offset of the binary region within
abHaystackcbHaystack
- the size in bytes of the binary region within
abHaystackbNeedle
- the byte to search for (a "needle")public static int memchr(byte[] abHaystack, int ofHaystack, int cbHaystack, byte bNeedle, boolean fBackwards)
abHaystack
- the byte array containing the binary region to
search within (the "haystack")ofHaystack
- the offset of the binary region within
abHaystackcbHaystack
- the size in bytes of the binary region within
abHaystackbNeedle
- the byte to search for (a "needle")fBackwards
- pass false to find the first occurrence, or true to
find the last occurrencepublic static int memmem(byte[] abHaystack, int ofHaystack, int cbHaystack, byte[] abNeedle, int ofNeedle, int cbNeedle)
abHaystack
- the byte array containing the binary region to
search within (the "haystack")ofHaystack
- the offset of the binary region within
abHaystackcbHaystack
- the size in bytes of the binary region within
abHaystackabNeedle
- the byte array containing the binary region to
search for (a "needle")ofNeedle
- the offset of the binary region within
abNeedlecbNeedle
- the size in bytes of the binary region within
abNeedlepublic static int memmem(byte[] abHaystack, int ofHaystack, int cbHaystack, byte[] abNeedle, int ofNeedle, int cbNeedle, boolean fBackwards)
abHaystack
- the byte array containing the binary region to
search within (the "haystack")ofHaystack
- the offset of the binary region within
abHaystackcbHaystack
- the size in bytes of the binary region within
abHaystackabNeedle
- the byte array containing the binary region to
search for (a "needle")ofNeedle
- the offset of the binary region within
abNeedlecbNeedle
- the size in bytes of the binary region within
abNeedlefBackwards
- pass false to find the first occurrence, or true to
find the last occurrencepublic static int memcmp(byte[] ab1, int of1, int cb1, byte[] ab2, int of2, int cb2)
ab1
- the byte array containing the first binary region to
compareof1
- the offset of the binary region within ab1cb1
- the size in bytes of the binary region within ab1ab2
- the byte array containing the second binary region to
compareof2
- the offset of the binary region within ab2cb2
- the size in bytes of the binary region within ab2public static void memcpy(byte[] abSrc, int ofSrc, byte[] abDest, int ofDest, int cbCopy)
abSrc
- the byte array containing the binary region to copy fromofSrc
- the offset of the binary region within abSrcabDest
- the byte array containing the binary region to copy toofDest
- the offset of the binary region within abDestcbCopy
- the size in bytes of the binary region to copypublic static boolean equals(byte[] ab1, int of1, byte[] ab2, int of2, int cb)
ab1
- the byte array containing the first binary region to
compareof1
- the offset of the binary region within ab1ab2
- the byte array containing the second binary region to
compareof2
- the offset of the binary region within ab2cb
- the size of the binary regions, which is the number of
bytes to comparepublic static byte[] clone(byte[] ab, int of, int cb)
ab
- the byte array containing the binary region to copy fromof
- the offset of the binary region within abcb
- the size in bytes of the binary region to copypublic static String toString(byte[] ab)
ab
- a byte array; may be null