Skip to content

Commit

Permalink
perf: Enhance Encoding constructor to allow skipping of ASCII number …
Browse files Browse the repository at this point in the history
…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 committed Mar 13, 2019
1 parent d059693 commit 2ce66ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 16 additions & 4 deletions pgjdbc/src/main/java/org/postgresql/core/Encoding.java
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;
this.fastASCIINumbers = testAsciiNumbers(encoding);
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
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/core/UTF8Encoding.java
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

0 comments on commit 2ce66ad

Please sign in to comment.