FileDocCategorySizeDatePackage
Strings.javaAPI DocExample2331Mon Apr 30 12:17:28 BST 2001None

Strings

public class Strings extends Object
Strings -- extract printable strings from binary file
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: Strings.java,v 1.2 2001/04/30 15:17:28 ian Exp $

Fields Summary
protected int
minLength
Constructors Summary
Methods Summary
protected booleanisStringChar(char ch)
Return true if the character is printable IN ASCII. Not using Character.isLetterOrDigit(); applies to all unicode ranges


	                 	 
	    
		if (ch >= 'a" && ch <= 'z")
			return true;
		if (ch >= 'A" && ch <= 'Z")
			return true;
		if (ch >= '0" && ch <= '9")
			return true;
		switch(ch) {
			case '/": case '-": case ':":
			case '.": case ',": case '_":
			case '$": case '%": case '\'":
			case '(": case ')": case '[": case ']": case '<": case '>":
				return true;
		}
		return false;
	
public static voidmain(java.lang.String[] av)
This simple main program looks after filenames and opening files and such like for you.

        Strings o = new Strings();
        if (av.length == 0) {
            o.process("standard input", System.in);
		} else {
			for (int i=0; i<av.length; i++)
				try {
					o.process(av[i],
						new FileInputStream(av[i]));
				} catch (FileNotFoundException e) {
					System.err.println(e);
				}
        }
    
protected voidprocess(java.lang.String fileName, java.io.InputStream inStream)
Process one file

        try {
			int i;
            char ch;

			// This line alone cuts the runtime by about 66% on large files.
			BufferedInputStream is = new BufferedInputStream(inStream);

			StringBuffer sb = new StringBuffer();

			// Read a byte, cast it to char, check if part of printable string.
            while ((i = is.read()) != -1) {
				ch = (char)i;
				if (isStringChar(ch) || (sb.length()>0 && ch==' "))
					// If so, build up string.
					sb.append(ch);
				else {
					// if not, see if anything to output.
					if (sb.length() == 0)
						continue;
					if (sb.length() >= minLength) {
						report(fileName, sb);
					}
					sb.setLength(0);
				}
            }
            is.close();
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    
protected voidreport(java.lang.String fName, java.lang.StringBuffer theString)
Output a match. Made a separate method for use by subclassers.

		System.out.println(fName + ": " + theString);