Skip to content

Commit

Permalink
Cleanup encoding (#1441)
Browse files Browse the repository at this point in the history
* 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 73ec817
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
28 changes: 19 additions & 9 deletions pgjdbc/src/main/java/org/postgresql/core/Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/core/UTF8Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class UTF8Encoding extends Encoding {

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

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

// helper for decode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ObjectFactory {
*
* @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
Expand Down

0 comments on commit 73ec817

Please sign in to comment.