Unverified 提交 0181856a authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #752 from katzyn/StandardCharsets

Use predefined charsets instead of names where possible
...@@ -17,6 +17,8 @@ import java.io.OutputStream; ...@@ -17,6 +17,8 @@ import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ParameterMetaData; import java.sql.ParameterMetaData;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
...@@ -572,11 +574,11 @@ public class PgServerThread implements Runnable { ...@@ -572,11 +574,11 @@ public class PgServerThread implements Runnable {
} }
} }
private String getEncoding() { private Charset getEncoding() {
if ("UNICODE".equals(clientEncoding)) { if ("UNICODE".equals(clientEncoding)) {
return "UTF-8"; return StandardCharsets.UTF_8;
} }
return clientEncoding; return Charset.forName(clientEncoding);
} }
private void setParameter(PreparedStatement prep, private void setParameter(PreparedStatement prep,
......
...@@ -433,12 +433,7 @@ public class IOUtils { ...@@ -433,12 +433,7 @@ public class IOUtils {
* @return the reader * @return the reader
*/ */
public static Reader getAsciiReader(InputStream in) { public static Reader getAsciiReader(InputStream in) {
try { return in == null ? null : new InputStreamReader(in, StandardCharsets.US_ASCII);
return in == null ? null : new InputStreamReader(in, "US-ASCII");
} catch (Exception e) {
// UnsupportedEncodingException
throw DbException.convert(e);
}
} }
/** /**
......
...@@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream; ...@@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
...@@ -151,7 +152,7 @@ public class MathUtils { ...@@ -151,7 +152,7 @@ public class MathUtils {
// can't use writeUTF, as the string // can't use writeUTF, as the string
// might be larger than 64 KB // might be larger than 64 KB
out.writeInt(s.length()); out.writeInt(s.length());
out.write(s.getBytes("UTF-8")); out.write(s.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) { } catch (Exception e) {
warn("generateAlternativeSeed", e); warn("generateAlternativeSeed", e);
} }
......
...@@ -15,6 +15,7 @@ import java.io.OutputStream; ...@@ -15,6 +15,7 @@ import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.lang.instrument.Instrumentation; import java.lang.instrument.Instrumentation;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
...@@ -272,11 +273,11 @@ public class Profiler implements Runnable { ...@@ -272,11 +273,11 @@ public class Profiler implements Runnable {
copyInThread(p.getInputStream(), out); copyInThread(p.getInputStream(), out);
copyInThread(p.getErrorStream(), err); copyInThread(p.getErrorStream(), err);
p.waitFor(); p.waitFor();
String e = new String(err.toByteArray(), "UTF-8"); String e = new String(err.toByteArray(), StandardCharsets.UTF_8);
if (e.length() > 0) { if (e.length() > 0) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
String output = new String(out.toByteArray(), "UTF-8"); String output = new String(out.toByteArray(), StandardCharsets.UTF_8);
return output; return output;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -15,6 +15,7 @@ import java.io.LineNumberReader; ...@@ -15,6 +15,7 @@ import java.io.LineNumberReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map.Entry; import java.util.Map.Entry;
...@@ -104,7 +105,7 @@ public class SortedProperties extends Properties { ...@@ -104,7 +105,7 @@ public class SortedProperties extends Properties {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
store(out, null); store(out, null);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
InputStreamReader reader = new InputStreamReader(in, "ISO8859-1"); InputStreamReader reader = new InputStreamReader(in, StandardCharsets.ISO_8859_1);
LineNumberReader r = new LineNumberReader(reader); LineNumberReader r = new LineNumberReader(reader);
Writer w; Writer w;
try { try {
......
...@@ -111,7 +111,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, ...@@ -111,7 +111,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
if (len == 0) { if (len == 0) {
break; break;
} }
byte[] data = new String(buff, 0, len).getBytes("UTF-8"); byte[] data = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
out.write(data); out.write(data);
tmpPrecision += len; tmpPrecision += len;
} }
......
...@@ -11,6 +11,7 @@ import java.io.InputStream; ...@@ -11,6 +11,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -36,7 +37,7 @@ public class Newsfeed { ...@@ -36,7 +37,7 @@ public class Newsfeed {
Class.forName("org.h2.Driver"); Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", ""); Connection conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
InputStream in = Newsfeed.class.getResourceAsStream("newsfeed.sql"); InputStream in = Newsfeed.class.getResourceAsStream("newsfeed.sql");
ResultSet rs = RunScript.execute(conn, new InputStreamReader(in, "ISO-8859-1")); ResultSet rs = RunScript.execute(conn, new InputStreamReader(in, StandardCharsets.ISO_8859_1));
in.close(); in.close();
while (rs.next()) { while (rs.next()) {
String file = rs.getString("FILE"); String file = rs.getString("FILE");
...@@ -46,7 +47,7 @@ public class Newsfeed { ...@@ -46,7 +47,7 @@ public class Newsfeed {
} }
new File(targetDir).mkdirs(); new File(targetDir).mkdirs();
FileOutputStream out = new FileOutputStream(targetDir + "/" + file); FileOutputStream out = new FileOutputStream(targetDir + "/" + file);
Writer writer = new OutputStreamWriter(out, "UTF-8"); Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
writer.write(content); writer.write(content);
writer.close(); writer.close();
out.close(); out.close();
......
...@@ -13,6 +13,7 @@ import java.io.OutputStream; ...@@ -13,6 +13,7 @@ import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -242,9 +243,9 @@ public class TestCsv extends TestBase { ...@@ -242,9 +243,9 @@ public class TestCsv extends TestBase {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
// UTF-8 "BOM" / marker // UTF-8 "BOM" / marker
out.write(StringUtils.convertHexToBytes("ef" + "bb" + "bf")); out.write(StringUtils.convertHexToBytes("ef" + "bb" + "bf"));
out.write("\"ID\", \"NAME\"\n1, Hello".getBytes("UTF-8")); out.write("\"ID\", \"NAME\"\n1, Hello".getBytes(StandardCharsets.UTF_8));
byte[] buff = out.toByteArray(); byte[] buff = out.toByteArray();
Reader r = new InputStreamReader(new ByteArrayInputStream(buff), "UTF-8"); Reader r = new InputStreamReader(new ByteArrayInputStream(buff), StandardCharsets.UTF_8);
ResultSet rs = new Csv().read(r, null); ResultSet rs = new Csv().read(r, null);
assertEquals("ID", rs.getMetaData().getColumnLabel(1)); assertEquals("ID", rs.getMetaData().getColumnLabel(1));
assertEquals("NAME", rs.getMetaData().getColumnLabel(2)); assertEquals("NAME", rs.getMetaData().getColumnLabel(2));
...@@ -306,7 +307,7 @@ public class TestCsv extends TestBase { ...@@ -306,7 +307,7 @@ public class TestCsv extends TestBase {
OutputStream out = FileUtils.newOutputStream(fileName, false); OutputStream out = FileUtils.newOutputStream(fileName, false);
String csvContent = "\"A\",\"B\",\"C\",\"D\"\n\\N,\"\",\"\\N\","; String csvContent = "\"A\",\"B\",\"C\",\"D\"\n\\N,\"\",\"\\N\",";
byte[] b = csvContent.getBytes("UTF-8"); byte[] b = csvContent.getBytes(StandardCharsets.UTF_8);
out.write(b, 0, b.length); out.write(b, 0, b.length);
out.close(); out.close();
Csv csv = new Csv(); Csv csv = new Csv();
......
...@@ -13,6 +13,7 @@ import java.io.InputStream; ...@@ -13,6 +13,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Clob; import java.sql.Clob;
import java.sql.Connection; import java.sql.Connection;
...@@ -1358,7 +1359,7 @@ public class TestLob extends TestBase { ...@@ -1358,7 +1359,7 @@ public class TestLob extends TestBase {
PreparedStatement prep; PreparedStatement prep;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(1, ?)"); prep = conn.prepareStatement("INSERT INTO TEST VALUES(1, ?)");
String s = new String(getRandomChars(10000, 1)); String s = new String(getRandomChars(10000, 1));
byte[] data = s.getBytes("UTF-8"); byte[] data = s.getBytes(StandardCharsets.UTF_8);
// if we keep the string, debugging with Eclipse is not possible // if we keep the string, debugging with Eclipse is not possible
// because Eclipse wants to display the large string and fails // because Eclipse wants to display the large string and fails
s = ""; s = "";
......
...@@ -10,6 +10,7 @@ import java.io.Reader; ...@@ -10,6 +10,7 @@ import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.Array; import java.sql.Array;
import java.sql.CallableStatement; import java.sql.CallableStatement;
import java.sql.Connection; import java.sql.Connection;
...@@ -365,15 +366,15 @@ public class TestCallableStatement extends TestBase { ...@@ -365,15 +366,15 @@ public class TestCallableStatement extends TestBase {
call.executeUpdate(); call.executeUpdate();
assertEquals("XYZ", call.getString("B")); assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", call.setAsciiStream("B",
new ByteArrayInputStream("xyz".getBytes("UTF-8"))); new ByteArrayInputStream("xyz".getBytes(StandardCharsets.UTF_8)));
call.executeUpdate(); call.executeUpdate();
assertEquals("XYZ", call.getString("B")); assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", call.setAsciiStream("B",
new ByteArrayInputStream("xyz-".getBytes("UTF-8")), 3); new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3);
call.executeUpdate(); call.executeUpdate();
assertEquals("XYZ", call.getString("B")); assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", call.setAsciiStream("B",
new ByteArrayInputStream("xyz-".getBytes("UTF-8")), 3L); new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3L);
call.executeUpdate(); call.executeUpdate();
assertEquals("XYZ", call.getString("B")); assertEquals("XYZ", call.getString("B"));
......
...@@ -12,6 +12,7 @@ import java.io.OutputStream; ...@@ -12,6 +12,7 @@ import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Clob; import java.sql.Clob;
import java.sql.Connection; import java.sql.Connection;
...@@ -76,7 +77,7 @@ public class TestLobApi extends TestBase { ...@@ -76,7 +77,7 @@ public class TestLobApi extends TestBase {
rs.next(); rs.next();
Clob clob = rs.getClob(2); Clob clob = rs.getClob(2);
byte[] data = IOUtils.readBytesAndClose(clob.getAsciiStream(), -1); byte[] data = IOUtils.readBytesAndClose(clob.getAsciiStream(), -1);
assertEquals("x", new String(data, "UTF-8")); assertEquals("x", new String(data, StandardCharsets.UTF_8));
assertTrue(clob.toString().endsWith("'x'")); assertTrue(clob.toString().endsWith("'x'"));
clob.free(); clob.free();
assertTrue(clob.toString().endsWith("null")); assertTrue(clob.toString().endsWith("null"));
......
...@@ -13,6 +13,7 @@ import java.io.InputStreamReader; ...@@ -13,6 +13,7 @@ import java.io.InputStreamReader;
import java.io.Writer; import java.io.Writer;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.sql.Array; import java.sql.Array;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Clob; import java.sql.Clob;
...@@ -257,37 +258,37 @@ public class TestResultSet extends TestBase { ...@@ -257,37 +258,37 @@ public class TestResultSet extends TestBase {
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 6); rs.updateInt(1, 6);
in = new ByteArrayInputStream("Hello".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream(2, in); rs.updateAsciiStream(2, in);
rs.insertRow(); rs.insertRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 7); rs.updateInt(1, 7);
in = new ByteArrayInputStream("Hello".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream("data", in); rs.updateAsciiStream("data", in);
rs.insertRow(); rs.insertRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 8); rs.updateInt(1, 8);
in = new ByteArrayInputStream("Hello-".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello-".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream(2, in, 5); rs.updateAsciiStream(2, in, 5);
rs.insertRow(); rs.insertRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 9); rs.updateInt(1, 9);
in = new ByteArrayInputStream("Hello-".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello-".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream("data", in, 5); rs.updateAsciiStream("data", in, 5);
rs.insertRow(); rs.insertRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 10); rs.updateInt(1, 10);
in = new ByteArrayInputStream("Hello-".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello-".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream(2, in, 5L); rs.updateAsciiStream(2, in, 5L);
rs.insertRow(); rs.insertRow();
rs.moveToInsertRow(); rs.moveToInsertRow();
rs.updateInt(1, 11); rs.updateInt(1, 11);
in = new ByteArrayInputStream("Hello-".getBytes("UTF-8")); in = new ByteArrayInputStream("Hello-".getBytes(StandardCharsets.UTF_8));
rs.updateAsciiStream("data", in, 5L); rs.updateAsciiStream("data", in, 5L);
rs.insertRow(); rs.insertRow();
...@@ -1615,7 +1616,7 @@ public class TestResultSet extends TestBase { ...@@ -1615,7 +1616,7 @@ public class TestResultSet extends TestBase {
rs.next(); rs.next();
InputStreamReader reader = null; InputStreamReader reader = null;
try { try {
reader = new InputStreamReader(rs.getAsciiStream(2), "ISO-8859-1"); reader = new InputStreamReader(rs.getAsciiStream(2), StandardCharsets.ISO_8859_1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(false); assertTrue(false);
} }
...@@ -1625,7 +1626,7 @@ public class TestResultSet extends TestBase { ...@@ -1625,7 +1626,7 @@ public class TestResultSet extends TestBase {
assertTrue(string != null && string.equals("Hello")); assertTrue(string != null && string.equals("Hello"));
rs.next(); rs.next();
try { try {
reader = new InputStreamReader(rs.getAsciiStream("value"), "ISO-8859-1"); reader = new InputStreamReader(rs.getAsciiStream("value"), StandardCharsets.ISO_8859_1);
} catch (Exception e) { } catch (Exception e) {
assertTrue(false); assertTrue(false);
} }
......
...@@ -12,6 +12,7 @@ import java.io.PrintStream; ...@@ -12,6 +12,7 @@ import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.ConnectException; import java.net.ConnectException;
import java.nio.charset.StandardCharsets;
import java.security.Principal; import java.security.Principal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -1175,11 +1176,7 @@ public class TestWeb extends TestBase { ...@@ -1175,11 +1176,7 @@ public class TestWeb extends TestBase {
@Override @Override
public String toString() { public String toString() {
try { return new String(buff.toByteArray(), StandardCharsets.UTF_8);
return new String(buff.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return e.toString();
}
} }
@Override @Override
......
...@@ -8,6 +8,7 @@ package org.h2.test.store; ...@@ -8,6 +8,7 @@ package org.h2.test.store;
import java.lang.Thread.UncaughtExceptionHandler; import java.lang.Thread.UncaughtExceptionHandler;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
...@@ -970,7 +971,7 @@ public class TestMVStore extends TestBase { ...@@ -970,7 +971,7 @@ public class TestMVStore extends TestBase {
} }
ByteBuffer buff = ByteBuffer.allocate(4 * 1024); ByteBuffer buff = ByteBuffer.allocate(4 * 1024);
fc.read(buff, i); fc.read(buff, i);
String h = new String(buff.array(), "UTF-8").trim(); String h = new String(buff.array(), StandardCharsets.UTF_8).trim();
int idx = h.indexOf("fletcher:"); int idx = h.indexOf("fletcher:");
int old = Character.digit(h.charAt(idx + "fletcher:".length()), 16); int old = Character.digit(h.charAt(idx + "fletcher:".length()), 16);
int bad = (old + 1) & 15; int bad = (old + 1) & 15;
......
...@@ -12,6 +12,7 @@ import java.io.InputStream; ...@@ -12,6 +12,7 @@ import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
...@@ -131,7 +132,7 @@ public class TestSampleApps extends TestBase { ...@@ -131,7 +132,7 @@ public class TestSampleApps extends TestBase {
out.flush(); out.flush();
System.setOut(oldOut); System.setOut(oldOut);
System.setErr(oldErr); System.setErr(oldErr);
String s = new String(buff.toByteArray(), "UTF-8"); String s = new String(buff.toByteArray(), StandardCharsets.UTF_8);
s = StringUtils.replaceAll(s, "\r\n", "\n"); s = StringUtils.replaceAll(s, "\r\n", "\n");
s = s.trim(); s = s.trim();
expected = expected.trim(); expected = expected.trim();
......
...@@ -20,6 +20,7 @@ import java.lang.reflect.Method; ...@@ -20,6 +20,7 @@ import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Clob; import java.sql.Clob;
import java.sql.Connection; import java.sql.Connection;
...@@ -860,7 +861,7 @@ public class TestTools extends TestBase { ...@@ -860,7 +861,7 @@ public class TestTools extends TestBase {
byte[] large = new byte[getSize(10 * 1024, 100 * 1024)]; byte[] large = new byte[getSize(10 * 1024, 100 * 1024)];
random.nextBytes(large); random.nextBytes(large);
prep.setBytes(2, large); prep.setBytes(2, large);
String largeText = new String(large, "ISO-8859-1"); String largeText = new String(large, StandardCharsets.ISO_8859_1);
prep.setString(3, largeText); prep.setString(3, largeText);
prep.execute(); prep.execute();
......
...@@ -28,6 +28,7 @@ import java.lang.reflect.InvocationTargetException; ...@@ -28,6 +28,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -495,7 +496,7 @@ public class BuildBase { ...@@ -495,7 +496,7 @@ public class BuildBase {
buff.write(b); buff.write(b);
if (b == '\n') { if (b == '\n') {
byte[] data = buff.toByteArray(); byte[] data = buff.toByteArray();
String line = new String(data, "UTF-8"); String line = new String(data, StandardCharsets.UTF_8);
boolean print = true; boolean print = true;
for (String l : exclude) { for (String l : exclude) {
if (line.startsWith(l)) { if (line.startsWith(l)) {
......
...@@ -8,6 +8,7 @@ package org.h2.build.code; ...@@ -8,6 +8,7 @@ package org.h2.build.code;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
/** /**
* Enable / disable AB-BA deadlock detector code. * Enable / disable AB-BA deadlock detector code.
...@@ -46,7 +47,7 @@ public class AbbaDetect { ...@@ -46,7 +47,7 @@ public class AbbaDetect {
byte[] data = new byte[(int) file.length()]; byte[] data = new byte[(int) file.length()];
in.readFully(data); in.readFully(data);
in.close(); in.close();
String source = new String(data, "UTF-8"); String source = new String(data, StandardCharsets.UTF_8);
String original = source; String original = source;
source = disable(source); source = disable(source);
...@@ -63,7 +64,7 @@ public class AbbaDetect { ...@@ -63,7 +64,7 @@ public class AbbaDetect {
} }
File newFile = new File(file + ".new"); File newFile = new File(file + ".new");
RandomAccessFile out = new RandomAccessFile(newFile, "rw"); RandomAccessFile out = new RandomAccessFile(newFile, "rw");
out.write(source.getBytes("UTF-8")); out.write(source.getBytes(StandardCharsets.UTF_8));
out.close(); out.close();
File oldFile = new File(file + ".old"); File oldFile = new File(file + ".old");
......
...@@ -8,6 +8,7 @@ package org.h2.build.doc; ...@@ -8,6 +8,7 @@ package org.h2.build.doc;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale; import java.util.Locale;
import org.h2.build.indexer.HtmlConverter; import org.h2.build.indexer.HtmlConverter;
...@@ -52,10 +53,10 @@ public class FileConverter { ...@@ -52,10 +53,10 @@ public class FileConverter {
private void convert() throws IOException { private void convert() throws IOException {
InputStream in = FileUtils.newInputStream(inFile); InputStream in = FileUtils.newInputStream(inFile);
byte[] bytes = IOUtils.readBytesAndClose(in, -1); byte[] bytes = IOUtils.readBytesAndClose(in, -1);
String s = new String(bytes, "UTF-8"); String s = new String(bytes, StandardCharsets.UTF_8);
String s2 = HtmlConverter.convertHtmlToString(s); String s2 = HtmlConverter.convertHtmlToString(s);
String s3 = StringUtils.javaDecode(s2); String s3 = StringUtils.javaDecode(s2);
byte[] result = s3.getBytes("UTF-8"); byte[] result = s3.getBytes(StandardCharsets.UTF_8);
OutputStream out = FileUtils.newOutputStream(outFile, false); OutputStream out = FileUtils.newOutputStream(outFile, false);
out.write(result); out.write(result);
out.close(); out.close();
......
...@@ -13,6 +13,7 @@ import java.io.FileReader; ...@@ -13,6 +13,7 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -54,7 +55,7 @@ public class UploadBuild { ...@@ -54,7 +55,7 @@ public class UploadBuild {
if (coverage) { if (coverage) {
byte[] data = IOUtils.readBytesAndClose( byte[] data = IOUtils.readBytesAndClose(
new FileInputStream("coverage/index.html"), -1); new FileInputStream("coverage/index.html"), -1);
String index = new String(data, "ISO-8859-1"); String index = new String(data, StandardCharsets.ISO_8859_1);
coverageFailed = index.contains("CLASS=\"h\""); coverageFailed = index.contains("CLASS=\"h\"");
while (true) { while (true) {
int idx = index.indexOf("<A HREF=\""); int idx = index.indexOf("<A HREF=\"");
...@@ -69,7 +70,7 @@ public class UploadBuild { ...@@ -69,7 +70,7 @@ public class UploadBuild {
index = StringUtils.replaceAll(index, "[all", ""); index = StringUtils.replaceAll(index, "[all", "");
index = StringUtils.replaceAll(index, "classes]", ""); index = StringUtils.replaceAll(index, "classes]", "");
FileOutputStream out = new FileOutputStream("coverage/overview.html"); FileOutputStream out = new FileOutputStream("coverage/overview.html");
out.write(index.getBytes("ISO-8859-1")); out.write(index.getBytes(StandardCharsets.ISO_8859_1));
out.close(); out.close();
new File("details").mkdir(); new File("details").mkdir();
zip("details/coverage_files.zip", "coverage", true); zip("details/coverage_files.zip", "coverage", true);
......
...@@ -9,6 +9,7 @@ import java.io.File; ...@@ -9,6 +9,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import org.h2.samples.Newsfeed; import org.h2.samples.Newsfeed;
...@@ -65,7 +66,7 @@ public class WebSite { ...@@ -65,7 +66,7 @@ public class WebSite {
if (f.getName().startsWith("fragments")) { if (f.getName().startsWith("fragments")) {
FileInputStream in = new FileInputStream(f); FileInputStream in = new FileInputStream(f);
byte[] bytes = IOUtils.readBytesAndClose(in, 0); byte[] bytes = IOUtils.readBytesAndClose(in, 0);
String page = new String(bytes, "UTF-8"); String page = new String(bytes, StandardCharsets.UTF_8);
fragments.put(f.getName(), page); fragments.put(f.getName(), page);
} }
} }
...@@ -140,7 +141,7 @@ public class WebSite { ...@@ -140,7 +141,7 @@ public class WebSite {
FileInputStream in = new FileInputStream(source); FileInputStream in = new FileInputStream(source);
byte[] bytes = IOUtils.readBytesAndClose(in, 0); byte[] bytes = IOUtils.readBytesAndClose(in, 0);
if (name.endsWith(".html")) { if (name.endsWith(".html")) {
String page = new String(bytes, "UTF-8"); String page = new String(bytes, StandardCharsets.UTF_8);
if (web) { if (web) {
page = StringUtils.replaceAll(page, ANALYTICS_TAG, ANALYTICS_SCRIPT); page = StringUtils.replaceAll(page, ANALYTICS_TAG, ANALYTICS_SCRIPT);
} }
...@@ -155,7 +156,7 @@ public class WebSite { ...@@ -155,7 +156,7 @@ public class WebSite {
page = StringUtils.replaceAll(page, "<pre>", "<pre class=\"notranslate\">"); page = StringUtils.replaceAll(page, "<pre>", "<pre class=\"notranslate\">");
page = StringUtils.replaceAll(page, "<code>", "<code class=\"notranslate\">"); page = StringUtils.replaceAll(page, "<code>", "<code class=\"notranslate\">");
} }
bytes = page.getBytes("UTF-8"); bytes = page.getBytes(StandardCharsets.UTF_8);
} }
FileOutputStream out = new FileOutputStream(target); FileOutputStream out = new FileOutputStream(target);
out.write(bytes); out.write(bytes);
......
...@@ -14,6 +14,7 @@ import java.io.IOException; ...@@ -14,6 +14,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
...@@ -153,7 +154,7 @@ public class PrepareTranslation { ...@@ -153,7 +154,7 @@ public class PrepareTranslation {
target = targetDir + "/" + name + "_" + language + ".html"; target = targetDir + "/" + name + "_" + language + ".html";
} }
OutputStream out = new FileOutputStream(target); OutputStream out = new FileOutputStream(target);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8"); OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
writer.write(html); writer.write(html);
writer.close(); writer.close();
} }
...@@ -231,7 +232,7 @@ public class PrepareTranslation { ...@@ -231,7 +232,7 @@ public class PrepareTranslation {
private static String extract(String documentName, File f, String target) private static String extract(String documentName, File f, String target)
throws Exception { throws Exception {
String xml = IOUtils.readStringAndClose(new InputStreamReader( String xml = IOUtils.readStringAndClose(new InputStreamReader(
new FileInputStream(f), "UTF-8"), -1); new FileInputStream(f), StandardCharsets.UTF_8), -1);
// the template contains ${} instead of text // the template contains ${} instead of text
StringBuilder template = new StringBuilder(xml.length()); StringBuilder template = new StringBuilder(xml.length());
int id = 0; int id = 0;
...@@ -444,7 +445,7 @@ public class PrepareTranslation { ...@@ -444,7 +445,7 @@ public class PrepareTranslation {
throws IOException { throws IOException {
if (utf8) { if (utf8) {
String s = new String(IOUtils.readBytesAndClose( String s = new String(IOUtils.readBytesAndClose(
new FileInputStream(fileName), -1), "UTF-8"); new FileInputStream(fileName), -1), StandardCharsets.UTF_8);
return SortedProperties.fromLines(s); return SortedProperties.fromLines(s);
} }
return SortedProperties.loadProperties(fileName); return SortedProperties.loadProperties(fileName);
...@@ -455,7 +456,7 @@ public class PrepareTranslation { ...@@ -455,7 +456,7 @@ public class PrepareTranslation {
if (utf8) { if (utf8) {
String s = p.toLines(); String s = p.toLines();
FileOutputStream f = new FileOutputStream(fileName); FileOutputStream f = new FileOutputStream(fileName);
f.write(s.getBytes("UTF-8")); f.write(s.getBytes(StandardCharsets.UTF_8));
f.close(); f.close();
} else { } else {
p.store(fileName); p.store(fileName);
......
...@@ -13,6 +13,7 @@ import java.io.LineNumberReader; ...@@ -13,6 +13,7 @@ import java.io.LineNumberReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import org.h2.build.code.CheckTextFiles; import org.h2.build.code.CheckTextFiles;
...@@ -55,7 +56,7 @@ public class PropertiesToUTF8 { ...@@ -55,7 +56,7 @@ public class PropertiesToUTF8 {
} }
Properties prop = SortedProperties.loadProperties(source); Properties prop = SortedProperties.loadProperties(source);
FileOutputStream out = new FileOutputStream(target); FileOutputStream out = new FileOutputStream(target);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
// keys is sorted // keys is sorted
for (Enumeration<Object> en = prop.keys(); en.hasMoreElements();) { for (Enumeration<Object> en = prop.keys(); en.hasMoreElements();) {
String key = (String) en.nextElement(); String key = (String) en.nextElement();
...@@ -79,7 +80,7 @@ public class PropertiesToUTF8 { ...@@ -79,7 +80,7 @@ public class PropertiesToUTF8 {
return; return;
} }
LineNumberReader reader = new LineNumberReader(new InputStreamReader( LineNumberReader reader = new LineNumberReader(new InputStreamReader(
new FileInputStream(source), "UTF-8")); new FileInputStream(source), StandardCharsets.UTF_8));
try { try {
SortedProperties prop = new SortedProperties(); SortedProperties prop = new SortedProperties();
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
...@@ -123,7 +124,7 @@ public class PropertiesToUTF8 { ...@@ -123,7 +124,7 @@ public class PropertiesToUTF8 {
continue; continue;
} }
FileInputStream in = new FileInputStream(f); FileInputStream in = new FileInputStream(f);
InputStreamReader r = new InputStreamReader(in, "UTF-8"); InputStreamReader r = new InputStreamReader(in, StandardCharsets.UTF_8);
String s = IOUtils.readStringAndClose(r, -1); String s = IOUtils.readStringAndClose(r, -1);
in.close(); in.close();
String name = f.getName(); String name = f.getName();
...@@ -142,7 +143,7 @@ public class PropertiesToUTF8 { ...@@ -142,7 +143,7 @@ public class PropertiesToUTF8 {
// s = unescapeHtml(s); // s = unescapeHtml(s);
utf8 = StringUtils.javaDecode(utf8); utf8 = StringUtils.javaDecode(utf8);
FileOutputStream out = new FileOutputStream("_utf8" + f.getName()); FileOutputStream out = new FileOutputStream("_utf8" + f.getName());
OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8"); OutputStreamWriter w = new OutputStreamWriter(out, StandardCharsets.UTF_8);
w.write(utf8); w.write(utf8);
w.close(); w.close();
out.close(); out.close();
......
...@@ -9,6 +9,7 @@ import java.io.File; ...@@ -9,6 +9,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
...@@ -256,7 +257,7 @@ public class Indexer { ...@@ -256,7 +257,7 @@ public class Indexer {
private void readPage(File file) throws Exception { private void readPage(File file) throws Exception {
byte[] data = IOUtils.readBytesAndClose(new FileInputStream(file), 0); byte[] data = IOUtils.readBytesAndClose(new FileInputStream(file), 0);
String text = new String(data, "UTF-8"); String text = new String(data, StandardCharsets.UTF_8);
StringTokenizer t = new StringTokenizer(text, "<> \r\n", true); StringTokenizer t = new StringTokenizer(text, "<> \r\n", true);
boolean inTag = false; boolean inTag = false;
title = false; title = false;
......
...@@ -7,6 +7,7 @@ package org.h2.dev.util; ...@@ -7,6 +7,7 @@ package org.h2.dev.util;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collections; import java.util.Collections;
...@@ -99,8 +100,8 @@ public class FileContentHash { ...@@ -99,8 +100,8 @@ public class FileContentHash {
checkCollision(f, length, StringUtils.convertHexToBytes(hash)); checkCollision(f, length, StringUtils.convertHexToBytes(hash));
} }
propNew.put(entry, hash); propNew.put(entry, hash);
mdDir.update(entry.getBytes("UTF-8")); mdDir.update(entry.getBytes(StandardCharsets.UTF_8));
mdDir.update(hash.getBytes("UTF-8")); mdDir.update(hash.getBytes(StandardCharsets.UTF_8));
} }
String oldFile = propOld.toString(); String oldFile = propOld.toString();
String newFile = propNew.toString(); String newFile = propNew.toString();
......
...@@ -8,6 +8,7 @@ package org.h2.dev.util; ...@@ -8,6 +8,7 @@ package org.h2.dev.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
...@@ -91,11 +92,11 @@ public class JavaProcessKiller { ...@@ -91,11 +92,11 @@ public class JavaProcessKiller {
copyInThread(p.getInputStream(), out); copyInThread(p.getInputStream(), out);
copyInThread(p.getErrorStream(), err); copyInThread(p.getErrorStream(), err);
p.waitFor(); p.waitFor();
String e = new String(err.toByteArray(), "UTF-8"); String e = new String(err.toByteArray(), StandardCharsets.UTF_8);
if (e.length() > 0) { if (e.length() > 0) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
String output = new String(out.toByteArray(), "UTF-8"); String output = new String(out.toByteArray(), StandardCharsets.UTF_8);
return output; return output;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
...@@ -9,6 +9,7 @@ import java.io.IOException; ...@@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer; import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel.MapMode; import java.nio.channels.FileChannel.MapMode;
import java.nio.charset.StandardCharsets;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.security.SHA256; import org.h2.security.SHA256;
...@@ -45,7 +46,7 @@ public class RemovePasswords { ...@@ -45,7 +46,7 @@ public class RemovePasswords {
} }
buff.position(i); buff.position(i);
buff.get(data); buff.get(data);
String s = new String(data, "UTF-8"); String s = new String(data, StandardCharsets.UTF_8);
if (!s.startsWith("CREATE USER ")) { if (!s.startsWith("CREATE USER ")) {
continue; continue;
} }
......
...@@ -8,6 +8,7 @@ package org.h2.java; ...@@ -8,6 +8,7 @@ package org.h2.java;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
...@@ -166,7 +167,7 @@ public class JavaParser { ...@@ -166,7 +167,7 @@ public class JavaParser {
RandomAccessFile file = new RandomAccessFile(fileName, "r"); RandomAccessFile file = new RandomAccessFile(fileName, "r");
byte[] buff = new byte[(int) file.length()]; byte[] buff = new byte[(int) file.length()];
file.readFully(buff); file.readFully(buff);
source = new String(buff, "UTF-8"); source = new String(buff, StandardCharsets.UTF_8);
file.close(); file.close();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论