Class AsciiStringInterner
String instances which contain only ascii characters,
keyed by the byte[] representation (in ascii).
The values are stored in SoftReferences, allowing them to be garbage collected if not in use and there is
memory pressure.
NOTE: Instances are safe for concurrent use.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static final classInstance used for inserting values into the cache.private final classCustomSoftReferenceimplementation which maintains a reference to the key in the cache, which allows aggressive cleaning when garbage collector collects theStringinstance.private static classOnly used for lookups, never to actually store entries. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final ConcurrentMap<AsciiStringInterner.BaseKey, SoftReference<String>> Contains the canonicalized values, keyed by the asciibyte[].(package private) final ReferenceQueue<String> Used forReferenceas values incache. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static booleanarrayEquals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) Performs equality check between a and b (with corresponding offset/length values).private voidProduces aStringinstance for the given bytes.getStringIfPresent(byte[] bytes, int offset, int length, Encoding encoding) Produces aStringinstance for the given bytes.private static inthashKey(byte[] bytes, int offset, int length) Generates a hash value for the relevant entries in bytes as long as all values are ascii (>= 0).booleanPreemptively populates a value into the cache.toString()
-
Field Details
-
cache
Contains the canonicalized values, keyed by the asciibyte[]. -
refQueue
Used forReferenceas values incache.
-
-
Constructor Details
-
AsciiStringInterner
AsciiStringInterner()
-
-
Method Details
-
putString
Preemptively populates a value into the cache. This is intended to be used withStringconstants which are frequently used. While this can work with otherStringvalues, if val is ever garbage collected, it will not be actively removed from this instance.- Parameters:
val- The value to intern. Must not benull.- Returns:
- Indication if val is an ascii String and placed into cache.
-
getString
Produces aStringinstance for the given bytes. If all are valid ascii (i.e.>= 0) either an existing value will be returned, or the newly createdStringwill be stored before being returned.If non-ascii bytes are discovered, the encoding will be used to
decodeand that value will be returned (but not stored).- Parameters:
bytes- The bytes of the String. Must not benull.offset- Offset into bytes to start.length- The number of bytes in bytes which are relevant.encoding- To use if non-ascii bytes seen.- Returns:
- Decoded
Stringfrom bytes. - Throws:
IOException- If error decoding from Encoding.
-
getStringIfPresent
public String getStringIfPresent(byte[] bytes, int offset, int length, Encoding encoding) throws IOException Produces aStringinstance for the given bytes.If all are valid ascii (i.e.
>= 0) and a correspondingStringvalue exists, it will be returned. If no value exists, aStringwill be created, but not stored.If non-ascii bytes are discovered, the encoding will be used to
decodeand that value will be returned (but not stored).- Parameters:
bytes- The bytes of the String. Must not benull.offset- Offset into bytes to start.length- The number of bytes in bytes which are relevant.encoding- To use if non-ascii bytes seen.- Returns:
- Decoded
Stringfrom bytes. - Throws:
IOException- If error decoding from Encoding.
-
cleanQueue
private void cleanQueue()- See Also:
-
hashKey
private static int hashKey(byte[] bytes, int offset, int length) Generates a hash value for the relevant entries in bytes as long as all values are ascii (>= 0).- Returns:
- hash code for relevant bytes, or
0if non-ascii bytes present.
-
arrayEquals
static boolean arrayEquals(byte[] a, int aOffset, int aLength, byte[] b, int bOffset, int bLength) Performs equality check between a and b (with corresponding offset/length values).The
static boolean equals(byte[].class, int, int, byte[], int, intmethod inArraysis optimized for longerbyte[]instances than is expected to be seen here. -
toString
-