IO Power
IO Power is the first library of Power Libraries. It contains some helper methods for opening Input- and OutputStreams and Readers and Writers. The main purpose of IO Power is to make opening streams, readers and writers less cluttered and simple to understand.
Examples
Writing UTF-8
in plain Java:
try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileWriter("test.txt"), StandardCharsets.UTF_8))) {
out.write("Hello World");
}
with IO Power:
try(BufferedWriter out = Out.file("test.txt").usingUTF8().asWriter()) {
out.write("Hello World");
}
Reading lines from a compressed file
in plain Java:
try(BufferedReader in = new BufferedReader(new InputStreamReader(new GZipInputStream(new FileInputStream("test.txt.gz"))))) {
String line;
while((line=in.readLine())!=null)
System.out.println(line);
}
with IO Power:
for(String line:In.file("test").decompress().readLines())
System.out.println(line);
Serializing Objects to bytes
in plain Java:
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try(ObjectOutputStream out = new ObjectOutputStream(byteOut);) {
out.writeObject(someObject);
}
byte[] bytes=byteOut.toByteArray();
with IO Power:
byte[] bytes;
try(BAObjectOutputStream out=Out.bytes().asObjects()) {
out.writeObject(someObject);
bytes=out.toByteArray(); //this closes the stream automatically
}
or even simpler
byte[] bytes=Out.bytes().writeObject(someObject);