001 /* JAPI - (Yet another (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.BufferedReader; 024 import java.io.InputStreamReader; 025 026 /** An ARGV Reader. 027 * Just to make life a bit less painful to Perl programmers that were reborn as Java programmers. 028 * <p /> 029 * Don't rely on this class being a subclass of {@link BufferedReader}. 030 * That is subject to change. 031 * But you can rely on this class retaining all important methods (like <code>readLine()</code>, for instance). 032 * <p /> 033 * An <code>ARGVReader</code> provides sequential access to one or more files. 034 * To create an <code>ARGVReader</code> that is just a {@link BufferedReader} on {@link System#in}, just pass an empty 035 * String array to its constructor. 036 * To create an <code>ARGVReader</code> that sequentially accesss one file after another (like <code><ARGV></code> does in Perl), 037 * pass an array with the desired filenames to its constructor. 038 * <p /> 039 * Usually, you'd use <code>ARGVReader</code> like this: 040 * <pre> 041 * // Sort in a similar way the UNIX command sort works like 042 * public class Sort { 043 * public static void main(final String... args) throws IOException { 044 * final ARGVReader in = new ARGVReader(args); 045 * final List<String> lineList = new ArrayList<String>(); 046 * for (String line; (line = in.readLine()) != null; ) { 047 * lineList.append(line); 048 * } 049 * Collections.sort(lineList); 050 * for (final String line : lineList) { 051 * System.out.println(lines[i]); 052 * } 053 * } 054 * } 055 * </pre> 056 * Internally this class uses {@link ARGVInputStream}, which uses {@link ARGVEnumeration} to sequentially access the Stream elements of ARGV. 057 * @note it is not required to invoke {@link #close()}. 058 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a> 059 * @see ARGVInputStream 060 * @see ARGVEnumeration 061 */ 062 public class ARGVReader extends BufferedReader { 063 064 /** Create an ARGVReader. 065 * @param args Command line arguments or some other String array containing 0 or more file names. 066 */ 067 @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) 068 public ARGVReader(final String... args) { 069 super(new InputStreamReader(new ARGVInputStream(args))); 070 } 071 072 } // class ARGVReader