Use StandardCharsets.UTF_8 instead of Charset.forName("utf-8); add charset where needed

Application should always work with utf-8 strings regardless of locale.
This commit is contained in:
Jano Svitok 2018-08-06 12:51:24 +02:00
parent b255b3b618
commit 71689e4757
5 changed files with 14 additions and 13 deletions

View File

@ -1,7 +1,7 @@
package se.vidstige.jadb;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* Created by vidstige on 2014-03-19.
@ -52,7 +52,7 @@ public class SyncTransport {
public String readString(int length) throws IOException {
byte[] buffer = new byte[length];
input.readFully(buffer);
return new String(buffer, Charset.forName("utf-8"));
return new String(buffer, StandardCharsets.UTF_8);
}
public RemoteFileRecord readDirectoryEntry() throws IOException {

View File

@ -2,7 +2,7 @@ package se.vidstige.jadb;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
class Transport {
@ -44,7 +44,7 @@ class Transport {
DataInput reader = new DataInputStream(inputStream);
byte[] responseBuffer = new byte[length];
reader.readFully(responseBuffer);
return new String(responseBuffer, Charset.forName("utf-8"));
return new String(responseBuffer, StandardCharsets.UTF_8);
}
public String getCommandLength(String command) {
@ -52,7 +52,7 @@ class Transport {
}
public void send(String command) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
writer.write(getCommandLength(command));
writer.write(command);
writer.flush();

View File

@ -6,6 +6,7 @@ import se.vidstige.jadb.JadbException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@ -24,7 +25,7 @@ public class PropertyManager {
public Map<String, String> getprop() throws IOException, JadbException {
try (BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(device.executeShell("getprop")))) {
new BufferedReader(new InputStreamReader(device.executeShell("getprop"), StandardCharsets.UTF_8))) {
return parseProp(bufferedReader);
}
}

View File

@ -11,7 +11,7 @@ import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ProtocolException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@ -179,7 +179,7 @@ public class FakeAdbServer implements AdbResponder {
@Override
public void withContent(String content) {
this.content = content.getBytes(Charset.forName("utf-8"));
this.content = content.getBytes(StandardCharsets.UTF_8);
}
public boolean matches(RemoteFile path) {
@ -212,7 +212,7 @@ public class FakeAdbServer implements AdbResponder {
}
public void returns(String stdout) {
this.stdout = stdout.getBytes(Charset.forName("utf-8"));
this.stdout = stdout.getBytes(StandardCharsets.UTF_8);
}
public void writeOutputTo(DataOutputStream stdout) throws IOException {

View File

@ -12,7 +12,7 @@ import se.vidstige.jadb.test.fakes.FakeAdbServer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -72,7 +72,7 @@ public class MockedTestCases {
server.add("serial-123");
server.expectPush("serial-123", new RemoteFile("/remote/path/abc.txt")).withContent("abc");
JadbDevice device = connection.getDevices().get(0);
ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes());
ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8));
device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt"));
}
@ -81,7 +81,7 @@ public class MockedTestCases {
server.add("serial-123");
server.expectPush("serial-123", new RemoteFile("/remote/path/abc.txt")).failWith("No such directory");
JadbDevice device = connection.getDevices().get(0);
ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes());
ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8));
device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt"));
}
@ -92,7 +92,7 @@ public class MockedTestCases {
JadbDevice device = connection.getDevices().get(0);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
device.pull(new RemoteFile("/remote/path/abc.txt"), buffer);
Assert.assertArrayEquals("foobar".getBytes(Charset.forName("utf-8")), buffer.toByteArray());
Assert.assertArrayEquals("foobar".getBytes(StandardCharsets.UTF_8), buffer.toByteArray());
}
@Test