Class LimitedStringBuilder

java.lang.Object
org.machanism.machai.gw.tools.LimitedStringBuilder

public class LimitedStringBuilder extends Object
A StringBuilder-like helper that retains only the last maxSize characters.

This utility is typically used when capturing potentially unbounded output (for example, process stdout/stderr) while keeping a deterministic upper bound on memory usage.

  • Constructor Details

    • LimitedStringBuilder

      public LimitedStringBuilder(int maxSize, String commandId, File projectDir)
      Creates a builder that keeps at most maxSize characters.
      Parameters:
      maxSize - maximum number of characters to retain; must be positive
      commandId -
      Throws:
      IllegalArgumentException - if maxSize is not positive
  • Method Details

    • append

      public LimitedStringBuilder append(String text)
      Appends text to this builder, trimming any excess characters from the start to stay within the configured maximum size. Also appends the text to a log file at: {projectDir}/.machai/command-log/{commandId}.log
      Parameters:
      text - text to append; ignored if null
      projectDir - the project directory (used for log file path)
      Returns:
      this instance for fluent chaining
    • getCommandLogPath

      public static Path getCommandLogPath(File projectDir, String commandId)
    • getLastText

      public String getLastText()
      Returns the retained content.

      If truncation occurred, a prefix is added to indicate that earlier content was discarded.

      Returns:
      retained text (possibly with a truncation prefix)
    • length

      public int length()
      Returns the number of characters currently retained.
      Returns:
      retained length
    • clear

      public void clear()
      Clears the retained content.