Skip to content
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

fix: FastWriter missing characters on buffer limit #115

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/cedarsoftware/util/FastWriter.java
Original file line number Diff line number Diff line change
@@ -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;
}

@@ -110,6 +113,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();
}

35 changes: 35 additions & 0 deletions src/test/java/com/cedarsoftware/util/TestIO.java
Original file line number Diff line number Diff line change
@@ -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,38 @@ 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
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
{