From de2f2007c49c8e33c9cd819653c91efd10d1cfed Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sun, 23 Feb 2025 20:45:06 -0500
Subject: [PATCH] Console: Improve writing efficiency

replace out.write(buf.toString) with out.append(buf)
to eliminate extra copy and object churn
---
 .../java/src/org/klomp/snark/web/I2PSnarkServlet.java  | 10 +++++-----
 .../java/src/net/i2p/router/news/PersistNews.java      |  2 +-
 .../net/i2p/router/update/ConsoleUpdateManager.java    |  2 +-
 .../net/i2p/router/web/helpers/BanlistRenderer.java    |  4 ++--
 .../i2p/router/web/helpers/ConfigKeyringHelper.java    |  2 +-
 .../net/i2p/router/web/helpers/ConfigNavHelper.java    |  2 +-
 .../src/net/i2p/router/web/helpers/JobQueueHelper.java |  6 +++---
 .../net/i2p/router/web/helpers/OldConsoleHelper.java   |  2 +-
 .../src/net/i2p/router/web/helpers/ProfilesHelper.java |  2 +-
 .../src/net/i2p/router/web/helpers/StatsGenerator.java | 10 +++++-----
 .../net/i2p/router/web/helpers/SummaryBarRenderer.java |  2 +-
 .../i2p/router/crypto/TransientSessionKeyManager.java  |  6 +++---
 .../src/net/i2p/router/crypto/ratchet/RatchetSKM.java  |  6 +++---
 .../src/net/i2p/router/startup/RouterAppManager.java   |  2 +-
 router/java/src/net/i2p/router/sybil/PersistSybil.java |  2 +-
 15 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
index 810ebc0c09..1d1df1ecb6 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java
@@ -908,7 +908,7 @@ public class I2PSnarkServlet extends BasicServlet {
     private void writeHiddenInputs(PrintWriter out, HttpServletRequest req, String action) {
         StringBuilder buf = new StringBuilder(256);
         writeHiddenInputs(buf, req, action);
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     /**
@@ -1995,7 +1995,7 @@ public class I2PSnarkServlet extends BasicServlet {
             buf.append("<a href=\"").append(encodedBaseName)
                .append("/\" title=\"").append(_t("Torrent details"))
                .append("\">");
-            out.write(buf.toString());
+            out.append(buf);
         }
         String icon;
         if (isMultiFile)
@@ -2024,7 +2024,7 @@ public class I2PSnarkServlet extends BasicServlet {
                    .append("\">");
                 toThemeImg(buf, "comment", "", "");
                 buf.append("</a>");
-                out.write(buf.toString());
+                out.append(buf);
             }
         }
 
@@ -2048,7 +2048,7 @@ public class I2PSnarkServlet extends BasicServlet {
             else
                 buf.append(_t("Open file"));
             buf.append("\">");
-            out.write(buf.toString());
+            out.append(buf);
         }
         out.write(DataHelper.escapeHTML(basename));
         if (remaining == 0 || isMultiFile)
@@ -3048,7 +3048,7 @@ public class I2PSnarkServlet extends BasicServlet {
                    "</td></tr>" +
                    "<tr class=\"spacer\"><td colspan=\"7\">&nbsp;</td></tr>\n" +  // spacer
                    "</table></div></div></form>\n");
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     private void writeConfigLink(PrintWriter out) throws IOException {
diff --git a/apps/routerconsole/java/src/net/i2p/router/news/PersistNews.java b/apps/routerconsole/java/src/net/i2p/router/news/PersistNews.java
index f4ca28dc54..93095cd835 100644
--- a/apps/routerconsole/java/src/net/i2p/router/news/PersistNews.java
+++ b/apps/routerconsole/java/src/net/i2p/router/news/PersistNews.java
@@ -77,7 +77,7 @@ class PersistNews {
                 out = new OutputStreamWriter(new GZIPOutputStream(new SecureFileOutputStream(file)), "UTF-8");
                 out.write(XML_START);
                 XMLParser.toString(buf, entry);
-                out.write(buf.toString());
+                out.append(buf);
                 buf.setLength(0);
             } catch (IOException ioe) {
                 if (log.shouldWarn())
diff --git a/apps/routerconsole/java/src/net/i2p/router/update/ConsoleUpdateManager.java b/apps/routerconsole/java/src/net/i2p/router/update/ConsoleUpdateManager.java
index d856756ca9..a8181614bd 100644
--- a/apps/routerconsole/java/src/net/i2p/router/update/ConsoleUpdateManager.java
+++ b/apps/routerconsole/java/src/net/i2p/router/update/ConsoleUpdateManager.java
@@ -1824,7 +1824,7 @@ public class ConsoleUpdateManager implements UpdateManager, RouterApp {
         buf.append("<div class=\"debug_container\">");
         toString(buf, _downloaders);
         buf.append("</div>");
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     /** debug */
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/BanlistRenderer.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/BanlistRenderer.java
index 7696a33a8a..0607c1650d 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/BanlistRenderer.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/BanlistRenderer.java
@@ -39,7 +39,7 @@ class BanlistRenderer {
         buf.append("<h3 id=\"bannedpeers\">").append(_t("Banned Peers"));
         if (entries.isEmpty()) {
             buf.append("</h3><i>").append(_t("none")).append("</i>");
-            out.write(buf.toString());
+            out.append(buf);
             return;
         } else {
             buf.append(" (").append(entries.size()).append(")</h3>");
@@ -81,7 +81,7 @@ class BanlistRenderer {
             buf.append("</li>\n");
         }
         buf.append("</ul>\n");
-        out.write(buf.toString());
+        out.append(buf);
         out.flush();
     }
 
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigKeyringHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigKeyringHelper.java
index 3f5fc177b9..450ad59344 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigKeyringHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigKeyringHelper.java
@@ -46,7 +46,7 @@ public class ConfigKeyringHelper extends HelperBase {
                        "<input type=\"submit\" name=\"action\" class=\"delete\" value=\"").append(_t("Delete key")).append("\">" +
                        "</td></tr></table>");
         }
-        out.write(buf.toString());
+        out.append(buf);
         out.flush();
     }
 
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNavHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNavHelper.java
index 9cfed79f87..fc9ce8a347 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNavHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ConfigNavHelper.java
@@ -92,6 +92,6 @@ public class ConfigNavHelper extends HelperBase {
         }
         if (!span)
             buf.append("</center>");
-        _out.write(buf.toString());
+        _out.append(buf);
     }
 }
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/JobQueueHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/JobQueueHelper.java
index 185fdba80b..0806e7419b 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/JobQueueHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/JobQueueHelper.java
@@ -92,7 +92,7 @@ public class JobQueueHelper extends HelperBase {
         }
         buf.append("</ol>\n");
         getJobCounts(buf, counter);
