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: r26508
This commit is contained in:
Per Bothner 1999-04-16 11:34:58 -07:00
parent 78638e240e
commit a99ce7cae5

View file

@ -24,6 +24,8 @@ details. */
#include <java/io/OutputStreamWriter.h> #include <java/io/OutputStreamWriter.h>
#include <java/io/ByteArrayInputStream.h> #include <java/io/ByteArrayInputStream.h>
#include <java/io/InputStreamReader.h> #include <java/io/InputStreamReader.h>
#include <gnu/gcj/convert/UnicodeToBytes.h>
#include <gnu/gcj/convert/BytesToUnicode.h>
#include <jvm.h> #include <jvm.h>
static jstring* strhash = NULL; static jstring* strhash = NULL;
@ -381,21 +383,33 @@ java::lang::String::init (jbyteArray bytes, jint offset, jint count,
if (offset < 0 || count < 0 || offset + count < 0 if (offset < 0 || count < 0 || offset + count < 0
|| offset + count > data_size) || offset + count > data_size)
JvThrow (new StringIndexOutOfBoundsException); JvThrow (new StringIndexOutOfBoundsException);
java::io::ByteArrayInputStream *b
= new java::io::ByteArrayInputStream (bytes, offset, count);
java::io::InputStreamReader *ir
= new java::io::InputStreamReader (b, encoding);
// FIXME: we allocate too much here in some cases.
jcharArray array = JvNewCharArray (count); jcharArray array = JvNewCharArray (count);
data = array; gnu::gcj::convert::BytesToUnicode *converter
boffset = (char *) elements (array) - (char *) array; = gnu::gcj::convert::BytesToUnicode::getDecoder(encoding);
// FIXME: this can throw IOException. jint outpos = 0;
this->count = ir->read(array, 0, count); int avail = count;
converter->setInput(bytes, offset, offset+count);
// In case read() doesn't read anything, change -1 for EOF to a count of 0. while (converter->inpos < converter->inlength)
if (this->count < 0) {
this->count = 0; int done = converter->read(array, outpos, avail);
if (done == 0)
{
jint new_size = 2 * (outpos + avail);
jcharArray new_array = JvNewCharArray (new_size);
memcpy (elements (new_array), elements (array),
outpos * sizeof(jchar));
array = new_array;
avail = new_size - outpos;
}
else
{
outpos += done;
avail -= done;
}
}
this->data = array;
this->boffset = (char *) elements (array) - (char *) array;
this->count = outpos;
} }
jboolean jboolean
@ -448,15 +462,34 @@ java::lang::String::getChars(jint srcBegin, jint srcEnd,
jbyteArray jbyteArray
java::lang::String::getBytes (jstring enc) java::lang::String::getBytes (jstring enc)
{ {
java::io::ByteArrayOutputStream *os jint todo = length();
= new java::io::ByteArrayOutputStream(length ()); jint buflen = todo;
java::io::OutputStreamWriter *ow jbyteArray buffer = JvNewByteArray(todo);
= new java::io::OutputStreamWriter(os, enc); jint bufpos = 0;
jint offset = 0;
ow->write(this, 0, length ()); gnu::gcj::convert::UnicodeToBytes *converter
ow->flush(); = gnu::gcj::convert::UnicodeToBytes::getEncoder(enc);
while (todo > 0)
return os->toByteArray(); {
converter->setOutput(buffer, bufpos);
int converted = converter->write(this, offset, todo, NULL);
if (converted == 0)
{
jbyteArray newbuffer = JvNewByteArray(2 * buflen);
memcpy (elements (newbuffer), elements (buffer), bufpos);
buffer = newbuffer;
}
else
{
offset += converted;
todo -= converted;
}
}
if (bufpos == buflen)
return buffer;
jbyteArray result = JvNewByteArray(bufpos);
memcpy (elements (result), elements (buffer), bufpos);
return result;
} }
void void