diff --git a/xstream-distribution/src/content/changes.html b/xstream-distribution/src/content/changes.html
index 4b6413e09..1d49167aa 100644
--- a/xstream-distribution/src/content/changes.html
+++ b/xstream-distribution/src/content/changes.html
@@ -112,6 +112,7 @@
Minor changes
- GHPR:#331, GHI:#326: Fix handling of empty java.util.concurrent.atomic.AtomicReference (by Alex Blekhman of Atlassian).
+ - GHPR:#334: Fix remaining buffer size calculation in QuickWriter (by Higuchi Yuta).
1.4.20
diff --git a/xstream/src/java/com/thoughtworks/xstream/core/util/QuickWriter.java b/xstream/src/java/com/thoughtworks/xstream/core/util/QuickWriter.java
index cb6572883..4a44bd010 100644
--- a/xstream/src/java/com/thoughtworks/xstream/core/util/QuickWriter.java
+++ b/xstream/src/java/com/thoughtworks/xstream/core/util/QuickWriter.java
@@ -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;
@@ -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());
@@ -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);
@@ -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);
diff --git a/xstream/src/test/com/thoughtworks/xstream/core/util/QuickWriterTest.java b/xstream/src/test/com/thoughtworks/xstream/core/util/QuickWriterTest.java
index cdcb50e97..9324632a6 100644
--- a/xstream/src/test/com/thoughtworks/xstream/core/util/QuickWriterTest.java
+++ b/xstream/src/test/com/thoughtworks/xstream/core/util/QuickWriterTest.java
@@ -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
@@ -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());
+ }
+ }
}