-        out.write(buf.toString());
+        out.append(buf);
         buf.setLength(0);
 
         buf.append("<h3 id=\"scheduledjobs\">")
@@ -118,14 +118,14 @@ public class JobQueueHelper extends HelperBase {
         }
         buf.append("</ol></div>\n");
         getJobCounts(buf, counter);
-        out.write(buf.toString());
+        out.append(buf);
         buf.setLength(0);
         
         buf.append("<h3 id=\"totaljobstats\">")
            .append(_t("Total Job Statistics"))
            .append("</h3>\n");
         getJobStats(buf);
-        out.write(buf.toString());
+        out.append(buf);
     }
     
     /** @since 0.9.5 */
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/OldConsoleHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/OldConsoleHelper.java
index 66055a1708..da4ea1140c 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/OldConsoleHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/OldConsoleHelper.java
@@ -75,7 +75,7 @@ public class OldConsoleHelper extends HelperBase {
         long free = Runtime.getRuntime().freeMemory()/1024;
         buf.append("<b>Memory:</b> In use: ").append((tot-free)).append("KB Free: ").append(free).append("KB <br>\n"); 
 
-        out.write(buf.toString());
+        out.append(buf);
         out.flush();
     }
     
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ProfilesHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ProfilesHelper.java
index b2a72dfaa4..632947c7c9 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/ProfilesHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/ProfilesHelper.java
@@ -119,6 +119,6 @@ public class ProfilesHelper extends HelperBase {
         if (!span)
             buf.append("</center>");
         buf.append("</div>");
-        _out.write(buf.toString());
+        _out.append(buf);
     }
 }
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/StatsGenerator.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/StatsGenerator.java
index eceb409924..00bdd10ba8 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/StatsGenerator.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/StatsGenerator.java
@@ -70,7 +70,7 @@ public class StatsGenerator {
 
         buf.append("<form action=\"\"><b>");
         buf.append(_t("Jump to section")).append(":</b> <select class=\"onchange\">");
-        out.write(buf.toString());
+        out.append(buf);
         buf.setLength(0);
 
         Map<String, SortedSet<String>> unsorted = _context.statManager().getStatsByGroup();
@@ -89,14 +89,14 @@ public class StatsGenerator {
             //    buf.append(stat);
             //    buf.append("</option>\n");
             //}
-            //out.write(buf.toString());
+            //out.append(buf);
             //buf.setLength(0);
         }
         // TODO this is broken for non-js
         buf.append("</select> <input type=\"submit\" value=\"").append(_t("GO")).append("\" />");
         buf.append("</form>");
 
-        out.write(buf.toString());
+        out.append(buf);
         buf.setLength(0);
 
         for (Map.Entry<String, Set<String>> entry : groups.entrySet()) {
@@ -108,7 +108,7 @@ public class StatsGenerator {
             buf.append(translateGroup(group));
             buf.append("</a></h3>");
             buf.append("<ul class=\"statlist\">");
-            out.write(buf.toString());
+            out.append(buf);
             buf.setLength(0);
             for (String stat : stats) {
                 buf.append("<li class=\"statsName\"><b><a name=\"");
@@ -120,7 +120,7 @@ public class StatsGenerator {
                     renderFrequency(stat, buf);
                 else
                     renderRate(stat, buf, showAll);
-                out.write(buf.toString());
+                out.append(buf);
                 buf.setLength(0);
             }
             out.write("</ul><br>\n");
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
index 9d2da1f2db..338b3dde4f 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
@@ -138,7 +138,7 @@ class SummaryBarRenderer {
 
             // Only output section if there's more than the <hr> to print
             if (buf.length() > 5)
-                out.write(buf.toString());
+                out.append(buf);
         }
     }
 
diff --git a/router/java/src/net/i2p/router/crypto/TransientSessionKeyManager.java b/router/java/src/net/i2p/router/crypto/TransientSessionKeyManager.java
index f9b12b8048..7e24febad0 100644
--- a/router/java/src/net/i2p/router/crypto/TransientSessionKeyManager.java
+++ b/router/java/src/net/i2p/router/crypto/TransientSessionKeyManager.java
@@ -807,7 +807,7 @@ public class TransientSessionKeyManager extends SessionKeyManager {
                 buf.append(size).append('/').append(ts.getOriginalSize()).append(" tags remaining</li>");
             }
             buf.append("</ul></td></tr>\n");
-            out.write(buf.toString());
+            out.append(buf);
             buf.setLength(0);
         }
         buf.append("<tr><th colspan=\"2\">Total inbound tags: ").append(total).append(" (")
@@ -841,7 +841,7 @@ public class TransientSessionKeyManager extends SessionKeyManager {
                 buf.append(size).append('/').append(ts.getOriginalSize()).append(" tags remaining; acked? ").append(ts.getAcked()).append("</li>");
             }
             buf.append("</ul></td></tr>\n");
-            out.write(buf.toString());
+            out.append(buf);
             buf.setLength(0);
         }
         buf.append("<tr><th colspan=\"2\">Total outbound tags: ").append(total).append(" (")
@@ -849,7 +849,7 @@ public class TransientSessionKeyManager extends SessionKeyManager {
            .append("; sessions: ").append(outbound.size())
            .append("</th></tr>\n</table>");
 
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     /**
diff --git a/router/java/src/net/i2p/router/crypto/ratchet/RatchetSKM.java b/router/java/src/net/i2p/router/crypto/ratchet/RatchetSKM.java
index e2c6044ef1..d0e74d230a 100644
--- a/router/java/src/net/i2p/router/crypto/ratchet/RatchetSKM.java
+++ b/router/java/src/net/i2p/router/crypto/ratchet/RatchetSKM.java
@@ -822,7 +822,7 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
                 }
             }
             buf.append("</ul></td></tr>\n");
-            out.write(buf.toString());
+            out.append(buf);
             buf.setLength(0);
         }
         buf.append("<tr><th colspan=\"2\">Total inbound tags: ").append(total).append(" (")
@@ -876,14 +876,14 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
                 buf.append("</li>");
             }
             buf.append("</ul></td></tr>\n");
-            out.write(buf.toString());
+            out.append(buf);
             buf.setLength(0);
         }
         buf.append("<tr><th colspan=\"2\">Total sets: ").append(totalSets)
            .append("; sessions: ").append(outbound.size())
            .append("</th></tr>\n</table>");
 
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     /**
diff --git a/router/java/src/net/i2p/router/startup/RouterAppManager.java b/router/java/src/net/i2p/router/startup/RouterAppManager.java
index 561a9f1ad1..d46ddcb5d2 100644
--- a/router/java/src/net/i2p/router/startup/RouterAppManager.java
+++ b/router/java/src/net/i2p/router/startup/RouterAppManager.java
@@ -308,7 +308,7 @@ public class RouterAppManager extends ClientAppManagerImpl {
         buf.append("<div class=\"debug_container\">");
         toString2(buf);
         buf.append("</div>");
-        out.write(buf.toString());
+        out.append(buf);
     }
 
     /**
diff --git a/router/java/src/net/i2p/router/sybil/PersistSybil.java b/router/java/src/net/i2p/router/sybil/PersistSybil.java
index 57796db813..4a3f419705 100644
--- a/router/java/src/net/i2p/router/sybil/PersistSybil.java
+++ b/router/java/src/net/i2p/router/sybil/PersistSybil.java
@@ -81,7 +81,7 @@ public class PersistSybil {
                 buf.append(h.toBase64()).append(':');
                 p.toString(buf);
                 buf.append('\n');
-                out.write(buf.toString());
+                out.append(buf);
                 buf.setLength(0);
             }
         } finally {
-- 
GitLab