-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #867 from Simulant87/863-improve-toString-performa…
…nce-StringBuilderWriter Improve toString Performance: Use StringBuilderWriter for toString methods
- Loading branch information
Showing
5 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 @@ | ||
package org.json; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
/** | ||
* Performance optimised alternative for {@link java.io.StringWriter} | ||
* using internally a {@link StringBuilder} instead of a {@link StringBuffer}. | ||
*/ | ||
public class StringBuilderWriter extends Writer { | ||
private final StringBuilder builder; | ||
|
||
/** | ||
* Create a new string builder writer using the default initial string-builder buffer size. | ||
*/ | ||
public StringBuilderWriter() { | ||
builder = new StringBuilder(); | ||
lock = builder; | ||
} | ||
|
||
/** | ||
* Create a new string builder writer using the specified initial string-builder buffer size. | ||
* | ||
* @param initialSize The number of {@code char} values that will fit into this buffer | ||
* before it is automatically expanded | ||
* | ||
* @throws IllegalArgumentException If {@code initialSize} is negative | ||
*/ | ||
public StringBuilderWriter(int initialSize) { | ||
builder = new StringBuilder(initialSize); | ||
lock = builder; | ||
} | ||
|
||
@Override | ||
public void write(int c) { | ||
builder.append((char) c); | ||
} | ||
|
||
@Override | ||
public void write(char[] cbuf, int offset, int length) { | ||
if ((offset < 0) || (offset > cbuf.length) || (length < 0) || | ||
((offset + length) > cbuf.length) || ((offset + length) < 0)) { | ||
throw new IndexOutOfBoundsException(); | ||
} else if (length == 0) { | ||
return; | ||
} | ||
builder.append(cbuf, offset, length); | ||
} | ||
|
||
@Override | ||
public void write(String str) { | ||
builder.append(str); | ||
} | ||
|
||
@Override | ||
public void write(String str, int offset, int length) { | ||
builder.append(str, offset, offset + length); | ||
} | ||
|
||
@Override | ||
public StringBuilderWriter append(CharSequence csq) { | ||
write(String.valueOf(csq)); | ||
return this; | ||
} | ||
|
||
@Override | ||
public StringBuilderWriter append(CharSequence csq, int start, int end) { | ||
if (csq == null) { | ||
csq = "null"; | ||
} | ||
return append(csq.subSequence(start, end)); | ||
} | ||
|
||
@Override | ||
public StringBuilderWriter append(char c) { | ||
write(c); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return builder.toString(); | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
} | ||
} |
This file contains 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
This file contains 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,60 @@ | ||
package org.json.junit; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.json.StringBuilderWriter; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class StringBuilderWriterTest { | ||
private StringBuilderWriter writer; | ||
|
||
@Before | ||
public void setUp() { | ||
writer = new StringBuilderWriter(); | ||
} | ||
|
||
@Test | ||
public void testWriteChar() { | ||
writer.write('a'); | ||
assertEquals("a", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testWriteCharArray() { | ||
char[] chars = {'a', 'b', 'c'}; | ||
writer.write(chars, 0, 3); | ||
assertEquals("abc", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testWriteString() { | ||
writer.write("hello"); | ||
assertEquals("hello", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testWriteStringWithOffsetAndLength() { | ||
writer.write("hello world", 6, 5); | ||
assertEquals("world", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testAppendCharSequence() { | ||
writer.append("hello"); | ||
assertEquals("hello", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testAppendCharSequenceWithStartAndEnd() { | ||
CharSequence csq = "hello world"; | ||
writer.append(csq, 6, 11); | ||
assertEquals("world", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testAppendChar() { | ||
writer.append('a'); | ||
assertEquals("a", writer.toString()); | ||
} | ||
} |