From ed5e89d5155a2245464f6a8b8c47cf4711908760 Mon Sep 17 00:00:00 2001 From: Oleksandr Zhelezniak Date: Mon, 17 Mar 2025 13:49:15 +0200 Subject: [PATCH 1/2] fix: FastWriter missing characters on buffer limit --- .../com/cedarsoftware/util/FastWriter.java | 1 + .../java/com/cedarsoftware/util/TestIO.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main/java/com/cedarsoftware/util/FastWriter.java b/src/main/java/com/cedarsoftware/util/FastWriter.java index 807d9a60f..88ef2fbf8 100644 --- a/src/main/java/com/cedarsoftware/util/FastWriter.java +++ b/src/main/java/com/cedarsoftware/util/FastWriter.java @@ -110,6 +110,7 @@ public void write(String str, int off, int len) throws IOException { str.getChars(off, off + available, cb, nextChar); off += available; len -= available; + nextChar = cb.length; flushBuffer(); } diff --git a/src/test/java/com/cedarsoftware/util/TestIO.java b/src/test/java/com/cedarsoftware/util/TestIO.java index b69938f84..e1b005f92 100644 --- a/src/test/java/com/cedarsoftware/util/TestIO.java +++ b/src/test/java/com/cedarsoftware/util/TestIO.java @@ -1,5 +1,6 @@ package com.cedarsoftware.util; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; @@ -10,6 +11,8 @@ import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class TestIO { @@ -78,6 +81,22 @@ public void testFastWriter() throws Exception assert content.equals(new String(baos.toByteArray(), StandardCharsets.UTF_8)); } + @Test + void fastWriterBufferLimitValue() throws IOException { + final String line511 = IntStream.range(0, 63).mapToObj(it -> "a").collect(Collectors.joining()); + final String nextLine = "Tbbb"; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + FastWriter out = new FastWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8), 64); + out.write(line511); + out.write(nextLine); + out.close(); + + final String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8); + + Assertions.assertEquals(line511+nextLine, actual); + } + @Test public void testFastWriterCharBuffer() throws Exception { From 0c897ba426e6ce941d3e1098e947dd3807bfd31c Mon Sep 17 00:00:00 2001 From: Oleksandr Zhelezniak Date: Mon, 17 Mar 2025 14:05:37 +0200 Subject: [PATCH 2/2] fix: FastWriter don't flush in case if exceed the limit --- .../java/com/cedarsoftware/util/FastWriter.java | 3 +++ src/test/java/com/cedarsoftware/util/TestIO.java | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/com/cedarsoftware/util/FastWriter.java b/src/main/java/com/cedarsoftware/util/FastWriter.java index 88ef2fbf8..61ad3fef1 100644 --- a/src/main/java/com/cedarsoftware/util/FastWriter.java +++ b/src/main/java/com/cedarsoftware/util/FastWriter.java @@ -101,6 +101,9 @@ public void write(String str, int off, int len) throws IOException { if (nextChar + len <= cb.length) { str.getChars(off, off + len, cb, nextChar); nextChar += len; + if (nextChar == cb.length) { + flushBuffer(); + } return; } diff --git a/src/test/java/com/cedarsoftware/util/TestIO.java b/src/test/java/com/cedarsoftware/util/TestIO.java index e1b005f92..037f10fbc 100644 --- a/src/test/java/com/cedarsoftware/util/TestIO.java +++ b/src/test/java/com/cedarsoftware/util/TestIO.java @@ -97,6 +97,22 @@ void fastWriterBufferLimitValue() throws IOException { Assertions.assertEquals(line511+nextLine, actual); } + @Test + void fastWriterBufferSizeIsEqualToLimit() throws IOException { + final String line511 = IntStream.range(0, 64).mapToObj(it -> "a").collect(Collectors.joining()); + final String nextLine = "Tbbb"; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + FastWriter out = new FastWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8), 64); + out.write(line511); + out.write(nextLine); + out.close(); + + final String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8); + + Assertions.assertEquals(line511+nextLine, actual); + } + @Test public void testFastWriterCharBuffer() throws Exception {