A comprehensive utility library for Light-4J microservices framework providing string manipulation, networking, I/O, security, and configuration utilities.
—
Stream processing, NIO operations, file handling, and byte conversions. Includes zip file handling, stream copying, and ByteBuffer operations for efficient I/O operations in microservices environments.
Abstract utility class providing stream copying and conversion utilities with optimized buffer management.
/**
* Stream copying and conversion utilities
*/
public abstract class StreamUtils {
public static final int BUFFER_SIZE = 4096;
// Stream to byte array/string conversion
public static byte[] copyToByteArray(InputStream in) throws IOException;
public static String copyToString(InputStream in, Charset charset) throws IOException;
public static String copyToString(ByteArrayOutputStream baos, Charset charset);
// Copy operations
public static void copy(byte[] in, OutputStream out) throws IOException;
public static void copy(String in, Charset charset, OutputStream out) throws IOException;
public static int copy(InputStream in, OutputStream out) throws IOException;
public static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException;
// Stream utilities
public static int drain(InputStream in) throws IOException;
public static InputStream emptyInput();
public static InputStream nonClosing(InputStream in);
public static OutputStream nonClosing(OutputStream out);
}NIO file operations, zip handling, and byte conversions for high-performance file processing.
/**
* NIO file operations, zip handling, and byte conversions
*/
public class NioUtils {
// Zip operations
public static void unzip(String zipFilename, String destDirname) throws IOException;
public static void create(String zipFilename, String... filenames) throws IOException;
public static void list(String zipFilename) throws IOException;
// File operations
public static void deleteOldFiles(String dirPath, int olderThanMinute);
public static String getFileExtension(File file);
public static String getFileExtension(String name);
// ByteBuffer operations
public static ByteBuffer toByteBuffer(String s);
public static ByteBuffer toByteBuffer(File file) throws IOException;
// System utilities
public static String getTempDir();
// Conversion utilities
public static byte[] toByteArray(InputStream is) throws IOException;
public static String toString(InputStream is) throws IOException;
}Usage Examples:
import com.networknt.utility.StreamUtils;
import com.networknt.utility.NioUtils;
// Stream operations
InputStream input = new FileInputStream("data.txt");
byte[] data = StreamUtils.copyToByteArray(input);
String content = StreamUtils.copyToString(input, StandardCharsets.UTF_8);
// Copy between streams
InputStream source = new FileInputStream("source.txt");
OutputStream dest = new FileOutputStream("dest.txt");
int bytesCopied = StreamUtils.copy(source, dest);
// Zip operations
NioUtils.create("archive.zip", "file1.txt", "file2.txt");
NioUtils.unzip("archive.zip", "/extracted/");
NioUtils.list("archive.zip");
// File utilities
String extension = NioUtils.getFileExtension("document.pdf"); // "pdf"
String tempDir = NioUtils.getTempDir();
NioUtils.deleteOldFiles("/logs", 60); // Delete files older than 60 minutes
// ByteBuffer operations
ByteBuffer buffer = NioUtils.toByteBuffer("Hello World");
ByteBuffer fileBuffer = NioUtils.toByteBuffer(new File("data.bin"));Install with Tessl CLI
npx tessl i tessl/maven-com-networknt--utility