1 package com.csss.opensource.jhexdump; 2 3 import java.io.*; 4 import com.csss.opensource.util.*; 5 6 /*** 7 * This class is responsible for providing the command line implementation 8 * of JHexDump. 9 * 10 * <p>When executed while using one of the provided scripts, the command 11 * line syntax is: 12 * <p>JHexDump.sh/.bat [-noascii] [-noposid] [-help] <filename> 13 * 14 * @author Lee Walton - <a href="http://www.jzone.co.uk/">The jZone</a> 15 * @version 1.1 16 */ 17 public class JHexDumpCmdLine extends JHexDump { 18 19 /*** 20 * Filename to process 21 */ 22 private String filename; 23 24 /*** 25 * The entry point into the application. 26 * <p> 27 * Instantiates a new JHexDumpCmdLine instance, and calls the execute() method. 28 * 29 * @param args Array of command line arguments 30 */ 31 public static void main(String[] args) { 32 JHexDumpCmdLine app = new JHexDumpCmdLine(); 33 app.execute(args); 34 } 35 36 /*** 37 * Protected constructor to inhibit the instantiation of this class. 38 */ 39 protected JHexDumpCmdLine() {} 40 41 /*** 42 * Gets the filename to process. 43 * 44 * @return String 45 */ 46 protected String getFilename() { 47 return filename; 48 } 49 50 /*** 51 * Sets the filename to process. 52 * 53 * @param filename 54 */ 55 protected void setFilename(String filename) { 56 this.filename = filename; 57 } 58 59 /*** 60 * Dumps the specified file to stdout. 61 * <p> 62 * Parses the command line arguments, opens the file specified, 63 * and writes the output to System.out using JHexDumpReader. 64 * 65 * @param args Array of command line arguments 66 */ 67 protected void execute(String[] args) { 68 System.out.println("JHexDump "+Version.VERSION+" "+Version.COPYRIGHT); 69 System.out.println(); 70 System.out.println("This program is free software; you can redistribute it and/or"); 71 System.out.println("modify it under the terms of the GNU General Public License"); 72 System.out.println("as published by the Free Software Foundation; either version 2"); 73 System.out.println("of the License, or (at your option) any later version."); 74 System.out.println(); 75 System.out.println("This program is distributed in the hope that it will be useful,"); 76 System.out.println("but WITHOUT ANY WARRANTY; without even the implied warranty of"); 77 System.out.println("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"); 78 System.out.println("GNU General Public License for more details."); 79 System.out.println(); 80 System.out.println("You should have received a copy of the GNU General Public License"); 81 System.out.println("along with this program; if not, write to the Free Software"); 82 System.out.println("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."); 83 System.out.println(); 84 85 if (parseCommandLine(args)) { 86 87 if (StringUtil.isEmpty(getFilename())) { 88 System.out.println("No filename specified!"); 89 System.out.println(); 90 return; 91 } 92 93 if (! new File(getFilename()).isFile()) { 94 System.out.println("The file "+getFilename()+" does not exist."); 95 return; 96 } 97 98 System.out.println("Filename : "+getFilename()); 99 System.out.println("Size : "+(new File(getFilename())).length()); 100 System.out.println(); 101 102 InputStream instream = null; 103 try { 104 instream = new FileInputStream(getFilename()); 105 106 JHexDumpReader reader = new JHexDumpReader(instream,getBytesPerRow(),isNoAscii(),isNoPosInd()); 107 108 while(true) { 109 StringBuffer output = new StringBuffer(); 110 int read = reader.readLine(output); 111 if (read < 1) break; 112 System.out.println(output.toString()); 113 } 114 115 } catch (Exception ex) { 116 ex.printStackTrace(); 117 } finally { 118 if (instream != null) { 119 try { 120 instream.close(); 121 } catch (IOException ioex) {} 122 } 123 } 124 } 125 } 126 127 /*** 128 * Displays the command line help 129 */ 130 protected void displayHelp() { 131 System.out.println(); 132 System.out.println("Arguments: [-noascii] [-noposind] [-help] <filename>"); 133 System.out.println(); 134 } 135 136 /*** 137 * Parses the command line arguments. 138 * <p> 139 * The accepted arguments are:<p> 140 * -help : Display help message<p> 141 * -noascii : Supress the display of the ASCII representation<p> 142 * -noposind : Supress the display of the file position indicator 143 * 144 * @param args array of command line arguments 145 */ 146 protected boolean parseCommandLine(String[] args) { 147 148 for (int i = 0; i < args.length; i++) { 149 String arg = args[i]; 150 if (arg.startsWith("-help")) { 151 displayHelp(); 152 return false; 153 } 154 if (arg.startsWith("-noascii")) { 155 setNoAscii(true); 156 } 157 if (arg.startsWith("-noposind")) { 158 setNoPosInd(true); 159 } 160 } 161 162 if (args.length > 0) { 163 String filename = args[args.length-1]; 164 setFilename(filename.trim()); 165 } 166 167 return true; 168 } 169 }