This is an automated email from the ASF dual-hosted git repository. jking pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/thrift.git
The following commit(s) were added to refs/heads/master by this push: new 5abf58c THRIFT-4709: Use StandardCharsets UTF-8 5abf58c is described below commit 5abf58cf792466302d3c21d6b93ff50bb42d4c2f Author: Beluga Behr <dam6...@gmail.com> AuthorDate: Mon Dec 31 16:47:04 2018 -0500 THRIFT-4709: Use StandardCharsets UTF-8 --- .../org/apache/thrift/TByteArrayOutputStream.java | 5 ++ .../apache/thrift/protocol/TBinaryProtocol.java | 33 +++------ .../apache/thrift/protocol/TCompactProtocol.java | 27 +++---- .../org/apache/thrift/protocol/TJSONProtocol.java | 82 +++++++--------------- .../thrift/protocol/TSimpleJSONProtocol.java | 20 ++---- .../org/apache/thrift/transport/TMemoryBuffer.java | 10 +-- .../thrift/transport/TSaslClientTransport.java | 9 +-- .../thrift/transport/TSaslServerTransport.java | 10 +-- .../apache/thrift/transport/TSaslTransport.java | 12 ++-- lib/java/test/org/apache/thrift/Fixtures.java | 3 +- .../apache/thrift/protocol/TestTJSONProtocol.java | 6 +- .../thrift/protocol/TestTSimpleJSONProtocol.java | 8 +-- .../thrift/transport/TestTSaslTransports.java | 15 ++-- 13 files changed, 83 insertions(+), 157 deletions(-) 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 class TByteArrayOutputStream extends ByteArrayOutputStream { 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 class TBinaryProtocol extends TProtocol { } 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 @@ public class TBinaryProtocol extends TProtocol { 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 class TBinaryProtocol extends TProtocol { 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 @@ package org.apache.thrift.protocol; 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 @@ public class TCompactProtocol extends TProtocol { * 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 @@ public class TCompactProtocol extends TProtocol { 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 @@ public class TJSONProtocol extends TProtocol { 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 @@ public class TJSONProtocol extends TProtocol { 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 @@ public class TJSONProtocol extends TProtocol { 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 @@ public class TJSONProtocol extends TProtocol { @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 @@ public class TJSONProtocol extends TProtocol { } 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 @@ public class TJSONProtocol extends TProtocol { 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; - } - catch (UnsupportedEncodingException ex) { - throw new TException("JVM DOES NOT SUPPORT UTF-8"); + 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"); } + return dub; } else { if (context_.escapeNum()) { @@ -868,13 +845,7 @@ public class TJSONProtocol extends TProtocol { 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 @@ public class TJSONProtocol extends TProtocol { @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 class TSimpleJSONProtocol extends TProtocol { } 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 class TSimpleJSONProtocol extends TProtocol { } 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 @@ public class TMemoryBuffer extends TTransport { * 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 @@ public class TMemoryBuffer extends TTransport { /** * 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 @@ public class TSaslClientTransport extends TSaslTransport { 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.Sasl; 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 @@ public class TSaslServerTransport extends TSaslTransport { } // 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 @@ abstract class TSaslTransport extends TTransport { 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 @@ abstract class TSaslTransport extends TTransport { */ 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 @@ public class Fixtures { 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 @@ public class TestTJSONProtocol extends ProtocolTestBase { 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 @@ public class TestTSimpleJSONProtocol extends TestCase { } 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 @@ public class TestTSaslTransports extends TestCase { 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 @@ public class TestTSaslTransports extends TestCase { 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; }