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 ByteIteratorEMPTYThe empty byte iterator.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description ByteStringBuilderappendTo(ByteStringBuilder builder)java.io.InputStreamasInputStream()Get this iterator as an input stream.CodePointIteratorasLatin1String()Get this byte iterator as a Latin-1 string.CodePointIteratorasUtf8String()Get this byte iterator as a UTF-8 string.CodePointIteratorbase32Encode()Base32-encode the current stream.CodePointIteratorbase32Encode(Base32Alphabet alphabet)Base32-encode the current stream.CodePointIteratorbase32Encode(Base32Alphabet alphabet, boolean addPadding)Base32-encode the current stream.CodePointIteratorbase64Encode()Base64-encode the current stream.CodePointIteratorbase64Encode(Base64Alphabet alphabet)Base64-encode the current stream.CodePointIteratorbase64Encode(Base64Alphabet alphabet, boolean addPadding)Base64-encode the current stream.booleancontentEquals(ByteIterator other)Determine if the remaining contents of this iterator are identical to the remaining contents of the other iterator.ByteIteratordelimitedBy(int... delims)Get a sub-iterator that is delimited by the given bytes.ByteIteratordoFinal(java.security.MessageDigest digest)ByteIteratordoFinal(javax.crypto.Mac mac)byte[]drain()Drain all the remaining bytes in this iterator.intdrain(byte[] dst)Drains up todst.lengthbytes from this iterator into the givendstarray.intdrain(byte[] dst, int offs, int len)Drains up tolenbytes from this iterator into the givendstarray.byte[]drain(int count)Drain up tocountbytes from this iterator, returning the result.byte[]drainAll(int count)Drain exactlycountbytes from this iterator, returning the result.java.io.ByteArrayOutputStreamdrainTo(java.io.ByteArrayOutputStream stream)Drain all the remaining bytes in this iterator to the given stream.java.lang.StringdrainToLatin1(int count)Convenience method to directly drain a certain number of bytes to a Latin-1 string.java.lang.StringdrainToUtf8(int count)Convenience method to directly drain a certain number of bytes to a UTF-8 string.intgetBE16()intgetBE32()longgetBE64()abstract longgetIndex()Get the current offset, in bytes.intgetLE16()intgetLE32()longgetLE64()intgetPackedBE32()longgetPackedBE64()abstract booleanhasNext()Determine if there are more bytes after the current byte.abstract booleanhasPrevious()Determine if there are more bytes before the current byte.CodePointIteratorhexEncode()Hex-encode the current stream.CodePointIteratorhexEncode(boolean toUpperCase)Hex-encode the current stream.ByteIteratorinterleavedWith(byte[] table)Get a byte iterator which translates this byte iterator through an interleaving table.ByteIteratorinterleavedWith(int[] table)Get a byte iterator which translates this byte iterator through an interleaving table.ByteIteratorlimitedTo(int size)Return a copy of this iterator which is limited to the given number of bytes after the current one.abstract intnext()Get the next byte.static ByteIteratorofByteBuffer(java.nio.ByteBuffer buffer)Get a byte iterator for a byte buffer.static ByteIteratorofBytes(byte... bytes)Get a byte iterator for a byte array.static ByteIteratorofBytes(byte[] bytes, int[] interleave)Get a byte iterator for a byte array with interleave.static ByteIteratorofBytes(byte[] bytes, int offs, int len)Get a byte iterator for a byte array.static ByteIteratorofBytes(byte[] bytes, int offs, int len, int[] interleave)Get a byte iterator for a byte array with interleave.static ByteIteratorofIterators(ByteIterator... iterators)Get a concatenated byte iterator.abstract intpeekNext()Peek at the next byte without advancing.abstract intpeekPrevious()Peek at the previous byte without moving backwards.abstract intprevious()Get the previous byte.ByteIteratorsign(java.security.Signature signature)voidupdate(java.security.MessageDigest digest)voidupdate(java.security.Signature signature)voidupdate(javax.crypto.Mac mac)booleanverify(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:
hasNextin interfaceBiDirIntIterator- Specified by:
hasNextin interfaceIntIterator- Returns:
trueif there are more bytes,falseotherwise
-
hasPrevious
public abstract boolean hasPrevious()
Determine if there are more bytes before the current byte.- Specified by:
hasPreviousin interfaceBiDirIntIterator- Returns:
trueif there are more bytes,falseotherwise
-
next
public abstract int next() throws java.util.NoSuchElementExceptionGet the next byte.- Specified by:
nextin interfaceBiDirIntIterator- Specified by:
nextin interfaceIntIterator- Returns:
- the next byte
- Throws:
java.util.NoSuchElementException- ifhasNext()returnsfalse
-
peekNext
public abstract int peekNext() throws java.util.NoSuchElementExceptionPeek at the next byte without advancing.- Specified by:
peekNextin interfaceBiDirIntIterator- Specified by:
peekNextin interfaceIntIterator- Returns:
- the next byte
- Throws:
java.util.NoSuchElementException- ifhasNext()returnsfalse
-
previous
public abstract int previous() throws java.util.NoSuchElementExceptionGet the previous byte.- Specified by:
previousin interfaceBiDirIntIterator- Returns:
- the previous byte
- Throws:
java.util.NoSuchElementException- ifhasPrevious()returnsfalse
-
peekPrevious
public abstract int peekPrevious() throws java.util.NoSuchElementExceptionPeek at the previous byte without moving backwards.- Specified by:
peekPreviousin interfaceBiDirIntIterator- Returns:
- the previous byte
- Throws:
java.util.NoSuchElementException- ifhasPrevious()returnsfalse
-
getIndex
public abstract long getIndex()
Get the current offset, in bytes.- Specified by:
getIndexin 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-trueto add trailing padding,falseto 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-trueto add trailing padding,falseto 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-trueto use upper case characters when encoding,falseto 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:
trueif the contents are equal,falseotherwise
-
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 returnfalseforhasNext()if the next character in the encapsulated iterator is a delimiter or if the underlying iterator returnsfalseforhasNext().- 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 tocountbytes 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.NoSuchElementExceptionDrain exactlycountbytes 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.lengthbytes from this iterator into the givendstarray. An attempt is made to drain as many asdst.lengthbytes, 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 tolenbytes from this iterator into the givendstarray. An attempt is made to drain as many aslenbytes, 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 arraydstat 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 thancountbytes 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 thancountbytes 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 benullor containnullelements)- Returns:
- a concatenated iterator
-
asInputStream
public final java.io.InputStream asInputStream()
Get this iterator as an input stream.- Returns:
- the input stream (not
null)
-
-