public abstract class Obfuscator extends Object
CharSequences
or the contents of Readers
.Modifier and Type | Class and Description |
---|---|
static interface |
Obfuscator.ObfuscatorFunction
A function that can parts of
CharSequence s. |
static class |
Obfuscator.PortionBuilder
A builder for obfuscators that obfuscate a specific portion of their input.
|
static class |
Obfuscator.Prefix
A prefix of a specific length that uses a specific obfuscator.
|
static class |
Obfuscator.SplitPoint
A point in a
CharSequence or Reader to split obfuscation. |
Constructor and Description |
---|
Obfuscator() |
Modifier and Type | Method and Description |
---|---|
static Obfuscator |
all()
Returns an immutable obfuscator that replaces all characters with
* . |
static Obfuscator |
all(char maskChar)
Returns an immutable obfuscator that replaces all characters with a specific character.
|
static Obfuscator |
fixedLength(int fixedLength)
Returns an immutable obfuscator that replaces all characters with a fixed number of
* . |
static Obfuscator |
fixedLength(int fixedLength,
char maskChar)
Returns an immutable obfuscator that replaces all characters with a fixed number of a specific character.
|
static Obfuscator |
fixedValue(String fixedValue)
Returns an immutable obfuscator that replaces all characters with a fixed value.
|
static Obfuscator |
fromFunction(Function<? super CharSequence,? extends CharSequence> function)
Returns an obfuscator that uses a function to obfuscate text.
|
static Obfuscator |
fromFunction(Obfuscator.ObfuscatorFunction function)
Returns an obfuscator that uses a function to obfuscate text.
|
static Obfuscator |
none()
Returns an immutable obfuscator that does not obfuscate anything.
|
<E> Collection<E> |
obfuscateCollection(Collection<E> collection)
Obfuscates a collection.
|
<E> Collection<E> |
obfuscateCollection(Collection<E> collection,
Function<? super E,? extends CharSequence> elementRepresentation)
Obfuscates a collection.
|
<E> List<E> |
obfuscateList(List<E> list)
Obfuscates a list.
|
<E> List<E> |
obfuscateList(List<E> list,
Function<? super E,? extends CharSequence> elementRepresentation)
Obfuscates a list.
|
<K,V> Map<K,V> |
obfuscateMap(Map<K,V> map)
Obfuscates a map.
|
<K,V> Map<K,V> |
obfuscateMap(Map<K,V> map,
Function<? super V,? extends CharSequence> valueRepresentation)
Obfuscates a map.
|
<T> Obfuscated<T> |
obfuscateObject(T value)
Obfuscates a value.
|
<T> Obfuscated<T> |
obfuscateObject(T value,
Supplier<? extends CharSequence> representation)
Obfuscates a value.
|
<E> Set<E> |
obfuscateSet(Set<E> set)
Obfuscates a set.
|
<E> Set<E> |
obfuscateSet(Set<E> set,
Function<? super E,? extends CharSequence> elementRepresentation)
Obfuscates a set.
|
CharSequence |
obfuscateText(CharSequence s)
Obfuscates the contents of a
CharSequence . |
void |
obfuscateText(CharSequence s,
Appendable destination)
Obfuscates the contents of a
CharSequence . |
abstract CharSequence |
obfuscateText(CharSequence s,
int start,
int end)
Obfuscates parts of the contents of a
CharSequence . |
abstract void |
obfuscateText(CharSequence s,
int start,
int end,
Appendable destination)
Obfuscates parts of the contents of a
CharSequence . |
void |
obfuscateText(CharSequence s,
int start,
int end,
StringBuffer destination)
Obfuscates parts of the contents of a
CharSequence . |
void |
obfuscateText(CharSequence s,
int start,
int end,
StringBuilder destination)
Obfuscates parts of the contents of a
CharSequence . |
void |
obfuscateText(CharSequence s,
StringBuffer destination)
Obfuscates the contents of a
CharSequence . |
void |
obfuscateText(CharSequence s,
StringBuilder destination)
Obfuscates the contents of a
CharSequence . |
CharSequence |
obfuscateText(Reader input)
Obfuscates the contents of a
Reader . |
abstract void |
obfuscateText(Reader input,
Appendable destination)
Obfuscates the contents of a
Reader . |
static Obfuscator.PortionBuilder |
portion()
Returns a builder for obfuscators that obfuscate a specific portion of their input.
|
abstract Writer |
streamTo(Appendable destination)
Returns a writer that, when written to, will obfuscate its contents.
|
Obfuscator.Prefix |
untilLength(int prefixLength)
Creates a prefix that can be used to chain another obfuscator to this obfuscator.
|
public CharSequence obfuscateText(CharSequence s)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.NullPointerException
- If the given CharSequence
is null
.public abstract CharSequence obfuscateText(CharSequence s, int start, int end)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.start
- The index in the CharSequence
to start obfuscating, inclusive.end
- The index in the CharSequence
to end obfuscating, exclusive.NullPointerException
- If the given CharSequence
is null
.IndexOutOfBoundsException
- If the given start index is negative or larger than the given end index,
or if the given end index is larger than the given CharSequence
's length.public void obfuscateText(CharSequence s, StringBuilder destination)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.destination
- The StringBuilder
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or StringBuilder
is null
.public void obfuscateText(CharSequence s, int start, int end, StringBuilder destination)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.start
- The index in the CharSequence
to start obfuscating, inclusive.end
- The index in the CharSequence
to end obfuscating, exclusive.destination
- The StringBuilder
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or StringBuilder
is null
.IndexOutOfBoundsException
- If the given start index is negative or larger than the given end index,
or if the given end index is larger than the given CharSequence
's length.public void obfuscateText(CharSequence s, StringBuffer destination)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.destination
- The StringBuffer
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or StringBuffer
is null
.public void obfuscateText(CharSequence s, int start, int end, StringBuffer destination)
CharSequence
.s
- The CharSequence
with the contents to obfuscate.start
- The index in the CharSequence
to start obfuscating, inclusive.end
- The index in the CharSequence
to end obfuscating, exclusive.destination
- The StringBuffer
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or StringBuffer
is null
.IndexOutOfBoundsException
- If the given start index is negative or larger than the given end index,
or if the given end index is larger than the given CharSequence
's length.public void obfuscateText(CharSequence s, Appendable destination) throws IOException
CharSequence
.s
- The CharSequence
with the contents to obfuscate.destination
- The Appendable
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or Appendable
is null
.IOException
- If an I/O error occurs.public abstract void obfuscateText(CharSequence s, int start, int end, Appendable destination) throws IOException
CharSequence
.s
- The CharSequence
with the contents to obfuscate.start
- The index in the CharSequence
to start obfuscating, inclusive.end
- The index in the CharSequence
to end obfuscating, exclusive.destination
- The Appendable
to append the obfuscated contents to.NullPointerException
- If the given CharSequence
or Appendable
is null
.IndexOutOfBoundsException
- If the given start index is negative or larger than the given end index,
or if the given end index is larger than the given CharSequence
's length.IOException
- If an I/O error occurs.public CharSequence obfuscateText(Reader input) throws IOException
Reader
.input
- The Reader
with the contents to obfuscate.NullPointerException
- If the given Reader
is null
.IOException
- If an I/O error occurs.public abstract void obfuscateText(Reader input, Appendable destination) throws IOException
Reader
.input
- The Reader
with the contents to obfuscate.destination
- The Appendable
to append the obfuscated contents to.NullPointerException
- If the given Reader
or Appendable
is null
.IOException
- If an I/O error occurs.public final <T> Obfuscated<T> obfuscateObject(T value)
string representation
will be used to obfuscate the value.T
- The type of value to obfuscate.value
- The value to obfuscate.Obfuscated
wrapper around the given value.NullPointerException
- If the given value is null
.public final <T> Obfuscated<T> obfuscateObject(T value, Supplier<? extends CharSequence> representation)
T
- The type of value to obfuscate.value
- The value to obfuscate.representation
- A supplier for the character representation that will be used to obfuscate the value.
This can be used for values that don't have a sensible string representation
of their own.Obfuscated
wrapper around the given value.NullPointerException
- If the given value is or supplier is null
.public final <E> List<E> obfuscateList(List<E> list)
string representation
will be used to obfuscate the element.
The result will be a list that will behave exactly the same as the given list, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the list structure or the number of elements.
E
- The list's element type.list
- The list to obfuscate.NullPointerException
- If the given list is null
.public final <E> List<E> obfuscateList(List<E> list, Function<? super E,? extends CharSequence> elementRepresentation)
The result will be a list that will behave exactly the same as the given list, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the list structure or the number of elements.
E
- The list's element type.list
- The list to obfuscate.elementRepresentation
- The function to use to create the character representation for each element.NullPointerException
- If the given list or function is null
.public final <E> Set<E> obfuscateSet(Set<E> set)
string representation
will be used to obfuscate the element.
The result will be a set that will behave exactly the same as the given set, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the set structure or the number of elements.
E
- The set's element type.set
- The set to obfuscate.NullPointerException
- If the given set is null
.public final <E> Set<E> obfuscateSet(Set<E> set, Function<? super E,? extends CharSequence> elementRepresentation)
The result will be a set that will behave exactly the same as the given set, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the set structure or the number of elements.
E
- The set's element type.set
- The set to obfuscate.elementRepresentation
- The function to use to create the character representation for each element.NullPointerException
- If the given set or function is null
.public final <E> Collection<E> obfuscateCollection(Collection<E> collection)
string representation
will be used to obfuscate the element.
The result will be a collection that will behave exactly the same as the given collection, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the collection structure or the number of elements.
E
- The collection's element type.collection
- The collection to obfuscate.NullPointerException
- If the given collection is null
.public final <E> Collection<E> obfuscateCollection(Collection<E> collection, Function<? super E,? extends CharSequence> elementRepresentation)
The result will be a collection that will behave exactly the same as the given collection, except it will obfuscate each element when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the collection structure or the number of elements.
E
- The collection's element type.collection
- The collection to obfuscate.elementRepresentation
- The function to use to create the character representation for each element.NullPointerException
- If the given collection or function is null
.public final <K,V> Map<K,V> obfuscateMap(Map<K,V> map)
string representation
will be used to obfuscate the value.
The result will be a map that will behave exactly the same as the given map, except it will obfuscate each value when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the map structure or the number of entries.
K
- The map's key type.V
- The map's value type.map
- The map to obfuscate.NullPointerException
- If the given map is null
.public final <K,V> Map<K,V> obfuscateMap(Map<K,V> map, Function<? super V,? extends CharSequence> valueRepresentation)
The result will be a map that will behave exactly the same as the given map, except it will obfuscate each value when its
toString()
method is called. This is different from obfuscateObject(Object)
because it will not obfuscate
the map structure or the number of entries.
K
- The map's key type.V
- The map's value type.map
- The map to obfuscate.valueRepresentation
- The function to use to create the character representation for each value.NullPointerException
- If the given map or function is null
.public abstract Writer streamTo(Appendable destination)
Closeable
or AutoCloseable
.destination
- The destination to append the obfuscated contents to.public final Obfuscator.Prefix untilLength(int prefixLength)
portion()
to create an obfuscator that does not obfuscate the first
4 characters, then obfuscates at least 8 characters, then does not obfuscate up to 4 characters at the end. With this method it's
possible to do that by combining none()
and portion()
:
Obfuscator obfuscator = none().untilLength(4).then(portion()
.keepAtEnd(4)
.atLeastFromStart(8)
.build());
prefixLength
- The length of the part to use this obfuscator.IllegalArgumentException
- If the prefix length is not larger than all previous prefix lengths in a method chain.
In other words, each prefix length must be larger than its direct predecessor.public static final Obfuscator all()
*
.
The length of obfuscated contents will be as long as the length of the source.*
.public static final Obfuscator all(char maskChar)
maskChar
- The character to replace with.public static final Obfuscator none()
null
.public static Obfuscator fixedLength(int fixedLength)
*
.fixedLength
- The fixed length.*
.IllegalArgumentException
- If the given fixed length is negative.public static Obfuscator fixedLength(int fixedLength, char maskChar)
fixedLength
- The fixed length.maskChar
- The character to replace with.IllegalArgumentException
- If the given fixed length is negative.public static Obfuscator fixedValue(String fixedValue)
fixedValue
- The fixed value.NullPointerException
- If the given fixed value is null
.public static Obfuscator.PortionBuilder portion()
public static Obfuscator fromFunction(Function<? super CharSequence,? extends CharSequence> function)
This method differs from fromFunction(ObfuscatorFunction)
in the way sub sequences are treated. This method uses
CharSequence.subSequence(int, int)
, which may unnecessarily create new sub sequences. fromFunction(ObfuscatorFunction)
on the
other hand lets the function handle the sub sequencing.
Note: the function should never return null
. The returned obfuscator will throw a NullPointerException
if the function returns
null
when obfuscating text.
function
- The function to use.NullPointerException
- If the given function is null
.public static Obfuscator fromFunction(Obfuscator.ObfuscatorFunction function)
This method differs from fromFunction(Function)
in the way sub sequences are treated. fromFunction(Function)
uses
CharSequence.subSequence(int, int)
, which may unnecessarily create new sub sequences. This method on the other hand lets the function
handle the sub sequencing.
Note: the function should never return null
. The returned obfuscator will throw a NullPointerException
if the function returns
null
when obfuscating text.
function
- The function to use.NullPointerException
- If the given function is null
.Copyright © 2020–2023. All rights reserved.