From swizzle
Reference for org.tomitribe.swizzle.stream stream manipulation and lexer library. TRIGGER when: code imports from org.tomitribe.swizzle, or user needs to find/replace/transform data in InputStreams using fixed string tokens, stream filters, or stream lexing. DO NOT TRIGGER when: working with regex-based stream processing or unrelated stream libraries.
How this skill is triggered — by the user, by Claude, or both
Slash command
/swizzle:swizzleThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Java library for finding, manipulating, and transforming data in streams using fixed string tokens (no regex). Provides stream filters for on-the-fly transformation and a stream lexer for token extraction. Memory-efficient with fixed-size buffers.
Java library for finding, manipulating, and transforming data in streams using fixed string tokens (no regex). Provides stream filters for on-the-fly transformation and a stream lexer for token extraction. Memory-efficient with fixed-size buffers.
Package: org.tomitribe.swizzle.stream
<groupId>org.tomitribe</groupId>
<artifactId>swizzle</artifactId>
<version>1.4-SNAPSHOT</version>
The simplest way to use Swizzle. Chain operations and get a transformed stream.
InputStream result = StreamBuilder.of("Hello WORLD, goodbye WORLD")
.replace("WORLD", "Earth")
.get();
String output = StreamUtils.streamToString(result);
// "Hello Earth, goodbye Earth"
StreamBuilder.of(InputStream in)
StreamBuilder.of(File file)
StreamBuilder.of(byte[] bytes)
StreamBuilder.of(String contents)
StreamBuilder.of(String contents, Charset charset)
// Include only content between tokens
.include(String begin, String end, boolean caseSensitive, boolean keepDelimiters)
// Exclude content between tokens
.exclude(String begin, String end, boolean caseSensitive, boolean keepDelimiters)
// Delete exact token
.delete(String token)
// Delete token and everything between begin/end (inclusive)
.delete(String begin, String end)
// Delete only content between begin/end (keep delimiters)
.deleteBetween(String begin, String end)
// Replace exact token
.replace(String token, String with)
.replace(String token, StringHandler handler)
// Replace begin/end and everything between (inclusive)
.replace(String begin, String end, String with)
.replace(String begin, String end, StringHandler handler)
// Replace only content between begin/end (keep delimiters)
.replaceBetween(String begin, String end, String with)
.replaceBetween(String begin, String end, StringHandler handler)
// Watch for tokens (observe without consuming)
.watch(String token, Consumer<String> consumer)
.watch(String begin, String end, Consumer<String> consumer)
.watch(OutputStream observer)
InputStream get() // get transformed stream
void to(OutputStream out) // write to output stream
void run() // consume and discard (for side effects)
All filters extend FilteredInputStream. Chain by wrapping one inside another.
Include only data between begin and end tokens:
InputStream in = new IncludeFilterInputStream(input, "<BODY", "</BODY>");
// Only data between <BODY and </BODY> passes through
Options: caseSensitive (default true), keepDelimiters (default true).
Remove data between begin and end tokens:
InputStream in = new ExcludeFilterInputStream(input, "<!--", "-->");
// HTML comments are removed
InputStream in = new BufferedInputStream(url.openStream());
in = new IncludeFilterInputStream(in, "<BODY", "</BODY>");
in = new ExcludeFilterInputStream(in, "<!--", "-->");
in = new ExcludeFilterInputStream(in, "<SCRIPT", "</SCRIPT>");
InputStream in = new ReplaceStringInputStream(input, "RED", "YELLOW");
Case-insensitive variant:
InputStream in = new ReplaceStringInputStream(input, false, "red", "YELLOW");
Map<String, String> replacements = new HashMap<>();
replacements.put("RED", "pear");
replacements.put("GREEN", "grape");
replacements.put("BLUE", "banana");
InputStream in = new ReplaceStringsInputStream(input, replacements);
Map<String, String> vars = new HashMap<>();
vars.put("name", "Alice");
vars.put("role", "admin");
InputStream in = new ReplaceVariablesInputStream(input, "{", "}", vars);
// "Hello {name}, you are {role}" -> "Hello Alice, you are admin"
Replace content between delimiters using a handler:
InputStream in = new DelimitedTokenReplacementInputStream(input, "${", "}",
new StringTokenHandler() {
public String handleToken(String token) {
return System.getProperty(token, "${" + token + "}");
}
});
InputStream in = new FixedTokenReplacementInputStream(input, "TODAY",
token -> new ByteArrayInputStream(LocalDate.now().toString().getBytes()));
public interface StreamTokenHandler {
InputStream processToken(String token) throws IOException;
}
Returns an InputStream -- can be anything from a simple string to a 10GB file.
public abstract class StringTokenHandler implements StreamTokenHandler {
public abstract String handleToken(String token) throws IOException;
}
StreamBuilder.of(input)
.replace("${", "}", (StringHandler) token -> System.getProperty(token, ""))
.get();
Map<String, String> map = new HashMap<>();
map.put("color", "blue");
map.put("size", "large");
new MappedTokenHandler(map); // returns mapped value or original token
Watch for tokens without consuming them from the stream:
InputStream in = new FixedTokenWatchInputStream(input, "ERROR",
token -> System.err.println("Found: " + token));
// Stream passes through unchanged; callback fires on each match
InputStream in = new DelimitedTokenWatchInputStream(input, "<error>", "</error>",
token -> log.warn("Error found: " + token));
Higher-level API for reading specific tokens from streams.
StreamLexer lexer = new StreamLexer(inputStream);
// Read content between begin and end tokens
String value = lexer.readToken("<title>", "</title>");
// Read exact token (seek to it and consume)
String token = lexer.readToken("Expected Text");
// Seek: advance to token position
String found = lexer.seek("<body>", "</body>");
// Peek: look ahead without advancing
String preview = lexer.peek("<title>", "</title>");
Limit reading scope to a section of the stream:
// Mark scope to <project>...</project>
lexer.seekAndMark("<project>", "</project>");
// Nested mark for <dependencies>...</dependencies>
lexer.seekAndMark("<dependencies>", "</dependencies>");
// Read within scope
String groupId = lexer.readToken("<groupId>", "</groupId>");
String artifactId = lexer.readToken("<artifactId>", "</artifactId>");
// Exit scopes
lexer.readAndUnmark(); // exit </dependencies>
lexer.readAndUnmark(); // exit </project>
String readToken(String begin, String end)
String readToken(String string)
String seek(String begin, String end)
String seek(String string)
String peek(String begin, String end)
String peek(String string)
StreamLexer mark()
StreamLexer mark(String limit)
void unmark()
boolean seekAndMark(String begin, String end)
boolean readAndMark(String begin, String end)
boolean seekAndUnmark()
boolean readAndUnmark()
Simple template engine:
StringTemplate template = new StringTemplate("{dir}/{name}.{ext}");
Map<String, String> context = new HashMap<>();
context.put("dir", "foo");
context.put("name", "bar");
context.put("ext", "txt");
String result = template.apply(context);
// "foo/bar.txt"
Resolve relative URLs to absolute:
URL baseUrl = new URL("http://example.com/docs/index.html");
InputStream in = new ResolveUrlInputStream(input, "<A HREF=\"", "\"", baseUrl);
// Converts relative href values to absolute URLs
StreamUtils.streamToString(InputStream in) // read entire stream to String
StreamUtils.stringToStream(String original) // wrap String as InputStream
boolean caseSensitive parameter (default true)keepDelimiters controls whether begin/end tokens appear in outputStringTokenHandler returns "null" if handler returns null; StringHandler returns empty stringnpx claudepluginhub tomitribe/claude-plugins --plugin swizzleGuides Java Streams API usage for functional-style data processing on collections, covering stream creation, filter, map, flatMap, and terminal operations like collect and reduce.
Process large data efficiently using Node.js streams (Readable, Writable, Transform) with pipeline for error handling and backpressure.
Guides high-performance streaming with System.IO.Pipelines in .NET for network protocols, binary data parsing, and large stream processing.