Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions user/super/com/google/gwt/emul/java/io/StringWriter.java
Original file line number Diff line number Diff line change
@@ -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 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/StringWriter.html">the official

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* See <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/StringWriter.html">the official
* See <a href="https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/io/StringWriter.html">the official

Maybe latest stable LTS? Alternatively 17 would also make some sense as it's currently the highest possible value of source level for GWT.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, its supersource, so the package and classname is really all we need... More important would be any notes about what makes the GWT impl unique (though this class won't have much of that).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class was taken from j2cl and adapted for gwt. In J2CL, the reference points to java.sun.com (which redirects to Oracle)

But I think that's probably the least significant of the issues that could exist

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I worry about the licensing because it does matter, but the code looks good to me.

If the tests are from google though, they should be attributed as such rather than replace the owner field.

* Java API doc</a> 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));
}
}
2 changes: 2 additions & 0 deletions user/test/com/google/gwt/emultest/EmulSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,7 @@
OutputStreamTest.class,
OutputStreamWriterTest.class,
PrintStreamTest.class,
StringWriterTest.class,
WriterTest.class,

// -- java.lang
Expand Down
136 changes: 136 additions & 0 deletions user/test/com/google/gwt/emultest/java/io/StringWriterTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading