Update BufferedWriter 59/108459/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 16 Oct 2023 15:18:53 +0000 (17:18 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 16 Oct 2023 16:46:44 +0000 (16:46 +0000)
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 <robert.varga@pantheon.tech>
netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/BufferedWriter.java

index 8258bc03b12b9b5e98aa7399e7084c72b0888daa..60957545e063be73b8e0ce0af9c7742b99ec6af5 100644 (file)
@@ -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..
  *
  * <p>
- * 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 <a href="https://bugs.openjdk.org/browse/JDK-8068498">Java 9</a>.
  *
  * <p>
- * 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.
+ *
+ * <p>
+ * 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;
         }