Package net.morimekta.util
Class Slice
- java.lang.Object
-
- net.morimekta.util.Slice
-
- All Implemented Interfaces:
java.lang.Comparable<Slice>
,Stringable
@Immutable public class Slice extends java.lang.Object implements java.lang.Comparable<Slice>, Stringable
A slice of a byte array. Note that all the helper methods assume UTF_8 encoding.
-
-
Constructor Summary
Constructors Constructor Description Slice(byte[] fb, int off, int len)
Create a slice instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
asString()
Get the whole slice as a string.char
charAt(int i)
Get character at slice relative position.int
compareTo(Slice o)
Compare slice with other slice.boolean
contains(byte a)
Checks if a single byte can be found in the slice.boolean
contains(byte[] a)
Checks if the byte array is contained in the slice.boolean
containsAny(byte... a)
Checks if any of the provided bytes is contained in the slice.boolean
equals(java.lang.Object o)
int
hashCode()
int
length()
Get the total length of the slice.int
offset()
Get the offset of the buffer.double
parseDouble()
Get the whole slice as a real number.long
parseInteger()
Get the whole slice as a simple integer.boolean
strEquals(byte[] a)
Checks if the slice is equal to given byte array.boolean
strEquals(byte[] a, int aOff, int aLen)
Checks if the slice is equal to a portion of a given byte array.Slice
substring(int start, int end)
Create a substring slice based on the current slice.java.lang.String
toString()
-
-
-
Method Detail
-
offset
public int offset()
Get the offset of the buffer.- Returns:
- The slice offset.
-
length
public int length()
Get the total length of the slice.- Returns:
- The slice length.
-
asString
public java.lang.String asString()
Get the whole slice as a string.- Specified by:
asString
in interfaceStringable
- Returns:
- Slice decoded as UTF_8 string.
-
substring
public Slice substring(int start, int end)
Create a substring slice based on the current slice.- Parameters:
start
- The internal start position, relative to the slice's offset.end
- The internal end position, relative to the slice's offset. If end is negative, then it is relative to the slice's end position.- Returns:
- The substring slice.
-
charAt
public char charAt(int i)
Get character at slice relative position.- Parameters:
i
- The position to get. If negative is relative to the slice's end position.- Returns:
- The char at given position.
-
parseInteger
public long parseInteger()
Get the whole slice as a simple integer.- Returns:
- Integer long value.
-
parseDouble
public double parseDouble()
Get the whole slice as a real number.- Returns:
- Real double value.
-
strEquals
public boolean strEquals(byte[] a)
Checks if the slice is equal to given byte array.- Parameters:
a
- The array to compare with.- Returns:
- True if equal.
-
strEquals
public boolean strEquals(byte[] a, int aOff, int aLen)
Checks if the slice is equal to a portion of a given byte array.- Parameters:
a
- The array to compare with.aOff
- The offset to compare with in the array.aLen
- The length to compare with.- Returns:
- True if equal.
-
containsAny
public boolean containsAny(byte... a)
Checks if any of the provided bytes is contained in the slice.- Parameters:
a
- Bytes to find in the slice.- Returns:
- True if any of the bytes were found.
-
contains
public boolean contains(byte[] a)
Checks if the byte array is contained in the slice.- Parameters:
a
- The byte array to find.- Returns:
- True if the byte array was found.
-
contains
public boolean contains(byte a)
Checks if a single byte can be found in the slice.- Parameters:
a
- The byte to find.- Returns:
- True of the byte was found.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
compareTo
public int compareTo(Slice o)
Compare slice with other slice. Slice ordering: - Firstly ordered by start (offset) position. - Secondly ordered by reverse length (longest slice first). Result is undefined of the two slices point to different byte buffers.- Specified by:
compareTo
in interfacejava.lang.Comparable<Slice>
- Parameters:
o
- The other slice.- Returns:
- Compared value.
-
-