From: <chr...@us...> - 2009-04-30 04:12:28
|
Revision: 5369 http://jnode.svn.sourceforge.net/jnode/?rev=5369&view=rev Author: chrisboertien Date: 2009-04-30 04:12:27 +0000 (Thu, 30 Apr 2009) Log Message: ----------- New openReader methods for IOUtils and some cat updates Signed-off-by: chrisboertien <chr...@gm...> Modified Paths: -------------- trunk/cli/src/commands/org/jnode/command/file/CatCommand.java trunk/cli/src/commands/org/jnode/command/util/IOUtils.java Modified: trunk/cli/src/commands/org/jnode/command/file/CatCommand.java =================================================================== --- trunk/cli/src/commands/org/jnode/command/file/CatCommand.java 2009-04-30 03:13:37 UTC (rev 5368) +++ trunk/cli/src/commands/org/jnode/command/file/CatCommand.java 2009-04-30 04:12:27 UTC (rev 5369) @@ -41,7 +41,7 @@ /** * Read files or network resources and write the concatenation to standard output. If * no filenames or URIs are provided, copy standard input to standard output. Data is - * copied byte-wise. + * copied byte-wise unless flags are used that add additional formatting to the lines. * <p> * If any file or URL cannot be opened, it is skipped and we (eventually) set a non-zero * return code. If we get an IOException reading or writing data, we allow it to propagate. @@ -50,6 +50,7 @@ * @author Andreas H\u00e4nel * @author cr...@jn... * @author Fabien DUMINY (fd...@jn...) + * @author chris boertien */ public class CatCommand extends AbstractCommand { @@ -81,6 +82,7 @@ private File[] files; private String end; private int rc = 0; + private int lineCount; private boolean squeeze; private boolean numAll; private boolean numNB; @@ -190,7 +192,6 @@ private void processReader(BufferedReader reader) throws IOException { String line; boolean haveBlank = false; - int lineCount = 0; while ((line = reader.readLine()) != null) { if (line.length() == 0) { @@ -217,8 +218,8 @@ * Attempt to open a file, writing an error message on failure. If the file * is '-', return stdin. * - * @param fname the filename of the file to be opened - * @return An open stream, or <code>null</code>. + * @param file the file to open the stream on + * @return the reader, or null */ private InputStream openFileStream(File file) { InputStream ret = null; @@ -235,15 +236,21 @@ return ret; } + /** + * Attempt to open a file reader, writing an error message on failure. If the file + * is '-', return stdin. + * + * @param file the file to open the reader on + * @return the reader, or null + */ private BufferedReader openFileReader(File file) { BufferedReader ret = null; if (file.getName().equals("-")) { ret = new BufferedReader(in, BUFFER_SIZE); } else { - try { - ret = new BufferedReader(new FileReader(file), BUFFER_SIZE); - } catch (FileNotFoundException e) { + ret = IOUtils.openBufferedReader(file, BUFFER_SIZE); + if (ret == null) { err.format(err_file, file); } } Modified: trunk/cli/src/commands/org/jnode/command/util/IOUtils.java =================================================================== --- trunk/cli/src/commands/org/jnode/command/util/IOUtils.java 2009-04-30 03:13:37 UTC (rev 5368) +++ trunk/cli/src/commands/org/jnode/command/util/IOUtils.java 2009-04-30 04:12:27 UTC (rev 5369) @@ -27,8 +27,10 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.Flushable; import java.io.InputStream; +import java.io.LineNumberReader; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; @@ -218,6 +220,66 @@ } /** + * Opens a Reader on a file. + * + * This method will not throw a FileNotFoundException like the FileReader + * constructor would. + * + * @param file the file to open the reader on + * @return the reader, or null if the file could not be opened + * @throws NullPointerException if file is null + */ + public static Reader openReader(File file) { + checkNull(file); + + try { + return new FileReader(file); + } catch (FileNotFoundException e) { + // fall through + } + + return null; + } + + /** + * Opens a BufferedReader on a file. + * + * This method will not throw a FileNotFoundException like the FileReader + * constructor would. + * + * @param file the file to open the reader on + * @param bufferSize the buffer size to use for this reader + * @return the reader, or null if the file could not be opened + * @throws NullPointerException if file is null + */ + public static BufferedReader openBufferedReader(File file, int bufferSize) { + Reader reader = openReader(file); + if (reader != null) { + return new BufferedReader(reader, bufferSize); + } + return null; + } + + /** + * Opens a LineNumberReader on a file. + * + * This method will not throw a FileNotFoundException like the FileReader + * constructor would. + * + * @param file the file to open the reader on + * @param bufferSize the buffer size to use for this reader + * @return the reader, or null if the file could not be opened + * @throws NullPointerException if file is null + */ + public static LineNumberReader openLineReader(File file, int bufferSize) { + Reader reader = openReader(file); + if (reader != null) { + return new LineNumberReader(reader, bufferSize); + } + return null; + } + + /** * Opens an OutputStream on a file for writing. * * If the file exists and has content, it will be overwritten. That is to say the append This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |