public class PercentCodec extends java.lang.Object implements BinaryEncoder, BinaryDecoder
This class is immutable. It is also thread-safe besides using BitSet which is not thread-safe, but its public interface only call the access
Modifier and Type | Field and Description |
---|---|
private java.util.BitSet |
alwaysEncodeChars
The bit set used to store the character that should be always encoded
|
private int |
alwaysEncodeCharsMax
The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
|
private int |
alwaysEncodeCharsMin
The minimum and maximum code of the bytes that is inserted in the bit set, used to prevent look-ups
|
private static byte |
ESCAPE_CHAR
The escape character used by the Percent-Encoding in order to introduce an encoded character.
|
private boolean |
plusForSpace
The flag defining if the space character should be encoded as '+'
|
Constructor and Description |
---|
PercentCodec()
Constructs a Percent coded that will encode all the non US-ASCII characters using the Percent-Encoding
while it will not encode all the US-ASCII characters, except for character '%' that is used as escape
character for Percent-Encoding.
|
PercentCodec(byte[] alwaysEncodeChars,
boolean plusForSpace)
Constructs a Percent codec by specifying the characters that belong to US-ASCII that should
always be encoded.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
canEncode(byte c) |
private boolean |
containsSpace(byte[] bytes) |
byte[] |
decode(byte[] bytes)
Decode bytes encoded with Percent-Encoding based on RFC 3986.
|
java.lang.Object |
decode(java.lang.Object obj)
Decodes a byte[] Object, whose bytes are encoded with Percent-Encoding.
|
private byte[] |
doEncode(byte[] bytes,
int expectedLength,
boolean willEncode) |
byte[] |
encode(byte[] bytes)
Percent-Encoding based on RFC 3986.
|
java.lang.Object |
encode(java.lang.Object obj)
Encodes an object into using the Percent-Encoding.
|
private int |
expectedDecodingBytes(byte[] bytes) |
private int |
expectedEncodingBytes(byte[] bytes) |
private boolean |
inAlwaysEncodeCharsRange(byte c) |
private void |
insertAlwaysEncodeChar(byte b)
Inserts a single character into a BitSet and maintains the min and max of the characters of the
BitSet alwaysEncodeChars in order to avoid look-ups when a byte is out of this range. |
private void |
insertAlwaysEncodeChars(byte[] alwaysEncodeCharsArray)
Adds the byte array into a BitSet for faster lookup
|
private boolean |
isAsciiChar(byte c) |
private static final byte ESCAPE_CHAR
private final java.util.BitSet alwaysEncodeChars
private final boolean plusForSpace
private int alwaysEncodeCharsMin
private int alwaysEncodeCharsMax
public PercentCodec()
public PercentCodec(byte[] alwaysEncodeChars, boolean plusForSpace)
alwaysEncodeChars
- the unsafe characters that should always be encodedplusForSpace
- the flag defining if the space character should be encoded as '+'private void insertAlwaysEncodeChars(byte[] alwaysEncodeCharsArray)
alwaysEncodeCharsArray
- private void insertAlwaysEncodeChar(byte b)
BitSet alwaysEncodeChars
in order to avoid look-ups when a byte is out of this range.b
- the byte that is candidate for min and max limitpublic byte[] encode(byte[] bytes) throws EncoderException
encode
in interface BinaryEncoder
bytes
- Data to be encodedEncoderException
- thrown if the Encoder encounters a failure condition during the encoding process.private byte[] doEncode(byte[] bytes, int expectedLength, boolean willEncode)
private int expectedEncodingBytes(byte[] bytes)
private boolean containsSpace(byte[] bytes)
private boolean canEncode(byte c)
private boolean inAlwaysEncodeCharsRange(byte c)
private boolean isAsciiChar(byte c)
public byte[] decode(byte[] bytes) throws DecoderException
decode
in interface BinaryDecoder
bytes
- A byte array which has been encoded with the appropriate encoderDecoderException
- A decoder exception is thrown if a Decoder encounters a failure condition during the decode process.private int expectedDecodingBytes(byte[] bytes)
public java.lang.Object encode(java.lang.Object obj) throws EncoderException
encode
in interface Encoder
obj
- the object to encodeEncoderException
- if the object is not a byte arraypublic java.lang.Object decode(java.lang.Object obj) throws DecoderException
decode
in interface Decoder
obj
- the object to decodeDecoderException
- if the object is not a byte array