提交 e9c2b93e authored 作者: Thomas Mueller's avatar Thomas Mueller

Buffered and unbuffered readers

上级 696ff6f2
...@@ -388,7 +388,7 @@ public class TraceObject { ...@@ -388,7 +388,7 @@ public class TraceObject {
synchronized (TraceObject.class) { synchronized (TraceObject.class) {
// e.printStackTrace(); // e.printStackTrace();
try { try {
Writer writer = IOUtils.getWriter(IOUtils.openFileOutputStream(SysProperties.LOG_ALL_ERRORS_FILE, true)); Writer writer = IOUtils.getBufferedWriter(IOUtils.openFileOutputStream(SysProperties.LOG_ALL_ERRORS_FILE, true));
PrintWriter p = new PrintWriter(writer); PrintWriter p = new PrintWriter(writer);
e.printStackTrace(p); e.printStackTrace(p);
p.close(); p.close();
......
...@@ -282,7 +282,7 @@ public class TraceSystem implements TraceWriter { ...@@ -282,7 +282,7 @@ public class TraceSystem implements TraceWriter {
// can't be opened // can't be opened
return false; return false;
} }
fileWriter = IOUtils.getWriter(IOUtils.openFileOutputStream(fileName, true)); fileWriter = IOUtils.getBufferedWriter(IOUtils.openFileOutputStream(fileName, true));
printWriter = new PrintWriter(fileWriter, true); printWriter = new PrintWriter(fileWriter, true);
} catch (Exception e) { } catch (Exception e) {
logWritingError(e); logWritingError(e);
......
...@@ -110,9 +110,9 @@ public class ConvertTraceFile extends Tool { ...@@ -110,9 +110,9 @@ public class ConvertTraceFile extends Tool {
* @throws IOException * @throws IOException
*/ */
private void convertFile(String traceFileName, String javaClassName, String script) throws IOException { private void convertFile(String traceFileName, String javaClassName, String script) throws IOException {
LineNumberReader reader = new LineNumberReader(IOUtils.getReader(IOUtils.openFileInputStream(traceFileName))); LineNumberReader reader = new LineNumberReader(IOUtils.getBufferedReader(IOUtils.openFileInputStream(traceFileName)));
PrintWriter javaWriter = new PrintWriter(IOUtils.getWriter(IOUtils.openFileOutputStream(javaClassName + ".java", false))); PrintWriter javaWriter = new PrintWriter(IOUtils.getBufferedWriter(IOUtils.openFileOutputStream(javaClassName + ".java", false)));
PrintWriter scriptWriter = new PrintWriter(IOUtils.getWriter(IOUtils.openFileOutputStream(script, false))); PrintWriter scriptWriter = new PrintWriter(IOUtils.getBufferedWriter(IOUtils.openFileOutputStream(script, false)));
javaWriter.println("import java.io.*;"); javaWriter.println("import java.io.*;");
javaWriter.println("import java.sql.*;"); javaWriter.println("import java.sql.*;");
javaWriter.println("import java.math.*;"); javaWriter.println("import java.math.*;");
......
...@@ -246,7 +246,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -246,7 +246,7 @@ public class Recover extends Tool implements DataHandler {
fileName = fileName.substring(0, fileName.length() - 3); fileName = fileName.substring(0, fileName.length() - 3);
String outputFile = fileName + suffix; String outputFile = fileName + suffix;
trace("Created file: " + outputFile); trace("Created file: " + outputFile);
return new PrintWriter(IOUtils.getWriter(IOUtils.openFileOutputStream(outputFile, false))); return new PrintWriter(IOUtils.getBufferedWriter(IOUtils.openFileOutputStream(outputFile, false)));
} }
private void writeDataError(PrintWriter writer, String error, byte[] data) { private void writeDataError(PrintWriter writer, String error, byte[] data) {
......
...@@ -188,7 +188,7 @@ public class Script extends Tool { ...@@ -188,7 +188,7 @@ public class Script extends Tool {
Statement stat = null; Statement stat = null;
try { try {
stat = conn.createStatement(); stat = conn.createStatement();
PrintWriter writer = new PrintWriter(IOUtils.getWriter(o)); PrintWriter writer = new PrintWriter(IOUtils.getBufferedWriter(o));
ResultSet rs = stat.executeQuery("SCRIPT"); ResultSet rs = stat.executeQuery("SCRIPT");
while (rs.next()) { while (rs.next()) {
String s = rs.getString(1); String s = rs.getString(1);
......
...@@ -338,9 +338,30 @@ public class IOUtils { ...@@ -338,9 +338,30 @@ public class IOUtils {
return off <= 0 ? -1 : off; return off <= 0 ? -1 : off;
} }
/**
* Create a buffered reader to read from an input stream using the UTF-8
* format. If the input stream is null, this method returns null. The
* InputStreamReader that is used here is not exact, that means it may read
* some additional bytes when buffering.
*
* @param in the input stream or null
* @return the reader
*/
public static Reader getBufferedReader(InputStream in) {
try {
//
return in == null ? null : new BufferedReader(new InputStreamReader(in, Constants.UTF8));
} catch (Exception e) {
// UnsupportedEncodingException
throw DbException.convert(e);
}
}
/** /**
* Create a reader to read from an input stream using the UTF-8 format. If * Create a reader to read from an input stream using the UTF-8 format. If
* the input stream is null, this method returns null. * the input stream is null, this method returns null. The InputStreamReader
* that is used here is not exact, that means it may read some additional
* bytes when buffering.
* *
* @param in the input stream or null * @param in the input stream or null
* @return the reader * @return the reader
...@@ -362,7 +383,7 @@ public class IOUtils { ...@@ -362,7 +383,7 @@ public class IOUtils {
* @param out the output stream or null * @param out the output stream or null
* @return the writer * @return the writer
*/ */
public static Writer getWriter(OutputStream out) { public static Writer getBufferedWriter(OutputStream out) {
try { try {
return out == null ? null : new BufferedWriter(new OutputStreamWriter(out, Constants.UTF8)); return out == null ? null : new BufferedWriter(new OutputStreamWriter(out, Constants.UTF8));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -148,7 +148,7 @@ public class SourceCompiler { ...@@ -148,7 +148,7 @@ public class SourceCompiler {
File classFile = new File(dir, className + ".class"); File classFile = new File(dir, className + ".class");
try { try {
OutputStream f = IOUtils.openFileOutputStream(javaFile.getAbsolutePath(), false); OutputStream f = IOUtils.openFileOutputStream(javaFile.getAbsolutePath(), false);
PrintWriter out = new PrintWriter(IOUtils.getWriter(f)); PrintWriter out = new PrintWriter(IOUtils.getBufferedWriter(f));
classFile.delete(); classFile.delete();
int endImport = source.indexOf("@CODE"); int endImport = source.indexOf("@CODE");
String importCode = "import java.util.*;\n" + String importCode = "import java.util.*;\n" +
......
...@@ -261,7 +261,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo ...@@ -261,7 +261,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, Value.ValueBlo
} }
public Reader getReader() { public Reader getReader() {
return IOUtils.getReader(getInputStream()); return IOUtils.getBufferedReader(getInputStream());
} }
public InputStream getInputStream() { public InputStream getInputStream() {
......
...@@ -587,6 +587,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -587,6 +587,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
new TestCancel().runTest(this); new TestCancel().runTest(this);
new TestDatabaseEventListener().runTest(this); new TestDatabaseEventListener().runTest(this);
new TestDriver().runTest(this); new TestDriver().runTest(this);
new TestLob().runTest(this);
new TestManyJdbcObjects().runTest(this); new TestManyJdbcObjects().runTest(this);
new TestMetaData().runTest(this); new TestMetaData().runTest(this);
new TestNativeSQL().runTest(this); new TestNativeSQL().runTest(this);
......
...@@ -35,7 +35,7 @@ public class TestReader extends TestBase { ...@@ -35,7 +35,7 @@ public class TestReader extends TestBase {
InputStream in = new ReaderInputStream(r); InputStream in = new ReaderInputStream(r);
byte[] buff = IOUtils.readBytesAndClose(in, 0); byte[] buff = IOUtils.readBytesAndClose(in, 0);
InputStream in2 = new ByteArrayInputStream(buff); InputStream in2 = new ByteArrayInputStream(buff);
Reader r2 = IOUtils.getReader(in2); Reader r2 = IOUtils.getBufferedReader(in2);
String s2 = IOUtils.readStringAndClose(r2, Integer.MAX_VALUE); String s2 = IOUtils.readStringAndClose(r2, Integer.MAX_VALUE);
assertEquals(s, s2); assertEquals(s, s2);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论