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

bug-fix: Initialize ThreadLocal static member variables on declaration (fixes 55). #56

Merged
merged 3 commits into from
Oct 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.nio.charset.StandardCharsets;

public class FlattenedByteArrayFactory {
private static ThreadLocal<ByteArrayOutputStream> reusableByteArrayOutputStream;
private static final ThreadLocal<ByteArrayOutputStream> reusableByteArrayOutputStream =
ThreadLocal.withInitial(ByteArrayOutputStream::new);

/**
* Constructs a FlattenedByteArray by concatenating the given byte arrays together.
Expand Down Expand Up @@ -46,14 +47,8 @@ public static FlattenedByteArray fromStrings(String[] strings) {
return EmptyArrayUtils.EMPTY_FLATTENED_BYTE_ARRAY;
}

ByteArrayOutputStream outputStream;
if (null == reusableByteArrayOutputStream) {
outputStream = new ByteArrayOutputStream();
reusableByteArrayOutputStream = ThreadLocal.withInitial(() -> outputStream);
} else {
outputStream = reusableByteArrayOutputStream.get();
outputStream.reset();
}
ByteArrayOutputStream outputStream = reusableByteArrayOutputStream.get();
outputStream.reset();

int[] elemEndOffsets = new int[strings.length];
for (int i = 0; i < strings.length; ++i) {
Expand Down
Loading