CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-nio

NIO extensions for Apache Groovy providing enhanced file system operations and path handling

Pending
Overview
Eval results
Files

Groovy NIO Extensions

Groovy NIO Extensions provides enhanced file system operations and path handling capabilities for Apache Groovy. This package extends Groovy's existing I/O functionality with modern Java NIO.2 features, enabling more efficient file system access, path manipulation, and stream operations with Groovy's characteristic closure-based programming model.

Package Information

  • Package Name: groovy-nio
  • Package Type: maven
  • Language: Java (Groovy)
  • Installation:
    implementation 'org.codehaus.groovy:groovy-nio:3.0.25'
  • Maven:
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-nio</artifactId>
      <version>3.0.25</version>
    </dependency>

Core Imports

import java.nio.file.Path
import java.nio.file.Paths
// Extension methods are automatically available on Path objects

Basic Usage

import java.nio.file.Path
import java.nio.file.Paths

// Create a path
Path myFile = Paths.get("example.txt")

// Write text to file
myFile.text = "Hello, World!"

// Read text from file
String content = myFile.getText()

// Append text
myFile << " Additional content"

// Process lines with closure
myFile.eachLine { line ->
    println line
}

// Directory traversal
Path myDir = Paths.get("my-directory")
myDir.eachFile { file ->
    println "Found: ${file.fileName}"
}

Architecture

Groovy NIO Extensions is built around several key components:

  • Extension Methods: The NioExtensions class provides static extension methods that are automatically applied to java.nio.file.Path objects
  • Closure Integration: Extensive use of Groovy closures for resource management, iteration, and filtering operations
  • Writable Interface: Integration with Groovy's Writable interface for streaming operations
  • Resource Management: Automatic resource management using try-with-resources patterns
  • Charset Support: Comprehensive character encoding support with optional BOM handling

Capabilities

File Reading and Writing

Core file I/O operations for reading and writing text, binary data, and objects to files. Provides both simple text operations and advanced charset handling.

// Text operations
String getText(Path self);
String getText(Path self, String charset);
void write(Path self, String text);
void write(Path self, String text, String charset);
void setText(Path self, String text);

// Binary operations  
byte[] getBytes(Path self);
byte[] readBytes(Path self);
void setBytes(Path self, byte[] bytes);

// Line operations
List<String> readLines(Path self);
List<String> readLines(Path self, String charset);

File I/O Operations

File Appending Operations

Append data to existing files with support for text, binary data, and various input sources including streams and closures.

// Text appending
void append(Path self, Object text);
void append(Path self, Object text, String charset);
Path leftShift(Path self, Object text);

// Binary appending
void append(Path self, byte[] bytes);
void append(Path self, InputStream stream);
Path leftShift(Path self, byte[] bytes);

File Appending

Line Processing

Process files line-by-line with closures, including splitting, filtering, and transformation operations with charset support.

<T> T eachLine(Path self, Closure<T> closure);
<T> T eachLine(Path self, String charset, Closure<T> closure);
<T> T splitEachLine(Path self, String regex, Closure<T> closure);
Writable filterLine(Path self, Closure closure);

Line Processing

Directory Traversal

Traverse directory structures with filtering, sorting, and recursion options. Support for file type filtering and advanced traversal patterns.

void eachFile(Path self, Closure closure);
void eachFile(Path self, FileType fileType, Closure closure);
void eachFileRecurse(Path self, Closure closure);
void traverse(Path self, Map<String, Object> options, Closure closure);

Directory Operations

Stream Management

Create and manage various stream types with automatic resource management using closures for safe operation.

BufferedReader newReader(Path self);
BufferedWriter newWriter(Path self);
<T> T withReader(Path self, Closure<T> closure);
<T> T withWriter(Path self, Closure<T> closure);
BufferedInputStream newInputStream(Path self);
BufferedOutputStream newOutputStream(Path self);

Stream Operations

Object Serialization

Handle Java object serialization and deserialization with support for custom class loaders and closure-based processing.

ObjectInputStream newObjectInputStream(Path self);
ObjectOutputStream newObjectOutputStream(Path self);
<T> T withObjectInputStream(Path self, Closure<T> closure);
void eachObject(Path self, Closure closure);

Object Serialization

Writable Conversion

Convert Path objects to Groovy's Writable interface for streaming and output operations.

Path asWritable(Path self);
Path asWritable(Path self, String encoding);
<T> T asType(Path path, Class<T> c);

Stream Operations

Types

// Core Groovy types used throughout the API
interface Closure<T> {
    T call(Object... args);
}

// File type enumeration for filtering
enum FileType {
    FILES,      // Regular files only
    DIRECTORIES, // Directories only  
    ANY         // Both files and directories
}

// Writable interface for streaming
interface Writable {
    Writer writeTo(Writer out);
}

// WritablePath - Path wrapper implementing Writable
class WritablePath implements Path, Writable {
    WritablePath(Path delegate);
    WritablePath(Path delegate, String encoding);
    Writer writeTo(Writer out);
}

Install with Tessl CLI

npx tessl i tessl/maven-org-codehaus-groovy--groovy-nio
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.codehaus.groovy/groovy-nio@3.0.x