Package org.wildfly.common.iteration
Class ByteIterator
- java.lang.Object
-
- org.wildfly.common.iteration.ByteIterator
-
- All Implemented Interfaces:
BiDirIntIterator
,IndexIterator
,IntIterator
public abstract class ByteIterator extends java.lang.Object implements BiDirIntIterator, IndexIterator
A byte iterator.
-
-
Field Summary
Fields Modifier and Type Field Description static ByteIterator
EMPTY
The empty byte iterator.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ByteStringBuilder
appendTo(ByteStringBuilder builder)
java.io.InputStream
asInputStream()
Get this iterator as an input stream.CodePointIterator
asLatin1String()
Get this byte iterator as a Latin-1 string.CodePointIterator
asUtf8String()
Get this byte iterator as a UTF-8 string.CodePointIterator
base32Encode()
Base32-encode the current stream.CodePointIterator
base32Encode(Base32Alphabet alphabet)
Base32-encode the current stream.CodePointIterator
base32Encode(Base32Alphabet alphabet, boolean addPadding)
Base32-encode the current stream.CodePointIterator
base64Encode()
Base64-encode the current stream.CodePointIterator
base64Encode(Base64Alphabet alphabet)
Base64-encode the current stream.CodePointIterator
base64Encode(Base64Alphabet alphabet, boolean addPadding)
Base64-encode the current stream.boolean
contentEquals(ByteIterator other)
Determine if the remaining contents of this iterator are identical to the remaining contents of the other iterator.ByteIterator
delimitedBy(int... delims)
Get a sub-iterator that is delimited by the given bytes.ByteIterator
doFinal(java.security.MessageDigest digest)
ByteIterator
doFinal(javax.crypto.Mac mac)
byte[]
drain()
Drain all the remaining bytes in this iterator.int
drain(byte[] dst)
Drains up todst.length
bytes from this iterator into the givendst
array.int
drain(byte[] dst, int offs, int len)
Drains up tolen
bytes from this iterator into the givendst
array.byte[]
drain(int count)
Drain up tocount
bytes from this iterator, returning the result.byte[]
drainAll(int count)
Drain exactlycount
bytes from this iterator, returning the result.java.io.ByteArrayOutputStream
drainTo(java.io.ByteArrayOutputStream stream)
Drain all the remaining bytes in this iterator to the given stream.java.lang.String
drainToLatin1(int count)
Convenience method to directly drain a certain number of bytes to a Latin-1 string.java.lang.String
drainToUtf8(int count)
Convenience method to directly drain a certain number of bytes to a UTF-8 string.int
getBE16()
int
getBE32()
long
getBE64()
abstract long
getIndex()
Get the current offset, in bytes.int
getLE16()
int
getLE32()
long
getLE64()
int
getPackedBE32()
long
getPackedBE64()
abstract boolean
hasNext()
Determine if there are more bytes after the current byte.abstract boolean
hasPrevious()
Determine if there are more bytes before the current byte.CodePointIterator
hexEncode()
Hex-encode the current stream.CodePointIterator
hexEncode(boolean toUpperCase)
Hex-encode the current stream.ByteIterator
interleavedWith(byte[] table)
Get a byte iterator which translates this byte iterator through an interleaving table.ByteIterator
interleavedWith(int[] table)
Get a byte iterator which translates this byte iterator through an interleaving table.ByteIterator
limitedTo(int size)
Return a copy of this iterator which is limited to the given number of bytes after the current one.abstract int
next()
Get the next byte.static ByteIterator
ofByteBuffer(java.nio.ByteBuffer buffer)
Get a byte iterator for a byte buffer.static ByteIterator
ofBytes(byte... bytes)
Get a byte iterator for a byte array.static ByteIterator
ofBytes(byte[] bytes, int[] interleave)
Get a byte iterator for a byte array with interleave.static ByteIterator
ofBytes(byte[] bytes, int offs, int len)
Get a byte iterator for a byte array.static ByteIterator
ofBytes(byte[] bytes, int offs, int len, int[] interleave)
Get a byte iterator for a byte array with interleave.static ByteIterator
ofIterators(ByteIterator... iterators)
Get a concatenated byte iterator.abstract int
peekNext()
Peek at the next byte without advancing.abstract int
peekPrevious()
Peek at the previous byte without moving backwards.abstract int
previous()
Get the previous byte.ByteIterator
sign(java.security.Signature signature)
void
update(java.security.MessageDigest digest)
void
update(java.security.Signature signature)
void
update(javax.crypto.Mac mac)
boolean
verify(java.security.Signature signature)
-
-
-
Field Detail
-
EMPTY
public static final ByteIterator EMPTY
The empty byte iterator.
-
-
Method Detail
-
hasNext
public abstract boolean hasNext()
Determine if there are more bytes after the current byte.- Specified by:
hasNext
in interfaceBiDirIntIterator
- Specified by:
hasNext
in interfaceIntIterator
- Returns:
true
if there are more bytes,false
otherwise
-
hasPrevious
public abstract boolean hasPrevious()
Determine if there are more bytes before the current byte.- Specified by:
hasPrevious
in interfaceBiDirIntIterator
- Returns:
true
if there are more bytes,false
otherwise
-
next
public abstract int next() throws java.util.NoSuchElementException
Get the next byte.- Specified by:
next
in interfaceBiDirIntIterator
- Specified by:
next
in interfaceIntIterator
- Returns:
- the next byte
- Throws:
java.util.NoSuchElementException
- ifhasNext()
returnsfalse
-
peekNext
public abstract int peekNext() throws java.util.NoSuchElementException
Peek at the next byte without advancing.- Specified by:
peekNext
in interfaceBiDirIntIterator
- Specified by:
peekNext
in interfaceIntIterator
- Returns:
- the next byte
- Throws:
java.util.NoSuchElementException
- ifhasNext()
returnsfalse
-
previous
public abstract int previous() throws java.util.NoSuchElementException
Get the previous byte.- Specified by:
previous
in interfaceBiDirIntIterator
- Returns:
- the previous byte
- Throws:
java.util.NoSuchElementException
- ifhasPrevious()
returnsfalse
-
peekPrevious
public abstract int peekPrevious() throws java.util.NoSuchElementException
Peek at the previous byte without moving backwards.- Specified by:
peekPrevious
in interfaceBiDirIntIterator
- Returns:
- the previous byte
- Throws:
java.util.NoSuchElementException
- ifhasPrevious()
returnsfalse
-
getIndex
public abstract long getIndex()
Get the current offset, in bytes.- Specified by:
getIndex
in interfaceIndexIterator
- Returns:
- the byte offset
-
getBE16
public int getBE16() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getBE32
public int getBE32() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getBE64
public long getBE64() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getLE16
public int getLE16() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getLE32
public int getLE32() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getLE64
public long getLE64() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getPackedBE32
public int getPackedBE32() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
getPackedBE64
public long getPackedBE64() throws java.util.NoSuchElementException
- Throws:
java.util.NoSuchElementException
-
appendTo
public ByteStringBuilder appendTo(ByteStringBuilder builder)
-
update
public void update(java.security.MessageDigest digest)
-
doFinal
public ByteIterator doFinal(java.security.MessageDigest digest)
-
update
public void update(javax.crypto.Mac mac)
-
doFinal
public ByteIterator doFinal(javax.crypto.Mac mac)
-
update
public void update(java.security.Signature signature) throws java.security.SignatureException
- Throws:
java.security.SignatureException
-
sign
public ByteIterator sign(java.security.Signature signature) throws java.security.SignatureException
- Throws:
java.security.SignatureException
-
verify
public boolean verify(java.security.Signature signature) throws java.security.SignatureException
- Throws:
java.security.SignatureException
-
base64Encode
public CodePointIterator base64Encode(Base64Alphabet alphabet, boolean addPadding)
Base64-encode the current stream.- Parameters:
alphabet
- the alphabet to useaddPadding
-true
to add trailing padding,false
to leave it off- Returns:
- an iterator over the encoded characters
-
base64Encode
public CodePointIterator base64Encode(Base64Alphabet alphabet)
Base64-encode the current stream.- Parameters:
alphabet
- the alphabet to use- Returns:
- an iterator over the encoded characters
-
base64Encode
public CodePointIterator base64Encode()
Base64-encode the current stream.- Returns:
- an iterator over the encoded characters
-
base32Encode
public CodePointIterator base32Encode(Base32Alphabet alphabet, boolean addPadding)
Base32-encode the current stream.- Parameters:
alphabet
- the alphabet to useaddPadding
-true
to add trailing padding,false
to leave it off- Returns:
- an iterator over the encoded characters
-
base32Encode
public CodePointIterator base32Encode(Base32Alphabet alphabet)
Base32-encode the current stream.- Parameters:
alphabet
- the alphabet to use- Returns:
- an iterator over the encoded characters
-
base32Encode
public CodePointIterator base32Encode()
Base32-encode the current stream.- Returns:
- an iterator over the encoded characters
-
hexEncode
public CodePointIterator hexEncode(boolean toUpperCase)
Hex-encode the current stream.- Parameters:
toUpperCase
-true
to use upper case characters when encoding,false
to use lower case characters- Returns:
- an iterator over the encoded characters
-
hexEncode
public CodePointIterator hexEncode()
Hex-encode the current stream.- Returns:
- an iterator over the encoded characters
-
asUtf8String
public CodePointIterator asUtf8String()
Get this byte iterator as a UTF-8 string.- Returns:
- the code point iterator
-
asLatin1String
public CodePointIterator asLatin1String()
Get this byte iterator as a Latin-1 string.- Returns:
- the code point iterator
-
contentEquals
public final boolean contentEquals(ByteIterator other)
Determine if the remaining contents of this iterator are identical to the remaining contents of the other iterator. If the contents are not equal, the iterators will be positioned at the location of the first difference. If the contents are equal, the iterators will both be positioned at the end of their contents.- Parameters:
other
- the other byte iterator- Returns:
true
if the contents are equal,false
otherwise
-
limitedTo
public final ByteIterator limitedTo(int size)
Return a copy of this iterator which is limited to the given number of bytes after the current one. Advancing the returned iterator will also advance this one.- Parameters:
size
- the number of bytes- Returns:
- the limited byte iterator
-
delimitedBy
public final ByteIterator delimitedBy(int... delims)
Get a sub-iterator that is delimited by the given bytes. The returned iterator offset starts at 0 and cannot be backed up before that point. The returned iterator will returnfalse
forhasNext()
if the next character in the encapsulated iterator is a delimiter or if the underlying iterator returnsfalse
forhasNext()
.- Parameters:
delims
- the byte delimiters- Returns:
- the sub-iterator
-
interleavedWith
public ByteIterator interleavedWith(byte[] table)
Get a byte iterator which translates this byte iterator through an interleaving table. The table should be 256 entries in size or exceptions may result.- Parameters:
table
- the interleaving table- Returns:
- the interleaving byte iterator
-
interleavedWith
public ByteIterator interleavedWith(int[] table)
Get a byte iterator which translates this byte iterator through an interleaving table. The table should be 256 entries in size or exceptions may result.- Parameters:
table
- the interleaving table- Returns:
- the interleaving byte iterator
-
drainTo
public java.io.ByteArrayOutputStream drainTo(java.io.ByteArrayOutputStream stream)
Drain all the remaining bytes in this iterator to the given stream.- Parameters:
stream
- the stream- Returns:
- the same stream
-
drain
public byte[] drain()
Drain all the remaining bytes in this iterator.- Returns:
- the remaining bytes as a single array
-
drain
public byte[] drain(int count)
Drain up tocount
bytes from this iterator, returning the result.- Parameters:
count
- the number of bytes to read- Returns:
- the array of consumed bytes (may be smaller than
count
)
-
drainAll
public byte[] drainAll(int count) throws java.util.NoSuchElementException
Drain exactlycount
bytes from this iterator, returning the result.- Parameters:
count
- the number of bytes to read- Returns:
- the array of consumed bytes
- Throws:
java.util.NoSuchElementException
- if there are not enough bytes to fill the array
-
drain
public int drain(byte[] dst)
Drains up todst.length
bytes from this iterator into the givendst
array. An attempt is made to drain as many asdst.length
bytes, but a smaller number may be drained.The number of bytes actually drained is returned as an integer. Unlike
InputStream.read(byte[], int, int)
, this method never returns a negative result.- Parameters:
dst
- the buffer into which the data is drained- Returns:
- the total number of bytes drained into
dst
, always greater or equal to0
-
drain
public int drain(byte[] dst, int offs, int len)
Drains up tolen
bytes from this iterator into the givendst
array. An attempt is made to drain as many aslen
bytes, but a smaller number may be drained.The number of bytes actually drained is returned as an integer. Unlike
InputStream.read(byte[], int, int)
, this method never returns a negative result.- Parameters:
dst
- the buffer into which the data is drainedoffs
- the start offset in arraydst
at which the data is written.len
- the maximum number of bytes to drain- Returns:
- the total number of bytes drained into
dst
, always greater or equal to0
-
drainToUtf8
public java.lang.String drainToUtf8(int count)
Convenience method to directly drain a certain number of bytes to a UTF-8 string. If fewer thancount
bytes are available, only the available bytes will be used to construct the string.- Parameters:
count
- the maximum number of bytes to consume- Returns:
- the UTF-8 string
-
drainToLatin1
public java.lang.String drainToLatin1(int count)
Convenience method to directly drain a certain number of bytes to a Latin-1 string. If fewer thancount
bytes are available, only the available bytes will be used to construct the string.- Parameters:
count
- the maximum number of bytes to consume- Returns:
- the Latin-1 string
-
ofBytes
public static ByteIterator ofBytes(byte... bytes)
Get a byte iterator for a byte array.- Parameters:
bytes
- the array- Returns:
- the byte iterator
-
ofBytes
public static ByteIterator ofBytes(byte[] bytes, int offs, int len)
Get a byte iterator for a byte array.- Parameters:
bytes
- the arrayoffs
- the array offsetlen
- the number of bytes to include- Returns:
- the byte iterator
-
ofBytes
public static ByteIterator ofBytes(byte[] bytes, int offs, int len, int[] interleave)
Get a byte iterator for a byte array with interleave.- Parameters:
bytes
- the arrayoffs
- the array offsetlen
- the number of bytes to includeinterleave
- the interleave table to use- Returns:
- the byte iterator
-
ofBytes
public static ByteIterator ofBytes(byte[] bytes, int[] interleave)
Get a byte iterator for a byte array with interleave.- Parameters:
bytes
- the arrayinterleave
- the interleave table to use- Returns:
- the byte iterator
-
ofByteBuffer
public static ByteIterator ofByteBuffer(java.nio.ByteBuffer buffer)
Get a byte iterator for a byte buffer. The buffer's position is kept up to date with the number of bytes consumed by the iterator. The iterator cannot be moved before the position that the buffer had when the iterator was constructed (this position is considered the zero offset).- Parameters:
buffer
- the byte buffer (must not benull
)- Returns:
- the byte iterator (not
null
)
-
ofIterators
public static ByteIterator ofIterators(ByteIterator... iterators)
Get a concatenated byte iterator. The array and the byte iterators in the array must not be modified or inconsistent behavior will result.- Parameters:
iterators
- the iterators array (must not benull
or containnull
elements)- Returns:
- a concatenated iterator
-
asInputStream
public final java.io.InputStream asInputStream()
Get this iterator as an input stream.- Returns:
- the input stream (not
null
)
-
-