THRIFT-4709: Use StandardCharsets UTF-8
diff --git a/lib/java/src/org/apache/thrift/TByteArrayOutputStream.java b/lib/java/src/org/apache/thrift/TByteArrayOutputStream.java
index 1c37ecd..3a2d56c 100644
--- a/lib/java/src/org/apache/thrift/TByteArrayOutputStream.java
+++ b/lib/java/src/org/apache/thrift/TByteArrayOutputStream.java
@@ -20,6 +20,7 @@
package org.apache.thrift;
import java.io.ByteArrayOutputStream;
+import java.nio.charset.Charset;
/**
* Class that allows access to the underlying buf without doing deep
@@ -53,4 +54,8 @@
public int len() {
return count;
}
+
+ public String toString(Charset charset) {
+ return new String(buf, 0, count, charset);
+ }
}
diff --git a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
index aaa1fd8..563128c 100644
--- a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -19,8 +19,8 @@
package org.apache.thrift.protocol;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransport;
@@ -202,13 +202,9 @@
}
public void writeString(String str) throws TException {
- try {
- byte[] dat = str.getBytes("UTF-8");
- writeI32(dat.length);
- trans_.write(dat, 0, dat.length);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] dat = str.getBytes(StandardCharsets.UTF_8);
+ writeI32(dat.length);
+ trans_.write(dat, 0, dat.length);
}
public void writeBinary(ByteBuffer bin) throws TException {
@@ -360,13 +356,10 @@
checkStringReadLength(size);
if (trans_.getBytesRemainingInBuffer() >= size) {
- try {
- String s = new String(trans_.getBuffer(), trans_.getBufferPosition(), size, "UTF-8");
- trans_.consumeBuffer(size);
- return s;
- } catch (UnsupportedEncodingException e) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ String s = new String(trans_.getBuffer(), trans_.getBufferPosition(),
+ size, StandardCharsets.UTF_8);
+ trans_.consumeBuffer(size);
+ return s;
}
return readStringBody(size);
@@ -374,13 +367,9 @@
public String readStringBody(int size) throws TException {
checkStringReadLength(size);
- try {
- byte[] buf = new byte[size];
- trans_.readAll(buf, 0, size);
- return new String(buf, "UTF-8");
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] buf = new byte[size];
+ trans_.readAll(buf, 0, size);
+ return new String(buf, StandardCharsets.UTF_8);
}
public ByteBuffer readBinary() throws TException {
diff --git a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
index 56c349a..92f186e 100644
--- a/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TCompactProtocol.java
@@ -22,6 +22,7 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import org.apache.thrift.ShortStack;
import org.apache.thrift.TException;
@@ -359,12 +360,8 @@
* Write a string to the wire with a varint size preceding.
*/
public void writeString(String str) throws TException {
- try {
- byte[] bytes = str.getBytes("UTF-8");
- writeBinary(bytes, 0, bytes.length);
- } catch (UnsupportedEncodingException e) {
- throw new TException("UTF-8 not supported!");
- }
+ byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
+ writeBinary(bytes, 0, bytes.length);
}
/**
@@ -680,17 +677,15 @@
return "";
}
- try {
- if (trans_.getBytesRemainingInBuffer() >= length) {
- String str = new String(trans_.getBuffer(), trans_.getBufferPosition(), length, "UTF-8");
- trans_.consumeBuffer(length);
- return str;
- } else {
- return new String(readBinary(length), "UTF-8");
- }
- } catch (UnsupportedEncodingException e) {
- throw new TException("UTF-8 not supported!");
+ final String str;
+ if (trans_.getBytesRemainingInBuffer() >= length) {
+ str = new String(trans_.getBuffer(), trans_.getBufferPosition(),
+ length, StandardCharsets.UTF_8);
+ trans_.consumeBuffer(length);
+ } else {
+ str = new String(readBinary(length), StandardCharsets.UTF_8);
}
+ return str;
}
/**
diff --git a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
index fd54fdf..d37c493 100644
--- a/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java
@@ -20,8 +20,8 @@
package org.apache.thrift.protocol;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Stack;
@@ -418,12 +418,8 @@
if (escapeNum) {
trans_.write(QUOTE);
}
- try {
- byte[] buf = str.getBytes("UTF-8");
- trans_.write(buf);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] buf = str.getBytes(StandardCharsets.UTF_8);
+ trans_.write(buf);
if (escapeNum) {
trans_.write(QUOTE);
}
@@ -453,12 +449,8 @@
if (escapeNum) {
trans_.write(QUOTE);
}
- try {
- byte[] b = str.getBytes("UTF-8");
- trans_.write(b, 0, b.length);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] b = str.getBytes(StandardCharsets.UTF_8);
+ trans_.write(b, 0, b.length);
if (escapeNum) {
trans_.write(QUOTE);
}
@@ -513,12 +505,8 @@
resetContext(); // THRIFT-3743
writeJSONArrayStart();
writeJSONInteger(VERSION);
- try {
- byte[] b = message.name.getBytes("UTF-8");
- writeJSONString(b);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] b = message.name.getBytes(StandardCharsets.UTF_8);
+ writeJSONString(b);
writeJSONInteger(message.type);
writeJSONInteger(message.seqid);
}
@@ -628,12 +616,8 @@
@Override
public void writeString(String str) throws TException {
- try {
- byte[] b = str.getBytes("UTF-8");
- writeJSONString(b);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] b = str.getBytes(StandardCharsets.UTF_8);
+ writeJSONString(b);
}
@Override
@@ -684,19 +668,17 @@
}
codeunits.add((char)cu);
- arr.write((new String(new int[] { codeunits.get(0), codeunits.get(1) }, 0, 2)).getBytes("UTF-8"));
+ arr.write(
+ (new String(new int[] { codeunits.get(0), codeunits.get(1) },
+ 0, 2)).getBytes(StandardCharsets.UTF_8));
codeunits.clear();
}
else {
- arr.write((new String(new int[] { cu }, 0, 1)).getBytes("UTF-8"));
+ arr.write((new String(new int[] { cu }, 0, 1))
+ .getBytes(StandardCharsets.UTF_8));
}
continue;
- }
- catch (UnsupportedEncodingException ex) {
- throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED,
- "JVM does not support UTF-8");
- }
- catch (IOException ex) {
+ } catch (IOException ex) {
throw new TProtocolException(TProtocolException.INVALID_DATA,
"Invalid unicode sequence");
}
@@ -777,19 +759,14 @@
context_.read();
if (reader_.peek() == QUOTE[0]) {
TByteArrayOutputStream arr = readJSONString(true);
- try {
- double dub = Double.valueOf(arr.toString("UTF-8"));
- if (!context_.escapeNum() && !Double.isNaN(dub) &&
- !Double.isInfinite(dub)) {
- // Throw exception -- we should not be in a string in this case
- throw new TProtocolException(TProtocolException.INVALID_DATA,
- "Numeric data unexpectedly quoted");
- }
- return dub;
+ double dub = Double.valueOf(arr.toString(StandardCharsets.UTF_8));
+ if (!context_.escapeNum() && !Double.isNaN(dub)
+ && !Double.isInfinite(dub)) {
+ // Throw exception -- we should not be in a string in this case
+ throw new TProtocolException(TProtocolException.INVALID_DATA,
+ "Numeric data unexpectedly quoted");
}
- catch (UnsupportedEncodingException ex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ return dub;
}
else {
if (context_.escapeNum()) {
@@ -868,13 +845,7 @@
throw new TProtocolException(TProtocolException.BAD_VERSION,
"Message contained bad version.");
}
- String name;
- try {
- name = readJSONString(false).toString("UTF-8");
- }
- catch (UnsupportedEncodingException ex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ String name = readJSONString(false).toString(StandardCharsets.UTF_8);
byte type = (byte) readJSONInteger();
int seqid = (int) readJSONInteger();
return new TMessage(name, type, seqid);
@@ -991,12 +962,7 @@
@Override
public String readString() throws TException {
- try {
- return readJSONString(false).toString("UTF-8");
- }
- catch (UnsupportedEncodingException ex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ return readJSONString(false).toString(StandardCharsets.UTF_8);
}
@Override
diff --git a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
index b24e421..e7e8d46 100644
--- a/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TSimpleJSONProtocol.java
@@ -19,8 +19,8 @@
package org.apache.thrift.protocol;
-import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.Stack;
import org.apache.thrift.TException;
@@ -262,12 +262,8 @@
}
public void _writeStringData(String s) throws TException {
- try {
- byte[] b = s.getBytes("UTF-8");
- trans_.write(b);
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ byte[] b = s.getBytes(StandardCharsets.UTF_8);
+ trans_.write(b);
}
public void writeI64(long i64) throws TException {
@@ -342,12 +338,10 @@
}
public void writeBinary(ByteBuffer bin) throws TException {
- try {
- // TODO(mcslee): Fix this
- writeString(new String(bin.array(), bin.position() + bin.arrayOffset(), bin.limit() - bin.position() - bin.arrayOffset(), "UTF-8"));
- } catch (UnsupportedEncodingException uex) {
- throw new TException("JVM DOES NOT SUPPORT UTF-8");
- }
+ // TODO(mcslee): Fix this
+ writeString(new String(bin.array(), bin.position() + bin.arrayOffset(),
+ bin.limit() - bin.position() - bin.arrayOffset(),
+ StandardCharsets.UTF_8));
}
/**
diff --git a/lib/java/src/org/apache/thrift/transport/TMemoryBuffer.java b/lib/java/src/org/apache/thrift/transport/TMemoryBuffer.java
index ef5f5c2..b19ac86 100644
--- a/lib/java/src/org/apache/thrift/transport/TMemoryBuffer.java
+++ b/lib/java/src/org/apache/thrift/transport/TMemoryBuffer.java
@@ -20,7 +20,7 @@
package org.apache.thrift.transport;
import org.apache.thrift.TByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
/**
* Memory buffer-based implementation of the TTransport interface.
@@ -30,6 +30,8 @@
* Create a TMemoryBuffer with an initial buffer size of <i>size</i>. The
* internal buffer will grow as necessary to accommodate the size of the data
* being written to it.
+ *
+ * @param size the initial size of the buffer
*/
public TMemoryBuffer(int size) {
arr_ = new TByteArrayOutputStream(size);
@@ -69,11 +71,11 @@
/**
* Output the contents of the memory buffer as a String, using the supplied
* encoding
- * @param enc the encoding to use
+ * @param charset the encoding to use
* @return the contents of the memory buffer as a String
*/
- public String toString(String enc) throws UnsupportedEncodingException {
- return arr_.toString(enc);
+ public String toString(Charset charset) {
+ return arr_.toString(charset);
}
public String inspect() {
diff --git a/lib/java/src/org/apache/thrift/transport/TSaslClientTransport.java b/lib/java/src/org/apache/thrift/transport/TSaslClientTransport.java
index 8122289..4b1ca0a 100644
--- a/lib/java/src/org/apache/thrift/transport/TSaslClientTransport.java
+++ b/lib/java/src/org/apache/thrift/transport/TSaslClientTransport.java
@@ -19,7 +19,7 @@
package org.apache.thrift.transport;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.security.auth.callback.CallbackHandler;
@@ -97,12 +97,7 @@
LOGGER.debug("Sending mechanism name {} and initial response of length {}", mechanism,
initialResponse.length);
- byte[] mechanismBytes;
- try {
- mechanismBytes = mechanism.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new TTransportException(e);
- }
+ byte[] mechanismBytes = mechanism.getBytes(StandardCharsets.UTF_8);
sendSaslMessage(NegotiationStatus.START,
mechanismBytes);
// Send initial response
diff --git a/lib/java/src/org/apache/thrift/transport/TSaslServerTransport.java b/lib/java/src/org/apache/thrift/transport/TSaslServerTransport.java
index e6c0e3e..39b81ca 100644
--- a/lib/java/src/org/apache/thrift/transport/TSaslServerTransport.java
+++ b/lib/java/src/org/apache/thrift/transport/TSaslServerTransport.java
@@ -19,8 +19,8 @@
package org.apache.thrift.transport;
-import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
+import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -31,7 +31,6 @@
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;
-import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -132,12 +131,7 @@
}
// Get the mechanism name.
- String mechanismName;
- try {
- mechanismName = new String(message.payload, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new TTransportException("JVM DOES NOT SUPPORT UTF-8");
- }
+ String mechanismName = new String(message.payload, StandardCharsets.UTF_8);
TSaslServerDefinition serverDefinition = serverDefinitionMap.get(mechanismName);
LOGGER.debug("Received mechanism name '{}'", mechanismName);
diff --git a/lib/java/src/org/apache/thrift/transport/TSaslTransport.java b/lib/java/src/org/apache/thrift/transport/TSaslTransport.java
index bbd3f9a..80f3557 100644
--- a/lib/java/src/org/apache/thrift/transport/TSaslTransport.java
+++ b/lib/java/src/org/apache/thrift/transport/TSaslTransport.java
@@ -19,7 +19,7 @@
package org.apache.thrift.transport;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@@ -194,12 +194,8 @@
underlyingTransport.readAll(payload, 0, payload.length);
if (status == NegotiationStatus.BAD || status == NegotiationStatus.ERROR) {
- try {
- String remoteMessage = new String(payload, "UTF-8");
- throw new TTransportException("Peer indicated failure: " + remoteMessage);
- } catch (UnsupportedEncodingException e) {
- throw new TTransportException(e);
- }
+ String remoteMessage = new String(payload, StandardCharsets.UTF_8);
+ throw new TTransportException("Peer indicated failure: " + remoteMessage);
}
if (LOGGER.isDebugEnabled())
@@ -224,7 +220,7 @@
*/
protected TTransportException sendAndThrowMessage(NegotiationStatus status, String message) throws TTransportException {
try {
- sendSaslMessage(status, message.getBytes("UTF-8"));
+ sendSaslMessage(status, message.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
LOGGER.warn("Could not send failure response", e);
message += "\nAlso, could not send response: " + e.toString();
diff --git a/lib/java/test/org/apache/thrift/Fixtures.java b/lib/java/test/org/apache/thrift/Fixtures.java
index 81671d8..61f40a5 100644
--- a/lib/java/test/org/apache/thrift/Fixtures.java
+++ b/lib/java/test/org/apache/thrift/Fixtures.java
@@ -20,6 +20,7 @@
package org.apache.thrift;
import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -267,7 +268,7 @@
oneOfEach.setInteger64((long) 6000 * 1000 * 1000);
oneOfEach.setDouble_precision(Math.PI);
oneOfEach.setSome_characters("JSON THIS! \"\1");
- oneOfEach.setZomg_unicode(new String(kUnicodeBytes, "UTF-8"));
+ oneOfEach.setZomg_unicode(new String(kUnicodeBytes, StandardCharsets.UTF_8));
oneOfEach.setBase64(ByteBuffer.wrap("base64".getBytes()));
// byte, i16, and i64 lists are populated by default constructor
diff --git a/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
index 1320749..c2ca1fa 100644
--- a/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
+++ b/lib/java/test/org/apache/thrift/protocol/TestTJSONProtocol.java
@@ -18,7 +18,7 @@
*/
package org.apache.thrift.protocol;
-import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TJSONProtocol;
@@ -35,13 +35,13 @@
return false;
}
- public void testEscapedUnicode() throws TException, IOException {
+ public void testEscapedUnicode() throws TException {
String jsonString = "\"hello unicode \\u0e01\\ud834\\udd1e world\"";
String expectedString = "hello unicode \u0e01\ud834\udd1e world";
TMemoryBuffer buffer = new TMemoryBuffer(1000);
TJSONProtocol protocol = new TJSONProtocol(buffer);
- buffer.write(jsonString.getBytes("UTF-8"));
+ buffer.write(jsonString.getBytes(StandardCharsets.UTF_8));
assertEquals(expectedString, protocol.readString());
}
diff --git a/lib/java/test/org/apache/thrift/protocol/TestTSimpleJSONProtocol.java b/lib/java/test/org/apache/thrift/protocol/TestTSimpleJSONProtocol.java
index b8c4657..9d125b1 100644
--- a/lib/java/test/org/apache/thrift/protocol/TestTSimpleJSONProtocol.java
+++ b/lib/java/test/org/apache/thrift/protocol/TestTSimpleJSONProtocol.java
@@ -18,7 +18,7 @@
*/
package org.apache.thrift.protocol;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;
@@ -40,11 +40,7 @@
}
private String bufToString() {
- try {
- return buf.toString("UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ return buf.toString(StandardCharsets.UTF_8);
}
public void testHolyMoley() throws TException {
diff --git a/lib/java/test/org/apache/thrift/transport/TestTSaslTransports.java b/lib/java/test/org/apache/thrift/transport/TestTSaslTransports.java
index 788395f..36a06e9 100644
--- a/lib/java/test/org/apache/thrift/transport/TestTSaslTransports.java
+++ b/lib/java/test/org/apache/thrift/transport/TestTSaslTransports.java
@@ -20,6 +20,7 @@
package org.apache.thrift.transport;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@@ -332,12 +333,8 @@
throw new SaslException("Already complete!");
}
- try {
- hasProvidedInitialResponse = true;
- return username.getBytes("UTF-8");
- } catch (IOException e) {
- throw new SaslException(e.toString());
- }
+ hasProvidedInitialResponse = true;
+ return username.getBytes(StandardCharsets.UTF_8);
}
public boolean isComplete() { return hasProvidedInitialResponse; }
public byte[] unwrap(byte[] incoming, int offset, int len) {
@@ -354,11 +351,7 @@
private String user;
public String getMechanismName() { return "ANONYMOUS"; }
public byte[] evaluateResponse(byte[] response) throws SaslException {
- try {
- this.user = new String(response, "UTF-8");
- } catch (IOException e) {
- throw new SaslException(e.toString());
- }
+ this.user = new String(response, StandardCharsets.UTF_8);
return null;
}
public boolean isComplete() { return user != null; }