final class AsciiStringInterner
extends java.lang.Object
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.
| Modifier and Type | Class and Description |
|---|---|
private static class |
AsciiStringInterner.BaseKey |
private static class |
AsciiStringInterner.Key
Instance used for inserting values into the cache.
|
private class |
AsciiStringInterner.StringReference
Custom
SoftReference implementation which maintains a reference to the key in the cache,
which allows aggressive cleaning when garbage collector collects the String instance. |
private static class |
AsciiStringInterner.TempKey
Only used for lookups, never to actually store entries.
|
| Modifier and Type | Field and Description |
|---|---|
(package private) java.util.concurrent.ConcurrentMap<AsciiStringInterner.BaseKey,java.lang.ref.SoftReference<java.lang.String>> |
cache
Contains the canonicalized values, keyed by the ascii
byte[]. |
(package private) java.lang.ref.ReferenceQueue<java.lang.String> |
refQueue
Used for
Reference as values in cache. |
| Constructor and Description |
|---|
AsciiStringInterner() |
| Modifier and Type | Method and Description |
|---|---|
(package private) 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).
|
private void |
cleanQueue()
|
java.lang.String |
getString(byte[] bytes,
int offset,
int length,
Encoding encoding)
Produces a
String instance for the given bytes. |
java.lang.String |
getStringIfPresent(byte[] bytes,
int offset,
int length,
Encoding encoding)
Produces a
String instance for the given bytes. |
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). |
boolean |
putString(java.lang.String val)
Preemptively populates a value into the cache.
|
java.lang.String |
toString() |
final java.util.concurrent.ConcurrentMap<AsciiStringInterner.BaseKey,java.lang.ref.SoftReference<java.lang.String>> cache
byte[].final java.lang.ref.ReferenceQueue<java.lang.String> refQueue
Reference as values in cache.public boolean putString(java.lang.String val)
String constants
which are frequently used. While this can work with other String values, if val is ever
garbage collected, it will not be actively removed from this instance.val - The value to intern. Must not be null.public java.lang.String getString(byte[] bytes,
int offset,
int length,
Encoding encoding)
throws java.io.IOException
String instance for the given bytes. If all are valid ascii (i.e. >= 0)
either an existing value will be returned, or the newly created String will be stored before being
returned.
If non-ascii bytes are discovered, the encoding will be used to
decode and that value will be returned (but not stored).
bytes - The bytes of the String. Must not be null.offset - Offset into bytes to start.length - The number of bytes in bytes which are relevant.encoding - To use if non-ascii bytes seen.String from bytes.java.io.IOException - If error decoding from Encoding.public java.lang.String getStringIfPresent(byte[] bytes,
int offset,
int length,
Encoding encoding)
throws java.io.IOException
String instance for the given bytes.
If all are valid ascii (i.e. >= 0) and a corresponding String value exists, it
will be returned. If no value exists, a String will be created, but not stored.
If non-ascii bytes are discovered, the encoding will be used to
decode and that value will be returned (but not stored).
bytes - The bytes of the String. Must not be null.offset - Offset into bytes to start.length - The number of bytes in bytes which are relevant.encoding - To use if non-ascii bytes seen.String from bytes.java.io.IOException - If error decoding from Encoding.private void cleanQueue()
private static int hashKey(byte[] bytes,
int offset,
int length)
>= 0).0 if non-ascii bytes present.static boolean arrayEquals(byte[] a,
int aOffset,
int aLength,
byte[] b,
int bOffset,
int bLength)
The static boolean equals(byte[].class, int, int, byte[], int, int method in Arrays
is optimized for longer byte[] instances than is expected to be seen here.
public java.lang.String toString()
toString in class java.lang.Object