Package Torello.Java

Class CmdLine


  • public class CmdLine
    extends java.lang.Object
    A few command-line helper utilities.

    Right now there is just one method provided by this class, but perhaps it will grow in subsequent version of this HTML Scrape Package. It is intended to provide additional utilities, not exactly provided by the UNIX Shell to Java Programmers.

    CmdLine -> 'Command Line'



    Stateless Class:
    This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's @Stateless Annotation.
    • 1 Constructor(s), 1 declared private, zero-argument constructor
    • 3 Method(s), 3 declared static
    • 0 Field(s)


    • Method Summary

       
      UNIX like 'SED', written in Java
      Modifier and Type Method
      static void SED​(String fileName)
       
      UNIX 'CURL' written in Java
      Modifier and Type Method
      static void CURL​(URL url, String outFileName)
       
      Main Method can be invoked from Command Line
      Modifier and Type Method
      static void main​(String[] argv)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • main

        🡇    
        public static void main​(java.lang.String[] argv)
                         throws java.io.IOException
        This calls CURL with command line parameters:

        EXAMPLE:
         $ java Torello.Java.CmdLine jpeg_img_url // Downloads an image from a web-URL to the file-system
         
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         switch (argv[0])
         {
             case "SED"  :   SED(argv[1]);
                             break;
        
             case "CURL" :   CURL(   new URL(argv[1]),
                                     (argv.length == 3)
                                         ? argv[2] 
                                         : StringParse.fromLastFileSeparatorPos(argv[1]));
                             break;
             default :
                 System.out.println("See Torello.Java.CmdLine Documentation for use of this class.");
         }
        
      • SED

        🡅  🡇    
        public static void SED​(java.lang.String fileName)
                        throws java.io.IOException
        Performs an operation similar to UNIX SED.
        Parameters:
        fileName - A text-file on the file-system that contains String's which shall be replaced
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         String          originalStr     = LFEC.loadFile("INPUT.html");
         int             originalStrLen  = originalStr.length();
         String          replaceStr      = LFEC.loadFile("OUTPUT.html");
         int             replaceStrLen   = replaceStr.length();
         String          file            = FileRW.loadFileToString(fileName);
         int             originalFileLen = file.length();
         int[]           posArr          = StrIndexOf.all(file, originalStr);
         int             count           = posArr.length;
         Stack<String>   stack           = new Stack<String>();
         int             size            = 0;
         StringBuilder   sb              = new StringBuilder();
        
         // ******************************************************************************
         // NOTE: The following Stack<String> push/pop code merely is intended to perform
         //       an operation that is supposed to be identical to the following line of
         //       code using Java's String.replace(CharSequence, CharSequence); 
         // ******************************************************************************
         // file = file.replace(originalStr, replaceStr);
        
         stack.push(file.substring(posArr[posArr.length-1] + originalStrLen));
         for (int i=(posArr.length-2); i >= 0; i--)
         {
             String temp = file.substring(posArr[i] + originalStrLen, posArr[i+1]);
             stack.push(temp);
             stack.push(replaceStr);
             size += temp.length() + replaceStrLen;
         }
         stack.push(file.substring(0, posArr[0]));
        
         while (! stack.empty()) sb.append(stack.pop());
        
         file = sb.toString();
        
         System.out.print("Writing File:" + fileName);
        
         String changed = ((originalFileLen != file.length())
             ? (BGREEN + "SIZE-CHANGED ")
             : (BRED + "NO-SIZE-CHANGE")) + RESET;
        
         System.out.println(
             changed + " old file length: [" + originalFileLen + "], " +
             "new file length: [" + file.length() + "]\n" +
             "There were (" + BYELLOW + StringParse.zeroPad(count) + RESET + ") " +
             "String Matches where substitution was performed."
         );
        
         FileRW.writeFile(file, fileName);
        
      • CURL

        🡅    
        public static void CURL​(java.net.URL url,
                                java.lang.String outFileName)
                         throws java.io.IOException
        As of today, the version of UNIX curl command does not seem to be downloading everything properly. It downloaded an image '.png' file just fine, but seemed to have botched a zip-file. This does what UNIX 'curl' command, but does not actually invoke the UNIX operating system to do it. It just does this...
        Parameters:
        url - This may be any URL, but it is intended to be a downloadable file. It will download '.html' files fine, but you may try images, data-files, zip-files, tar-archives, and movies.
        outFileName - You must specify a file-name, and if this parameter is null, a NullPointerException will be thrown immediately. If you would like your program to guess the filename - based on the file named in the URL, please use the method URL.getFile(), or something to that effect.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con.setRequestMethod("GET");
         con.setRequestProperty("User-Agent", "Chrome/61.0.3163.100");
        
         InputStream         is  = con.getInputStream();
         FileOutputStream    fos     = new FileOutputStream(outFileName);
         byte[]              b       = new byte[5000];
         int                 result  = 0;
        
         while ((result = is.read(b)) != -1) fos.write(b, 0, result);
         
         fos.flush();    fos.close();    is.close();