JAPI Guide: I/O
JAPI I/O Introduction
JAPI extends Java with some I/O classes that are mainly useful for writing I/O programs.
JAPI I/O Stream extensions
The stream extensions are ARGVInputStream
for binary data and ARGVReader
for character data which enable you to write Java command line programs in a similar way you would do in Perl using <ARGV>
.
The class ARGV
provides access to the lines provided by an ARGVReader
in an iterable way.
Other JAPI I/O extensions
The class ARGVEnumeration
is a class providing an enumeration suitable for java.lang.SequenceInputStream
that creates an Enumeration of InputStreams from an array of filenames.
ARGVInputStream
and ARGVReader
use ARGVEnumeration
.
The class IOHelper
provides some useful I/O methods, currently only for copying data.
The class net.sf.japi.util.PrintStreamThrowableHandler
is used by ARGVInputStream
and ARGVReader
as a default error handler for reporting ARGV problems to the user via STDERR.
Examples
Example 1: The UNIX command cat
cat
without JAPI
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static java.lang.System.err;
import static java.lang.System.in;
import static java.lang.System.out;
/** Plain implementation of the UNIX comand <code>cat</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class CatPlain {
/** Main program.
* @param args command line arguments: names of files to concatenate
*/
public static void main(final String... args) {
if (args.length == 0) {
try {
copy(in, out);
} catch (final IOException e) {
err.println(e);
}
} else {
for (final String arg : args) {
InputStream in = null;
try {
in = new FileInputStream(arg);
copy(in, out);
} catch (final IOException e) {
err.println(e);
} finally {
try { in.close(); } catch (final Exception e) { /* ignore */ }
}
}
}
}
/** Copies all data from one stream to another.
* @param in source stream
* @param out sink stream
* @throws IOException in case reading or writing caused an error.
*/
private static void copy(final InputStream in, final OutputStream out) throws IOException {
final byte[] buf = new byte[4096];
for (int bytesRead; (bytesRead = in.read(buf)) != -1; ) {
out.write(buf, 0, bytesRead);
}
}
} // class CatPlain
cat
with JAPI
import static java.lang.System.out;
import java.io.IOException;
import net.sf.japi.io.ARGVInputStream;
import static net.sf.japi.io.IOHelper.copy;
/** JAPI-based implementation of the UNIX comand <code>cat</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class CatJAPI {
/** Main program.
* @param args command line arguments: names of files to concatenate
*/
public static void main(final String... args) throws IOException {
copy(new ARGVInputStream(args), out);
}
} // class CatJAPI
Example 2: The UNIX command sort
sort
without JAPI
import static java.lang.System.out;
import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import net.sf.japi.io.ARGV;
/** Plain implementation of the UNIX command <code>sort</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class SortPlain {
/** Main program.
* @param args command line arguments: names of files to sort
*/
public static void main(final String... args) {
final List<String> lineList = new ArrayList<String>();
if (args.length == 0) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(System.in));
for (String line; (line = in.readLine()) != null; lineList.add(line));
} catch (final IOException e) {
System.err.println(e);
} finally {
try { in.close(); } catch (final Exception ignore) { /* ignore */ }
}
} else {
for (final String arg : args) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(arg));
for (String line; (line = in.readLine()) != null; lineList.add(line));
} catch (final IOException e) {
System.err.println(e);
} finally {
try { in.close(); } catch (final Exception ignore) { /* ignore */ }
}
}
}
sort(lineList);
for (final String line : lineList) {
out.println(line);
}
}
} // class SortJAPI
sort
with JAPI
import static java.lang.System.out;
import java.util.ArrayList;
import static java.util.Collections.sort;
import java.util.List;
import net.sf.japi.io.ARGV;
/** JAPI-based implementation of the UNIX command <code>sort</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class SortJAPI {
/** Main program.
* @param args command line arguments: names of files to sort
*/
public static void main(final String... args) {
final List<String> lineList = new ArrayList<String>();
for (final String line : new ARGV(args)) {
lineList.add(line);
}
sort(lineList);
for (final String line : lineList) {
out.println(line);
}
}
} // class SortJAPI
Example 3: The UNIX command uniq
uniq
without JAPI
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.err;
import static java.lang.System.out;
/** JAPI-based implementation of the UNIX comand <code>uniq</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class UniqPlain {
/** Main program.
* @param args command line arguments: names of files to print unique lines from
*/
public static void main(final String... args) {
String prevLine = null;
if (args.length == 0) {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(System.in));
for (String line; (line = in.readLine()) != null;) {
if (!line.equals(prevLine)) {
out.println(line);
}
prevLine = line;
}
} catch (final IOException e) {
err.println(e);
} finally {
try { in.close(); } catch (final Exception ignore) { /* ignore */ }
}
} else {
for (final String arg : args) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(arg));
for (String line; (line = in.readLine()) != null;) {
if (!line.equals(prevLine)) {
out.println(line);
}
prevLine = line;
}
} catch (final IOException e) {
err.println(e);
} finally {
try { in.close(); } catch (final Exception ignore) { /* ignore */ }
}
}
}
}
} // class UniqJAPI
uniq
with JAPI
import static java.lang.System.out;
import net.sf.japi.io.ARGV;
/** JAPI-based implementation of the UNIX comand <code>uniq</code>.
* @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
*/
public class UniqJAPI {
/** Main program.
* @param args command line arguments: names of files to print unique lines from
*/
public static void main(final String... args) {
String prevLine = null;
for (final String line : new ARGV(args)) {
if (!line.equals(prevLine)) {
out.println(line);
}
prevLine = line;
}
}
} // class UniqJAPI