InputStreamReader.java (<init>): Set super.in correctly.

�
	* java/io/InputStreamReader.java (<init>):  Set super.in correctly.
	* java/io/OutputStreamWriter.java (<init>):  Set super.in correctly.
	(writeChars):  Don't be quite so eager to flush.
	* java/io/PrintStream.java:  Rewrite.  Now more similar to
	OutputStreamWriter, using explicit UnicodeToBytes converter.
	Also, autoflush does not need to flush so often.
	* java/lang/natString.cc (getBytes):  More efficient algorithm.
 	(init(jbyteArray,jint,jint,jstring)):  More efficient.

From-SVN: r26509
This commit is contained in:
Per Bothner 1999-04-16 11:35:02 -07:00
parent a99ce7cae5
commit 839df96120
3 changed files with 120 additions and 55 deletions

View File

@ -44,9 +44,9 @@ public class InputStreamReader extends Reader
private InputStreamReader(InputStream in, BytesToUnicode decoder) private InputStreamReader(InputStream in, BytesToUnicode decoder)
{ {
super(in); super((this.in = (in instanceof BufferedInputStream
this.in = in instanceof BufferedInputStream ? (BufferedInputStream) in ? (BufferedInputStream) in
: new BufferedInputStream(in, 250); : new BufferedInputStream(in, 250))));
converter = decoder; converter = decoder;
converter.setInput(this.in.buf, 0, 0); converter.setInput(this.in.buf, 0, 0);
} }

View File

