Class StringEncoding
- All Implemented Interfaces:
Encoding<String>,NaturalSortAware,Serializable,Comparator<String>
String encoding.
Null values are not supported by this class and there is no default value.
Strings are encoded as a sequence of characters followed by 0x00, where each character is encoded via
UnsignedIntEncoder, with the special exception that the characters 0x0000 and 0x0001
are prefixed with a 0x01 byte to avoid writing a 0x00. We rely on the fact that UnsignedIntEncoder
encodes 0 and 1 as 0x00 and 0x01, respectively. As a result of this encoding,
this encoding sortsNaturally().
- See Also:
-
Field Summary
Fields inherited from class io.permazen.encoding.AbstractEncoding
encodingId, typeTokenFields inherited from interface io.permazen.encoding.Encoding
MAX_ARRAY_DIMENSIONS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintOrder two values.<S> StringfromString(String string) Parse a non-null value previously encoded bytoString().Get the fixed width of this encoding, if any.booleanDetermine whether any of this encoding's encoded values start with a0x00byte.booleanDetermine whether any of this encoding's encoded values start with a0xffbyte.read(ByteData.Reader reader) Read a value from the given input.voidskip(ByteData.Reader reader) Read and discard an encoded value from the given input.booleanDetermine if this instance sorts Java instances naturally.booleanDetermine whether this encoding supports null values.Encode the given non-null value as aString.voidwrite(ByteData.Writer writer, String value) Write a value to the given output.Methods inherited from class io.permazen.encoding.AbstractEncoding
equals, getDefaultValue, getDefaultValueBytes, getEncodingId, getTypeToken, hashCode, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Comparator
reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongMethods inherited from interface io.permazen.encoding.Encoding
decode, encode, validate, validateAndWrite
-
Constructor Details
-
StringEncoding
public StringEncoding()
-
-
Method Details
-
read
Description copied from interface:EncodingRead a value from the given input.- Parameters:
reader- byte input- Returns:
- decoded value (possibly null)
-
write
Description copied from interface:EncodingWrite a value to the given output.- Parameters:
writer- byte outputvalue- value to write (possibly null)
-
skip
Description copied from interface:EncodingRead and discard an encoded value from the given input.If the value skipped over is invalid, this method may, but is not required to, throw
IllegalArgumentException.If the value skipped over is truncated, this method must throw
IndexOutOfBoundsException.- Parameters:
reader- byte input
-
toString
Encode the given non-null value as aString.Because
Encoding.toString()disallows XML-invalid characters, the returned string is not always equal tovalue. Instead, the implementation inStringEncodingdelegates toStringEncoder.encode()to backslash-escape invalid characters.- Parameters:
value- string value, never null- Returns:
- backslash-escaped
value - Throws:
IllegalArgumentException- ifvalueis null- See Also:
-
fromString
Parse a non-null value previously encoded bytoString().The implementation in
StringEncodingdelegates toStringEncoder.decode().- Parameters:
string- non-null value previously encoded bytoString()- Returns:
- decoded value
- Throws:
IllegalArgumentException- ifstringis invalidIllegalArgumentException- ifstringis null
-
supportsNull
public boolean supportsNull()Description copied from interface:EncodingDetermine whether this encoding supports null values.- Returns:
- true if null is a valid value, otherwise false
-
hasPrefix0x00
public boolean hasPrefix0x00()Description copied from interface:EncodingDetermine whether any of this encoding's encoded values start with a0x00byte. Certain optimizations are possible when this is not the case. It is safe for this method to always return true.Note: changing the result of this method may result in an incompatible encoding if this encoding is wrapped in another class.
- Returns:
- true if an encoded value starting with
0x00exists
-
hasPrefix0xff
public boolean hasPrefix0xff()Description copied from interface:EncodingDetermine whether any of this encoding's encoded values start with a0xffbyte. Certain optimizations are possible when this is not the case. It is safe for this method to always return true.Note: changing the result of this method may result in an incompatible encoding if this encoding is wrapped in another class.
- Returns:
- true if an encoded value starting with
0xffexists
-
compare
Description copied from interface:EncodingOrder two values.This method must provide a total ordering of all supported Java values that is consistent with the database ordering, i.e., the unsigned lexicographical ordering of the corresponding
byte[]encoded values.If null is a supported Java value, then the this method must accept null parameters without throwing an exception (note, this is a stronger requirement than the
Comparatorinterface normally requires).Note: by convention, null values usually sort last.
-
sortsNaturally
public boolean sortsNaturally()Description copied from interface:NaturalSortAwareDetermine if this instance sorts Java instances naturally.This method should return true only if all of the following are true:
- This class also implements
Comparatorfor some Java typeT. - Type
Thas a natural ordering (i.e.,Titself implementsComparable). - The ordering implied by this class's
compare()method is identical toT's natural ordering.
- Returns:
- true if this instance orders Java values in their natural order
- This class also implements
-
getFixedWidth
Description copied from interface:EncodingGet the fixed width of this encoding, if any.Some encodings encode every value into the same number of bytes. For such encodings, this method returns that number. For variable width encodings, this method must return empty.
- Returns:
- the number of bytes of every encoded value, or empty if the encoding length varies
-
convert
Description copied from interface:EncodingAttempt to convert a value from the givenEncodinginto a value of thisEncoding.For a non-null
value, the implementation inEncodingfirst checks whether thevalueis already a valid value for this encoding; if so, the value is returned. Otherwise, it invokesencoding.toString(value)to convertvalueinto aString, and then attempts to parse that string viathis.fromString(); if the parse fails, anIllegalArgumentExceptionis thrown. Note this means that any value will convert successfully to aString, as long as it doesn't contain an invalid escape sequence (seetoString(java.lang.String)).If
valueis null, the implementation inEncodingreturns null, unless this encoding does not support null values, in which case anIllegalArgumentExceptionis thrown.Permazen's built-in encodings include the following conversions:
- Non-boolean Primitive types:
- Convert from other non-boolean primitive types as if by the corresponding Java cast
- Convert from boolean by converting to zero (if false) or one (if true)
- Boolean: converts from other primitive types as if by
value != 0 - A
char[]array and aStringare convertible to each other - A
charand aStringof length one are convertible to each other (otherStrings are not) - Arrays: converted by converting each array element individually (if possible)
- Type Parameters:
S- source encoding- Parameters:
type- theEncodingofvaluevalue- the value to convert- Returns:
valueconverted to this instance's type
- Non-boolean Primitive types:
-