Skip to content
Permalink
Browse files

Cleanup encoding (#1441)

* fix: Correct typo weather to whether

* misc: Change internal Encoding.testAsciiNumbers(...) to be static

* perf: Enhance Encoding constructor to allow skipping of ASCII number compatibility test

Adds a two parameter constructor to Encoding to allow sub classes to specify whether their
known ASCII compatability so as to skip testing. The only usage of it is the UTF8Encoding
which is changed to use the new constructor.
  • Loading branch information...
sehrope authored and davecramer committed Apr 9, 2019
1 parent 31bc6e5 commit 73ec817311857756164b1933ce2dfa8127f8dffd
@@ -87,22 +87,34 @@ private Encoding() {
}

/**
* Use the charset passed as parameter.
* Subclasses may use this constructor if they know in advance of their ASCII number
* compatibility.
*
* @param encoding charset name to use
* @param fastASCIINumbers whether this encoding is compatible with ASCII numbers.
*/
protected Encoding(String encoding) {
protected Encoding(String encoding, boolean fastASCIINumbers) {
if (encoding == null) {
throw new NullPointerException("Null encoding charset not supported");
}
this.encoding = encoding;
fastASCIINumbers = testAsciiNumbers();
this.fastASCIINumbers = fastASCIINumbers;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Creating new Encoding {0} with fastASCIINumbers {1}",
new Object[]{encoding, fastASCIINumbers});
}
}

/**
* Use the charset passed as parameter and tests at creation time whether the specified encoding
* is compatible with ASCII numbers.
*
* @param encoding charset name to use
*/
protected Encoding(String encoding) {
this(encoding, testAsciiNumbers(encoding));
}

/**
* Returns true if this encoding has characters '-' and '0'..'9' in exactly same posision as
* ascii.
@@ -122,7 +134,7 @@ public boolean hasAsciiNumbers() {
*/
public static Encoding getJVMEncoding(String jvmEncoding) {
if ("UTF-8".equals(jvmEncoding)) {
return new UTF8Encoding(jvmEncoding);
return new UTF8Encoding();
}
if (Charset.isSupported(jvmEncoding)) {
return new Encoding(jvmEncoding);
@@ -251,25 +263,23 @@ public String toString() {
}

/**
* Checks weather this encoding is compatible with ASCII for the number characters '-' and
* Checks whether this encoding is compatible with ASCII for the number characters '-' and
* '0'..'9'. Where compatible means that they are encoded with exactly same values.
*
* @return If faster ASCII number parsing can be used with this encoding.
*/
private boolean testAsciiNumbers() {
private static boolean testAsciiNumbers(String encoding) {
// TODO: test all postgres supported encoding to see if there are
// any which do _not_ have ascii numbers in same location
// at least all the encoding listed in the encodings hashmap have
// working ascii numbers
try {
String test = "-0123456789";
byte[] bytes = encode(test);
byte[] bytes = test.getBytes(encoding);
String res = new String(bytes, "US-ASCII");
return test.equals(res);
} catch (java.io.UnsupportedEncodingException e) {
return false;
} catch (IOException e) {
return false;
}
}
}
@@ -17,8 +17,8 @@

private char[] decoderArray = new char[1024];

UTF8Encoding(String jvmEncoding) {
super(jvmEncoding);
UTF8Encoding() {
super("UTF-8", true);
}

// helper for decode
@@ -23,7 +23,7 @@
*
* @param classname name of the class to instantiate
* @param info parameter to pass as Properties
* @param tryString weather to look for a single String argument constructor
* @param tryString whether to look for a single String argument constructor
* @param stringarg parameter to pass as String
* @return the instantiated class
* @throws ClassNotFoundException if something goes wrong

0 comments on commit 73ec817

Please sign in to comment.
You can’t perform that action at this time.