@ -32,9 +32,9 @@ public class OutputStreamWriter extends Writer
private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder) private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
{ {
super(out); super((this.out = (out instanceof BufferedOutputStream
this.out = out instanceof BufferedOutputStream ? (BufferedOutputStream) out ? (BufferedOutputStream) out
: new BufferedOutputStream(out, 2048); : new BufferedOutputStream(out, 250))));
this.converter = encoder; this.converter = encoder;
} }
@ -90,12 +90,17 @@ public class OutputStreamWriter extends Writer
} }
} }
/** Writes characters through to the inferior BufferedOutputStream.
* Ignores wcount and the work buffer. */
private void writeChars(char[] buf, int offset, int count) private void writeChars(char[] buf, int offset, int count)
throws IOException throws IOException
{ {
while (count > 0) while (count > 0)
{ {
if (out.count != 0) // We must flush if out.count == out.buf.length.
// It is probably a good idea to flush if out.buf is almost full.
// This test is an approximation for "almost full".
if (out.count + count >= out.buf.length)
{ {
out.flush(); out.flush();
if (out.count != 0) if (out.count != 0)

View File

@ -9,6 +9,7 @@ Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */ details. */
package java.io; package java.io;
import gnu.gcj.convert.UnicodeToBytes;
/** /**
* @author Tom Tromey <tromey@cygnus.com> * @author Tom Tromey <tromey@cygnus.com>
@ -22,6 +23,10 @@ package java.io;
public class PrintStream extends FilterOutputStream public class PrintStream extends FilterOutputStream
{ {
/* Notice the implementation is quite similar to OutputStreamWriter.
* This leads to some minor duplication, because neither inherits
* from the other, and we want to maximize performance. */
public boolean checkError () public boolean checkError ()
{ {
return error; return error;
@ -51,15 +56,15 @@ public class PrintStream extends FilterOutputStream
} }
} }
private final void print (String str, boolean check_term) private synchronized void print (String str, boolean println)
{ {
try try
{ {
write(str.getBytes()); writeChars(str, 0, str.length());
if (check_term if (println)
&& auto_flush writeChars(line_separator, 0, line_separator.length);
&& str.indexOf(line_separator) != -1) if (auto_flush)
flush (); flush();
} }
catch (IOException e) catch (IOException e)
{ {
@ -67,122 +72,177 @@ public class PrintStream extends FilterOutputStream
} }
} }
private synchronized void print (char[] chars, int pos, int len,
boolean println)
{
try
{
writeChars(chars, pos, len);
if (println)
writeChars(line_separator, 0, line_separator.length);
if (auto_flush)
flush();
}
catch (IOException e)
{
setError ();
}
}
/** Writes characters through to the inferior BufferedOutputStream. */
private void writeChars(char[] buf, int offset, int count)
throws IOException
{
while (count > 0)
{
// We must flush if out.count == out.buf.length.
// It is probably a good idea to flush if out.buf is almost full.
// This test is an approximation for "almost full".
if (out.count + count >= out.buf.length)
{
out.flush();
if (out.count != 0)
throw new IOException("unable to flush output byte buffer");
}
converter.setOutput(out.buf, out.count);
int converted = converter.write(buf, offset, count);
offset += converted;
count -= converted;
out.count = converter.count;
}
}
private void writeChars(String str, int offset, int count)
throws IOException
{
while (count > 0)
{
// We must flush if out.count == out.buf.length.
// It is probably a good idea to flush if out.buf is almost full.
// This test is an approximation for "almost full".
if (out.count + count >= out.buf.length)
{
out.flush();
if (out.count != 0)
throw new IOException("unable to flush output byte buffer");
}
converter.setOutput(out.buf, out.count);
int converted = converter.write(str, offset, count, work);
offset += converted;
count -= converted;
out.count = converter.count;
}
}
public void print (boolean bool) public void print (boolean bool)
{ {
print (String.valueOf(bool), false); print(String.valueOf(bool), false);
} }
public void print (int inum) public void print (int inum)
{ {
print (String.valueOf(inum), false); print(String.valueOf(inum), false);
} }
public void print (long lnum) public void print (long lnum)
{ {
print (String.valueOf(lnum), false); print(String.valueOf(lnum), false);
} }
public void print (float fnum) public void print (float fnum)
{ {
print (String.valueOf(fnum), false); print(String.valueOf(fnum), false);
} }
public void print (double dnum) public void print (double dnum)
{ {
print (String.valueOf(dnum), false); print(String.valueOf(dnum), false);
} }
public void print (Object obj) public void print (Object obj)
{ {
print (String.valueOf(obj), false); print(obj == null ? "null" : obj.toString(), false);
} }
public void print (String str) public void print (String str)
{ {
print (str == null ? "null" : str, true); print(str == null ? "null" : str, false);
} }
public void print (char ch) public synchronized void print (char ch)
{ {
print (String.valueOf(ch), true); work[0] = ch;
print(work, 0, 1, false);
} }
public void print (char[] charArray) public void print (char[] charArray)
{ {
print (String.valueOf(charArray), true); print(charArray, 0, charArray.length, false);
} }
public void println () public void println ()
{ {
print (line_separator, false); print(line_separator, 0, line_separator.length, false);
if (auto_flush)
flush ();
} }
public void println (boolean bool) public void println (boolean bool)
{ {
print (String.valueOf(bool), false); print(String.valueOf(bool), true);
println ();
} }
public void println (int inum) public void println (int inum)
{ {
print (String.valueOf(inum), false); print(String.valueOf(inum), true);
println ();
} }
public void println (long lnum) public void println (long lnum)
{ {
print (String.valueOf(lnum), false); print(String.valueOf(lnum), true);
println ();
} }
public void println (float fnum) public void println (float fnum)
{ {
print (String.valueOf(fnum), false); print(String.valueOf(fnum), true);
println ();
} }
public void println (double dnum) public void println (double dnum)
{ {
print (String.valueOf(dnum), false); print(String.valueOf(dnum), true);
println ();
} }
public void println (Object obj) public void println (Object obj)
{ {
print (String.valueOf(obj), false); print(obj == null ? "null" : obj.toString(), true);
println ();
} }
public void println (String str) public void println (String str)
{ {
print (str == null ? "null" : str, false); print (str == null ? "null" : str, true);
println (); println ();
} }
public void println (char ch) public synchronized void println (char ch)
{ {
print (String.valueOf(ch), false); work[0] = ch;
println (); print(work, 0, 1, true);
} }
public void println (char[] charArray) public void println (char[] charArray)
{ {
print (String.valueOf(charArray), false); print(charArray, 0, charArray.length, true);
println ();
} }
public PrintStream (OutputStream out) public PrintStream (OutputStream out)
{ {
super (out); this(out, false);
error = false;
auto_flush = false;
} }
public PrintStream (OutputStream out, boolean af) public PrintStream (OutputStream out, boolean af)
{ {
super (out); super ((this.out = (out instanceof BufferedOutputStream
? (BufferedOutputStream) out
: new BufferedOutputStream(out, 250))));
converter = UnicodeToBytes.getDefaultEncoder();
error = false; error = false;
auto_flush = af; auto_flush = af;
} }
@ -197,7 +257,6 @@ public class PrintStream extends FilterOutputStream
try try
{ {
out.write(oneByte); out.write(oneByte);
// JCL says to do this. I think it is wrong. FIXME.
if (auto_flush && oneByte == '\n') if (auto_flush && oneByte == '\n')
out.flush(); out.flush();
} }
@ -212,10 +271,6 @@ public class PrintStream extends FilterOutputStream
try try
{ {
out.write(buffer, offset, count); out.write(buffer, offset, count);
// FIXME: JCL says to flush. But elsewhere the JCL says to
// use write to write the stringified form of an object, and
// only to flush if that string contains the line separator.
// How to resolve the contradiction?
if (auto_flush) if (auto_flush)
out.flush(); out.flush();
} }
@ -225,12 +280,17 @@ public class PrintStream extends FilterOutputStream
} }
} }
BufferedOutputStream out;
UnicodeToBytes converter;
char[] work = new char[100];
// True if error occurred. // True if error occurred.
private boolean error; private boolean error;
// True if auto-flush. // True if auto-flush.
private boolean auto_flush; private boolean auto_flush;
// Line separator string. // Line separator string.
private static final String line_separator private static final char[] line_separator
= System.getProperty("line.separator"); = System.getProperty("line.separator").toCharArray();
} }