diff --git a/user/super/com/google/gwt/emul/java/io/StringWriter.java b/user/super/com/google/gwt/emul/java/io/StringWriter.java new file mode 100644 index 0000000000..b7dffecd00 --- /dev/null +++ b/user/super/com/google/gwt/emul/java/io/StringWriter.java @@ -0,0 +1,92 @@ +/* + * Copyright 2026 GWT Project Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package java.io; + +import static javaemul.internal.InternalPreconditions.checkArgument; + +/** + * See the official + * Java API doc for details. + */ +public class StringWriter extends Writer { + + private final StringBuffer buf = new StringBuffer(); + + public StringWriter() {} + + public StringWriter(int initialSize) { + checkArgument(initialSize >= 0); + } + + public StringBuffer getBuffer() { + return buf; + } + + @Override + public void close() throws IOException {} + + @Override + public void flush() {} + + @Override + public String toString() { + return buf.toString(); + } + + @Override + public void write(char[] chars, int offset, int count) { + IOUtils.checkOffsetAndCount(chars, offset, count); + if (count == 0) { + return; + } + buf.append(chars, offset, count); + } + + @Override + public void write(int oneChar) { + buf.append((char) oneChar); + } + + @Override + public void write(String str) { + buf.append(str); + } + + @Override + public void write(String str, int offset, int count) { + buf.append(str, offset, offset + count); + } + + @Override + public StringWriter append(char c) { + write(c); + return this; + } + + @Override + public StringWriter append(CharSequence csq) { + write(String.valueOf(csq)); + return this; + } + + @Override + public StringWriter append(CharSequence csq, int start, int end) { + if (csq == null) { + csq = "null"; + } + return append(csq.subSequence(start, end)); + } +} diff --git a/user/test/com/google/gwt/emultest/EmulSuite.java b/user/test/com/google/gwt/emultest/EmulSuite.java index 5ac7cde95d..0302c9f124 100644 --- a/user/test/com/google/gwt/emultest/EmulSuite.java +++ b/user/test/com/google/gwt/emultest/EmulSuite.java @@ -25,6 +25,7 @@ import com.google.gwt.emultest.java.io.OutputStreamTest; import com.google.gwt.emultest.java.io.OutputStreamWriterTest; import com.google.gwt.emultest.java.io.PrintStreamTest; +import com.google.gwt.emultest.java.io.StringWriterTest; import com.google.gwt.emultest.java.io.WriterTest; import com.google.gwt.emultest.java.lang.BooleanTest; import com.google.gwt.emultest.java.lang.ByteTest; @@ -84,6 +85,7 @@ OutputStreamTest.class, OutputStreamWriterTest.class, PrintStreamTest.class, + StringWriterTest.class, WriterTest.class, // -- java.lang diff --git a/user/test/com/google/gwt/emultest/java/io/StringWriterTest.java b/user/test/com/google/gwt/emultest/java/io/StringWriterTest.java new file mode 100644 index 0000000000..c9993b76ea --- /dev/null +++ b/user/test/com/google/gwt/emultest/java/io/StringWriterTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2025 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.gwt.emultest.java.io; + +import com.google.gwt.junit.client.GWTTestCase; + +import java.io.IOException; +import java.io.StringWriter; + +/** Unit test for the {@link java.io.StringWriter} emulated class. */ +public class StringWriterTest extends GWTTestCase { + + private StringWriter sw; + + @Override + public String getModuleName() { + return "com.google.gwt.emultest.EmulSuite"; + } + + @Override + protected void gwtSetUp() throws Exception { + super.gwtSetUp(); + sw = new StringWriter(); + } + + public void testClose() { + try { + sw.close(); + } catch (IOException e) { + fail("IOException closing StringWriter : " + e.getMessage()); + } + } + + public void testFlush() { + sw.flush(); + sw.write('c'); + assertEquals("Failed to flush char", "c", sw.toString()); + } + + public void testGetBuffer() { + sw.write("This is a test string"); + String sb = sw.getBuffer().toString(); + assertEquals("Incorrect buffer returned", "This is a test string", sb); + } + + public void testToString() { + sw.write("This is a test string"); + assertEquals("Incorrect string returned", "This is a test string", sw.toString()); + } + + public void testWrite_charArrayWithOffsetAndLength() { + char[] c = new char[1000]; + "This is a test string".getChars(0, 21, c, 0); + sw.write(c, 0, 21); + assertEquals("Chars not written properly", "This is a test string", sw.toString()); + } + + public void testWrite_charArray_negativeLength_throwsException() { + StringWriter obj = new StringWriter(); + try { + obj.write(new char[0], 0, -1); + fail("should have thrown IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException expected) { + } + } + + public void testWrite_charArray_negativeOffset_throwsException() { + StringWriter obj = new StringWriter(); + try { + obj.write(new char[0], -1, 0); + fail("should have thrown IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException expected) { + } + } + + public void testWrite_charArray_negativeOffsetAndLength_throwsException() { + StringWriter obj = new StringWriter(); + try { + obj.write(new char[0], -1, -1); + fail("should have thrown IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException expected) { + } + } + + public void testWrite_int() { + sw.write('c'); + assertEquals("Char not written properly", "c", sw.toString()); + } + + public void testWrite_string() { + sw.write("This is a test string"); + assertEquals("String not written properly", "This is a test string", sw.toString()); + } + + public void testWrite_stringWithOffsetAndLength() { + sw.write("This is a test string", 2, 2); + assertEquals("String not written properly", "is", sw.toString()); + } + + public void testAppend_char() throws IOException { + char testChar = ' '; + StringWriter stringWriter = new StringWriter(20); + stringWriter.append(testChar); + assertEquals(String.valueOf(testChar), stringWriter.toString()); + stringWriter.close(); + } + + public void testAppend_charSequence() throws IOException { + String testString = "My Test String"; + StringWriter stringWriter = new StringWriter(20); + stringWriter.append(testString); + assertEquals(String.valueOf(testString), stringWriter.toString()); + stringWriter.close(); + } + + public void testAppend_charSequenceWithStartAndEnd() throws IOException { + String testString = "My Test String"; + StringWriter stringWriter = new StringWriter(20); + stringWriter.append(testString, 1, 3); + assertEquals(testString.substring(1, 3), stringWriter.toString()); + stringWriter.close(); + } +}