From 8d8e2a09e93198199e9da84befb7491e3f6fda1a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 16 Oct 2023 17:18:53 +0200 Subject: [PATCH] Update BufferedWriter Clarify why this class is still present, as its primary motivation has been removed in Java 9. Also use String.formatted() and drop the use of checkArgument(). Change-Id: I3312cc6690dd6fda9d6a28c7915c9f23f6f4b260 Signed-off-by: Robert Varga --- .../nettyutil/handler/BufferedWriter.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/BufferedWriter.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/BufferedWriter.java index 8258bc03b1..60957545e0 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/BufferedWriter.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/BufferedWriter.java @@ -7,25 +7,25 @@ */ package org.opendaylight.netconf.nettyutil.handler; -import com.google.common.base.Preconditions; import java.io.IOException; import java.io.Writer; /** - * Custom BufferedWriter optimized for netconf pipeline implemented instead of default BufferedWriter provided by jdk. + * Custom BufferedWriter optimized for NETCONF pipeline implemented instead of default BufferedWriter provided by JDK.. * *

- * The line separator instance field in java.io.BufferedWriter is - * assigned using AccessController and takes considerable amount of time especially - * if lots of BufferedWriters are created in the system. + * In versions up to and including Java 8, java.io.BufferedWriter initialized its lineSeparator field using + * AccessController and takes considerable amount of time especially if lots of BufferedWriters are created in the + * system. This has been rectified in Java 9. * *

- * This implementation should only be used if newLine method is not required - * such as netconf message to XML encoders. - * Methods in this implementation are not synchronized. + * Despite that issue having been fixed we retain this implementation because its methods are not synchronized, hence + * offer a tad better performance. + * + *

+ * This implementation should only be used if newLine method is not required such as NETCONF message to XML encoders. */ public final class BufferedWriter extends Writer { - private static final int DEFAULT_CHAR_BUFFER_SIZE = 8192; private final Writer writer; @@ -40,10 +40,12 @@ public final class BufferedWriter extends Writer { public BufferedWriter(final Writer writer, final int bufferSize) { super(writer); - Preconditions.checkArgument(bufferSize > 0, "Buffer size <= 0"); + if (bufferSize <= 0) { + throw new IllegalArgumentException("Buffer size <= 0"); + } this.writer = writer; - this.buffer = new char[bufferSize]; this.bufferSize = bufferSize; + buffer = new char[bufferSize]; } private void flushBuffer() throws IOException { @@ -68,7 +70,7 @@ public final class BufferedWriter extends Writer { if (offset < 0 || offset > buffer.length || length < 0 || offset + length > buffer.length || offset + length < 0) { throw new IndexOutOfBoundsException( - String.format("Buffer size: %d, Offset: %d, Length: %d", buffer.length, offset, length)); + "Buffer size: %d, Offset: %d, Length: %d".formatted(buffer.length, offset, length)); } else if (length == 0) { return; } -- 2.36.6