0
# I/O & File Operations
1
2
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.
3
4
## Capabilities
5
6
### Stream Utilities
7
8
Abstract utility class providing stream copying and conversion utilities with optimized buffer management.
9
10
```java { .api }
11
/**
12
* Stream copying and conversion utilities
13
*/
14
public abstract class StreamUtils {
15
public static final int BUFFER_SIZE = 4096;
16
17
// Stream to byte array/string conversion
18
public static byte[] copyToByteArray(InputStream in) throws IOException;
19
public static String copyToString(InputStream in, Charset charset) throws IOException;
20
public static String copyToString(ByteArrayOutputStream baos, Charset charset);
21
22
// Copy operations
23
public static void copy(byte[] in, OutputStream out) throws IOException;
24
public static void copy(String in, Charset charset, OutputStream out) throws IOException;
25
public static int copy(InputStream in, OutputStream out) throws IOException;
26
public static long copyRange(InputStream in, OutputStream out, long start, long end) throws IOException;
27
28
// Stream utilities
29
public static int drain(InputStream in) throws IOException;
30
public static InputStream emptyInput();
31
public static InputStream nonClosing(InputStream in);
32
public static OutputStream nonClosing(OutputStream out);
33
}
34
```
35
36
### NIO Utilities
37
38
NIO file operations, zip handling, and byte conversions for high-performance file processing.
39
40
```java { .api }
41
/**
42
* NIO file operations, zip handling, and byte conversions
43
*/
44
public class NioUtils {
45
// Zip operations
46
public static void unzip(String zipFilename, String destDirname) throws IOException;
47
public static void create(String zipFilename, String... filenames) throws IOException;
48
public static void list(String zipFilename) throws IOException;
49
50
// File operations
51
public static void deleteOldFiles(String dirPath, int olderThanMinute);
52
public static String getFileExtension(File file);
53
public static String getFileExtension(String name);
54
55
// ByteBuffer operations
56
public static ByteBuffer toByteBuffer(String s);
57
public static ByteBuffer toByteBuffer(File file) throws IOException;
58
59
// System utilities
60
public static String getTempDir();
61
62
// Conversion utilities
63
public static byte[] toByteArray(InputStream is) throws IOException;
64
public static String toString(InputStream is) throws IOException;
65
}
66
```
67
68
**Usage Examples:**
69
70
```java
71
import com.networknt.utility.StreamUtils;
72
import com.networknt.utility.NioUtils;
73
74
// Stream operations
75
InputStream input = new FileInputStream("data.txt");
76
byte[] data = StreamUtils.copyToByteArray(input);
77
String content = StreamUtils.copyToString(input, StandardCharsets.UTF_8);
78
79
// Copy between streams
80
InputStream source = new FileInputStream("source.txt");
81
OutputStream dest = new FileOutputStream("dest.txt");
82
int bytesCopied = StreamUtils.copy(source, dest);
83
84
// Zip operations
85
NioUtils.create("archive.zip", "file1.txt", "file2.txt");
86
NioUtils.unzip("archive.zip", "/extracted/");
87
NioUtils.list("archive.zip");
88
89
// File utilities
90
String extension = NioUtils.getFileExtension("document.pdf"); // "pdf"
91
String tempDir = NioUtils.getTempDir();
92
NioUtils.deleteOldFiles("/logs", 60); // Delete files older than 60 minutes
93
94
// ByteBuffer operations
95
ByteBuffer buffer = NioUtils.toByteBuffer("Hello World");
96
ByteBuffer fileBuffer = NioUtils.toByteBuffer(new File("data.bin"));
97
```