001 /* JAPI - (Yet anothr (hopefully) useful) Java API
002 *
003 * Copyright (C) 2004-2006 Christian Hujer
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License as
007 * published by the Free Software Foundation; either version 2 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but
011 * WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018 * 02111-1307, USA.
019 */
020
021 package net.sf.japi.io;
022
023 import java.io.SequenceInputStream;
024 import static net.sf.japi.util.PrintStreamThrowableHandler.STDERR;
025
026
027 /** An ARGV InputStream, behaving similar as <code><ARGV></code> in Perl.
028 * Just to make life a bit less painful to Perl programmers that were reborn as Java programmers.
029 * <p />
030 * Don't rely on this class being a subclass of {@link SequenceInputStream}.
031 * That is subject to change.
032 * <p />
033 * An <code>ARGVInputStream</code> provides sequential access to one or more files.
034 * To create an <code>ARGVInputStream</code> that is just a synonym on {@link System#in}, just pass an empty String array to its
035 * constructor.
036 * To create an <code>ARGVInputStream</code> that sequentially accesss one file after another (similar as <code><ARGV></code> does
037 * in Perl), pass an array with the desired filenames to its constructor.
038 * <p />
039 * Errors that happen due to files that cannot be opened are always reported to {@link System#err} and otherwise silently ignored, i.e. not reported to the main program.
040 * <p />
041 * Usually, you'd use <code>ARGVInputStream</code> like this:
042 * <pre>
043 * // Cat in a similar way the UNIX command cat works like
044 * import java.io.IOException;
045 * import net.sf.japi.io.ARGVInputStream;
046 * import static net.sf.japi.io.IOHelper.copy;
047 *
048 * public class Cat {
049 * public static void main(final String... args) {
050 * try {
051 * copy(new ARGVInputStream(args), System.out);
052 * } catch (final IOException e) {
053 * System.err.println(e);
054 * }
055 * }
056 * }
057 * </pre>
058 * Internally this class uses {@link ARGVEnumeration} to sequentially access the Stream elements of ARGV.
059 * @note it is not required to invoke {@link #close()}.
060 * @author <a href="mailto:Christian.Hujer@itcqis.com">Christian Hujer</a>
061 * @see ARGVEnumeration
062 */
063 public class ARGVInputStream extends SequenceInputStream {
064
065 /** Create an ARGVInputStream.
066 * @param args Command line arguments or some other String array containing 0 or more file names.
067 */
068 public ARGVInputStream(final String... args) {
069 super(new ARGVEnumeration(STDERR, args));
070 }
071
072 } // class ARGVInputStream