From a99ce7cae5a040bbf71486ab1472f61ef0ced76c Mon Sep 17 00:00:00 2001 From: Per Bothner Date: Fri, 16 Apr 1999 11:34:58 -0700 Subject: [PATCH] InputStreamReader.java (): Set super.in correctly. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit � * java/io/InputStreamReader.java (): Set super.in correctly. * java/io/OutputStreamWriter.java (): 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 --- libjava/java/lang/natString.cc | 79 ++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/libjava/java/lang/natString.cc b/libjava/java/lang/natString.cc index 8aad9ee4b8c..a5984856dff 100644 --- a/libjava/java/lang/natString.cc +++ b/libjava/java/lang/natString.cc @@ -24,6 +24,8 @@ details. */ #include #include #include +#include +#include #include 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 || offset + count > data_size) 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); - data = array; - boffset = (char *) elements (array) - (char *) array; - // FIXME: this can throw IOException. - this->count = ir->read(array, 0, count); - - // In case read() doesn't read anything, change -1 for EOF to a count of 0. - if (this->count < 0) - this->count = 0; + gnu::gcj::convert::BytesToUnicode *converter + = gnu::gcj::convert::BytesToUnicode::getDecoder(encoding); + jint outpos = 0; + int avail = count; + converter->setInput(bytes, offset, offset+count); + while (converter->inpos < converter->inlength) + { + 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 @@ -448,15 +462,34 @@ java::lang::String::getChars(jint srcBegin, jint srcEnd, jbyteArray java::lang::String::getBytes (jstring enc) { - java::io::ByteArrayOutputStream *os - = new java::io::ByteArrayOutputStream(length ()); - java::io::OutputStreamWriter *ow - = new java::io::OutputStreamWriter(os, enc); - - ow->write(this, 0, length ()); - ow->flush(); - - return os->toByteArray(); + jint todo = length(); + jint buflen = todo; + jbyteArray buffer = JvNewByteArray(todo); + jint bufpos = 0; + jint offset = 0; + gnu::gcj::convert::UnicodeToBytes *converter + = gnu::gcj::convert::UnicodeToBytes::getEncoder(enc); + while (todo > 0) + { + 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 -- 2.30.2