-
Notifications
You must be signed in to change notification settings - Fork 386
Add StringWriter #10333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
treblereel
wants to merge
3
commits into
gwtproject:main
Choose a base branch
from
treblereel:string_writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−0
Open
Add StringWriter #10333
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| * 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)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
user/test/com/google/gwt/emultest/java/io/StringWriterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe latest stable LTS? Alternatively 17 would also make some sense as it's currently the highest possible value of source level for GWT.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.