Svjatoslav Commons - Java library of commonly used functions

Table of Contents

1. Introduction

Svjatoslav Commons is a shared Java utility library that consolidates commonly needed functionality: reading and writing files, manipulating strings, handling binary data, and displaying errors to users.

The library requires Java 8 and has minimal dependencies: only JUnit for testing and servlet-api (optional, provided scope). See How to take library into use for Maven dependency configuration. The complete API documentation is available in the JavaDoc.

2. Library contents

2.1. Binary data utilities (eu.svjatoslav.commons.data)

The data package provides classes for working with binary data at the bit level and converting it to human-readable formats.

BitInputStream and BitOutputStream allow reading and writing individual bits from streams, not just whole bytes. This is essential for applications that work with bit-packed data formats where data is not byte-aligned, such as custom compression algorithms, network protocols, or binary file formats. Bits are processed from most significant to least significant within each byte, and you can read or write any number of bits (not just multiples of 8).

HexConverter converts byte arrays to uppercase hexadecimal strings. Each byte becomes exactly two characters (0-9, A-F), making binary data visible in logs, debug output, or text-based protocols. Null inputs return null; empty arrays return empty strings.

2.2. File system utilities (eu.svjatoslav.commons.file)

The file package provides utilities for common file operations: reading and writing files, parsing paths, and resolving system directories.

IOHelper is the primary file I/O class. It reads and writes files as byte arrays or UTF-8 strings, deletes files and directories recursively (without following symbolic links, which prevents accidental deletion of data outside the intended scope), and overwrites files only when content actually differs, avoiding unnecessary disk writes and preserving timestamps.

FilePathParser extracts metadata from file paths: the file extension (lowercase normalized), the file name without extension, and formats file sizes using binary units (b, KiB, MiB, GiB, TiB, PiB) with human-readable descriptions like "15 MiB".

CommonPathResolver resolves common system paths. Currently it provides the user's home directory, useful for storing application configuration files or user-specific data.

2.3. Graphical dialogs (eu.svjatoslav.commons.gui.dialog)

The dialog package provides Swing-based components for displaying information to users in graphical applications.

ExceptionDialog displays exceptions with their full stack traces in a windowed dialog. It shows the exception type, error message, cause (if present), and complete stack trace, making it easy to debug errors in GUI applications without requiring console output or log files. Note: this requires a graphical environment and is not suitable for headless or server-side applications.

2.4. String utilities (eu.svjatoslav.commons.string)

The string package provides a mutable string builder optimized for prefix and suffix operations, plus simple glob-style pattern matching.

String2 is a mutable string that stores characters in a list, enabling efficient operations at both ends. It provides prepend() and append() for adding text, trimPrefix() and trimSuffix() for removing characters, trimPrefixIfExists() and trimSuffixIfExists() for conditional removal, hasPrefix() and hasSuffix() for checking, repeat() for duplication, and enforceLength() for padding or truncating to exact lengths. All methods return the same instance for fluent chaining.

GlobMatcher matches strings against glob-style wildcard patterns using * (matches any sequence of characters, including empty) and ? (matches exactly one character). This is simpler than regular expressions and useful for file name filtering, simple validation, or user-friendly pattern input.

2.5. Tokenizer (eu.svjatoslav.commons.string.tokenizer)

The tokenizer package provides a regex-based tokenizer for parsing structured text into tokens with lookahead and backtracking support.

Tokenizer is the main class. You add Terminator objects that define token boundaries using regex patterns. Each terminator has a strategy: PRESERVE returns matched tokens for processing, DROP silently discards them (useful for whitespace or comments). You can peek at the next token without consuming it, unread tokens to backtrack, expect specific tokens and throw exceptions on mismatch, and categorize tokens using group names.

TokenizerMatch is the result object containing the matched text, the terminator that identified it, the regex matcher (for extracting capture groups), and methods to check group membership.

InvalidSyntaxException is thrown when expectations fail during parsing, such as when expecting a specific token but finding something else.

3. How to take library into use

Add the svjatoslavcommons dependency to your Maven project:

<dependencies>
  <dependency>
    <groupId>eu.svjatoslav</groupId>
    <artifactId>svjatoslavcommons</artifactId>
    <version>1.9</version>
  </dependency>
</dependencies>

Also add the repository (the library is not on Maven Central):

<repositories>
  <repository>
    <id>svjatoslav.eu</id>
    <name>Svjatoslav repository</name>
    <url>https://www3.svjatoslav.eu/maven/</url>
  </repository>
</repositories>

4. Source code

This program is free software: released under Creative Commons Zero (CC0) license

Program author:

Getting the source code:

Created: 2026-04-12 Sun 07:11

Validate