Skip to content

Commit

Permalink
QuickWriter: Fix remaining buffer size calculation. Closes #334.
Browse files Browse the repository at this point in the history
  • Loading branch information
y-higuchi authored and joehni committed Sep 23, 2023
1 parent 2692d75 commit 59b50d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ <h2>Minor changes</h2>

<ul>
<li>GHPR:#331, GHI:#326: Fix handling of empty java.util.concurrent.atomic.AtomicReference (by Alex Blekhman of Atlassian).</li>
<li>GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).</li>
</ul>

<h1 id="1.4.20">1.4.20</h1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (C) 2004, 2005, 2006 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2014 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2014, 2023 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*
* Created on 07. March 2004 by Joe Walnes
*/
package com.thoughtworks.xstream.core.util;
Expand Down Expand Up @@ -35,7 +35,7 @@ public QuickWriter(final Writer writer, final int bufferSize) {

public void write(final String str) {
final int len = str.length();
if (pointer + len >= buffer.length) {
if (pointer + len > buffer.length) {
flush();
if (len > buffer.length) {
raw(str.toCharArray());
Expand All @@ -47,7 +47,7 @@ public void write(final String str) {
}

public void write(final char c) {
if (pointer + 1 >= buffer.length) {
if (pointer + 1 > buffer.length) {
flush();
if (buffer.length == 0) {
raw(c);
Expand All @@ -59,7 +59,7 @@ public void write(final char c) {

public void write(final char[] c) {
final int len = c.length;
if (pointer + len >= buffer.length) {
if (pointer + len > buffer.length) {
flush();
if (len > buffer.length) {
raw(c);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009, 2018 XStream Committers.
* Copyright (C) 2009, 2018, 2023 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -31,4 +31,44 @@ public void testUnbuffered() {
assertEquals(stringWriter.toString(), "Joe Walnes");
}
}

public void testBufferingChar() {
final StringWriter stringWriter = new StringWriter();
try (QuickWriter writer = new QuickWriter(stringWriter, 1024)) {
final char[] filler = new char[1023];
writer.write(filler);
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
writer.write(' ');
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
writer.write(' ');
assertEquals("flushed", 1024, stringWriter.getBuffer().length());
}
}

public void testBufferingCharArray() {
final StringWriter stringWriter = new StringWriter();
try (QuickWriter writer = new QuickWriter(stringWriter, 1024)) {
final char[] filler = new char[1023];
writer.write(filler);
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
final char[] one = {' '};
writer.write(one);
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
writer.write(one);
assertEquals("flushed", 1024, stringWriter.getBuffer().length());
}
}

public void testBufferingString() {
final StringWriter stringWriter = new StringWriter();
try (QuickWriter writer = new QuickWriter(stringWriter, 1024)) {
final char[] filler = new char[1023];
writer.write(filler);
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
writer.write(" ");
assertEquals("not flushed yet", 0, stringWriter.getBuffer().length());
writer.write(" ");
assertEquals("flushed", 1024, stringWriter.getBuffer().length());
}
}
}

0 comments on commit 59b50d8

Please sign in to comment.