From 13c2d69ddf418cffd4f5f3356af4756d832287c6 Mon Sep 17 00:00:00 2001 From: "ilya (leo)" Date: Mon, 5 Jul 2021 00:30:32 +0300 Subject: [PATCH] Create StringBuilder with known size of string --- .../me/tongfei/progressbar/DefaultProgressBarRenderer.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java b/src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java index 16261d9..1973d36 100644 --- a/src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java +++ b/src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java @@ -98,6 +98,9 @@ protected String speed(ProgressState progress, Duration elapsed) { } public String render(ProgressState progress, int maxLength) { + if (maxLength <= 0) { + return ""; + } Instant currTime = Instant.now(); Duration elapsed = Duration.between(progress.startInstant, currTime); @@ -126,7 +129,7 @@ public String render(ProgressState progress, int maxLength) { int length = maxLength - prefixLength - suffixLength; - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(maxLength); sb.append(prefix); // case of indefinite progress bars