Skip to content

Commit

Permalink
AVRO-2336: Use Java Standard Charsets - Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
belugabehr authored and iemejia committed Mar 28, 2019
1 parent 8197803 commit c552c17
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 86 deletions.
5 changes: 3 additions & 2 deletions lang/java/avro/src/main/java/org/apache/avro/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
Expand Down Expand Up @@ -450,7 +451,7 @@ void toJson(JsonGenerator gen) throws IOException {
public byte[] getMD5() {
if (md5 == null)
try {
md5 = MessageDigest.getInstance("MD5").digest(this.toString().getBytes("UTF-8"));
md5 = MessageDigest.getInstance("MD5").digest(this.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new AvroRuntimeException(e);
}
Expand Down Expand Up @@ -478,7 +479,7 @@ public static Protocol parse(String string, String... more) {
/** Read a protocol from a Json string. */
public static Protocol parse(String string) {
try {
return parse(Schema.FACTORY.createParser(new ByteArrayInputStream(string.getBytes("UTF-8"))));
return parse(Schema.FACTORY.createParser(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8))));
} catch (IOException e) {
throw new AvroRuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -2708,11 +2709,11 @@ private static JsonNode toJsonNode(Object o) {
byte[] data = new byte[bytes.remaining()];
bytes.get(data);
bytes.reset(); // put the buffer back the way we got it
s = new String(data, "ISO-8859-1");
s = new String(data, StandardCharsets.ISO_8859_1);
char[] quoted = BufferRecyclers.getJsonStringEncoder().quoteAsString(s);
s = "\"" + new String(quoted) + "\"";
} else if (o instanceof byte[]) {
s = new String((byte[]) o, "ISO-8859-1");
s = new String((byte[]) o, StandardCharsets.ISO_8859_1);
char[] quoted = BufferRecyclers.getJsonStringEncoder().quoteAsString(s);
s = '\"' + new String(quoted) + '\"';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
Expand Down Expand Up @@ -222,7 +223,7 @@ public DataFileWriter<D> appendTo(SeekableInput in, OutputStream out) throws IOE
this.meta.putAll(reader.getHeader().meta);
byte[] codecBytes = this.meta.get(DataFileConstants.CODEC);
if (codecBytes != null) {
String strCodec = new String(codecBytes, "UTF-8");
String strCodec = new String(codecBytes, StandardCharsets.UTF_8);
this.codec = CodecFactory.fromString(strCodec).createInstance();
} else {
this.codec = CodecFactory.nullCodec().createInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.apache.avro.util.Utf8;

Expand Down Expand Up @@ -51,7 +52,7 @@ public void writeString(String string) throws IOException {
writeZero();
return;
}
byte[] bytes = string.getBytes("UTF-8");
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
writeInt(bytes.length);
writeFixed(bytes, 0, bytes.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -62,8 +63,6 @@ private static class ReorderBuffer {
public JsonParser origParser = null;
}

static final String CHARSET = "ISO-8859-1";

private JsonDecoder(Symbol root, InputStream in) throws IOException {
super(root);
configure(in);
Expand Down Expand Up @@ -266,7 +265,7 @@ public ByteBuffer readBytes(ByteBuffer old) throws IOException {
}

private byte[] readByteArray() throws IOException {
byte[] result = in.getText().getBytes(CHARSET);
byte[] result = in.getText().getBytes(StandardCharsets.ISO_8859_1);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;

import org.apache.avro.AvroTypeException;
Expand Down Expand Up @@ -206,7 +207,7 @@ public void writeBytes(byte[] bytes, int start, int len) throws IOException {
}

private void writeByteArray(byte[] bytes, int start, int len) throws IOException {
out.writeString(new String(bytes, start, len, JsonDecoder.CHARSET));
out.writeString(new String(bytes, start, len, StandardCharsets.ISO_8859_1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.avro.AvroTypeException;
import org.apache.avro.Schema;
Expand Down Expand Up @@ -210,13 +210,11 @@ public Utf8 readString(Utf8 old) throws IOException {
}
}

private static final Charset UTF8 = Charset.forName("UTF-8");

@Override
public String readString() throws IOException {
Symbol actual = parser.advance(Symbol.STRING);
if (actual == Symbol.BYTES) {
return new String(in.readBytes(null).array(), UTF8);
return new String(in.readBytes(null).array(), StandardCharsets.UTF_8);
} else {
assert actual == Symbol.STRING;
return in.readString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -444,7 +445,7 @@ static void encode(Encoder e, Schema s, JsonNode n) throws IOException {
case FIXED:
if (!n.isTextual())
throw new AvroTypeException("Non-string default value for fixed: " + n);
byte[] bb = n.textValue().getBytes("ISO-8859-1");
byte[] bb = n.textValue().getBytes(StandardCharsets.ISO_8859_1);
if (bb.length != s.getFixedSize()) {
bb = Arrays.copyOf(bb, s.getFixedSize());
}
Expand All @@ -458,7 +459,7 @@ static void encode(Encoder e, Schema s, JsonNode n) throws IOException {
case BYTES:
if (!n.isTextual())
throw new AvroTypeException("Non-string default value for bytes: " + n);
e.writeBytes(n.textValue().getBytes("ISO-8859-1"));
e.writeBytes(n.textValue().getBytes(StandardCharsets.ISO_8859_1));
break;
case INT:
if (!n.isNumber())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testSingleSubRecordExtraField() throws IOException {
// this field should be safely ignored
" \"extraField\":\"extraValue\"\n" + " },\n" + " \"parentField2\":\"parentValue2\"\n" + "}";

final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputAsExpected.getBytes());
final ByteArrayInputStream inputStream = new ByteArrayInputStream(inputAsExpected.getBytes(UTF_8));

final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(parent, inputStream);
final DatumReader<Object> reader = new GenericDatumReader<>(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;

Expand Down Expand Up @@ -268,7 +269,7 @@ public void asciiStringWrittenWithUnionSchemaIsConvertedToBytesSchema() throws E
Record record = defaultRecordWithSchema(writer, FIELD_A, "42");
byte[] encoded = encodeGenericBlob(record);
ByteBuffer actual = (ByteBuffer) decodeGenericBlob(BYTES_RECORD, writer, encoded).get(FIELD_A);
assertArrayEquals("42".getBytes("UTF-8"), actual.array());
assertArrayEquals("42".getBytes(StandardCharsets.UTF_8), actual.array());
}

@Test
Expand All @@ -278,13 +279,13 @@ public void utf8StringWrittenWithUnionSchemaIsConvertedToBytesSchema() throws Ex
Record record = defaultRecordWithSchema(writer, FIELD_A, goeran);
byte[] encoded = encodeGenericBlob(record);
ByteBuffer actual = (ByteBuffer) decodeGenericBlob(BYTES_RECORD, writer, encoded).get(FIELD_A);
assertArrayEquals(goeran.getBytes("UTF-8"), actual.array());
assertArrayEquals(goeran.getBytes(StandardCharsets.UTF_8), actual.array());
}

@Test
public void asciiBytesWrittenWithUnionSchemaIsConvertedToStringSchema() throws Exception {
Schema writer = UNION_STRING_BYTES_RECORD;
ByteBuffer buf = ByteBuffer.wrap("42".getBytes("UTF-8"));
ByteBuffer buf = ByteBuffer.wrap("42".getBytes(StandardCharsets.UTF_8));
Record record = defaultRecordWithSchema(writer, FIELD_A, buf);
byte[] encoded = encodeGenericBlob(record);
CharSequence read = (CharSequence) decodeGenericBlob(STRING_RECORD, writer, encoded).get(FIELD_A);
Expand Down
20 changes: 10 additions & 10 deletions lang/java/avro/src/test/java/org/apache/avro/io/TestBlockingIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -35,7 +36,6 @@

@RunWith(Parameterized.class)
public class TestBlockingIO {
private static final String UTF_8 = "UTF-8";

private final int iSize;
private final int iDepth;
Expand All @@ -55,9 +55,9 @@ private static class Tests {
public Tests(int bufferSize, int depth, String input) throws IOException {

this.depth = depth;
byte[] in = input.getBytes("UTF-8");
byte[] in = input.getBytes(StandardCharsets.UTF_8);
JsonFactory f = new JsonFactory();
JsonParser p = f.createParser(new ByteArrayInputStream(input.getBytes("UTF-8")));
JsonParser p = f.createParser(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)));

ByteArrayOutputStream os = new ByteArrayOutputStream();
EncoderFactory factory = new EncoderFactory().configureBlockSize(bufferSize);
Expand Down Expand Up @@ -92,13 +92,13 @@ public void scan() throws IOException {
continue;
case VALUE_STRING: {
String s = parser.getText();
int n = s.getBytes(UTF_8).length;
int n = s.getBytes(StandardCharsets.UTF_8).length;
checkString(s, input, n);
break;
}
case FIELD_NAME: {
String s = parser.getCurrentName();
int n = s.getBytes(UTF_8).length;
int n = s.getBytes(StandardCharsets.UTF_8).length;
checkString(s, input, n);
continue;
}
Expand Down Expand Up @@ -149,14 +149,14 @@ public void skip(int skipLevel) throws IOException {
input.skipBytes();
} else {
String s = parser.getText();
int n = s.getBytes(UTF_8).length;
int n = s.getBytes(StandardCharsets.UTF_8).length;
checkString(s, input, n);
}
break;
}
case FIELD_NAME: {
String s = parser.getCurrentName();
int n = s.getBytes(UTF_8).length;
int n = s.getBytes(StandardCharsets.UTF_8).length;
checkString(s, input, n);
continue;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ private static void skipArray(JsonParser parser, Decoder input, int depth) throw
private static void checkString(String s, Decoder input, int n) throws IOException {
ByteBuffer buf = input.readBytes(null);
assertEquals(n, buf.remaining());
String s2 = new String(buf.array(), buf.position(), buf.remaining(), UTF_8);
String s2 = new String(buf.array(), buf.position(), buf.remaining(), StandardCharsets.UTF_8);
assertEquals(s, s2);
}

Expand Down Expand Up @@ -298,7 +298,7 @@ private static void serialize(Encoder cos, JsonParser p, ByteArrayOutputStream o
cos.startItem();
counts[stackTop]++;
}
byte[] bb = p.getText().getBytes(UTF_8);
byte[] bb = p.getText().getBytes(StandardCharsets.UTF_8);
cos.writeBytes(bb);
break;
case START_OBJECT:
Expand All @@ -315,7 +315,7 @@ private static void serialize(Encoder cos, JsonParser p, ByteArrayOutputStream o
cos.setItemCount(1);
cos.startItem();
counts[stackTop]++;
cos.writeBytes(p.getCurrentName().getBytes(UTF_8));
cos.writeBytes(p.getCurrentName().getBytes(StandardCharsets.UTF_8));
break;
default:
throw new RuntimeException("Unsupported: " + p.getCurrentToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -171,7 +171,7 @@ public void testBadRequest() throws IOException {
Socket sock = new Socket();
sock.connect(sockAddr);
OutputStream out = sock.getOutputStream();
out.write(msg.getBytes(Charset.forName("UTF-8")));
out.write(msg.getBytes(StandardCharsets.UTF_8));
out.flush();
byte[] buf = new byte[2048];
int bytesRead = sock.getInputStream().read(buf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

import javax.security.sasl.Sasl;
import javax.security.sasl.SaslServer;
import javax.security.sasl.SaslException;
Expand Down Expand Up @@ -87,11 +89,7 @@ public String getMechanismName() {

@Override
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void open(boolean isClient) throws IOException {
response = sasl.evaluate(frame.array());
status = sasl.isComplete() ? Status.COMPLETE : Status.CONTINUE;
} catch (SaslException e) {
response = e.toString().getBytes("UTF-8");
response = e.toString().getBytes(StandardCharsets.UTF_8);
status = Status.FAIL;
}
write(status, response != null ? ByteBuffer.wrap(response) : EMPTY);
Expand Down Expand Up @@ -272,7 +272,7 @@ private void write(Status status, String prefix, ByteBuffer response) throws IOE
}

private void write(Status status, String response) throws IOException {
write(status, ByteBuffer.wrap(response.getBytes("UTF-8")));
write(status, ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
}

private void write(Status status, ByteBuffer response) throws IOException {
Expand Down Expand Up @@ -399,11 +399,7 @@ public boolean hasInitialResponse() {

@Override
public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
try {
return System.getProperty("user.name").getBytes("UTF-8");
} catch (IOException e) {
throw new SaslException(e.toString());
}
return System.getProperty("user.name").getBytes(StandardCharsets.UTF_8);
}

@Override
Expand Down

0 comments on commit c552c17

Please sign in to comment.