Package Torello.Java
Class MSDOS
- java.lang.Object
-
- Torello.Java.OSCommands
-
- Torello.Java.MSDOS
-
- All Implemented Interfaces:
java.lang.Cloneable
public class MSDOS extends OSCommands implements java.lang.Cloneable
This Class ExtendsOSCommands:
ClassOSCommandsis the parent class for all classes which invoke Operating-System Processes using the Java-Standardjava.lang.ProcessObject-Utility.OSCommandshas several constructors inherited by this class which allow a user to configure this class for sending its output in any number of ways toAppendableinstances / loggers of your choice. Read this class documentation about invoking a constructor using any number of log-configurations, for sending text-output to any neccessary destination.
All Methods ReturnOSResponse:
In addition to real-time process-output printing, this class will collect any all text sent to Standard-Output and Error-Output and save theString-data for return to the user. All Operating-System calls in this class (which are all invoked by these methods, below) will always return an instance ofOSResponse.
ClassOSResponsealso contains the fieldresponse, which signals what the OS-Process' return-value was. Operating-Systems, generally, return a'0'to indicate the process ran to completion without errors, and some other value to signal an error occurred.java.lang.ProcessExtrasjava.lang.Processprovides several additional features that allow a user to redirect output, and collect-input to and from any number of additional sources, among others.Torello.Javatakes advantage of these withclass OSExtras. Copious documentation about this class is provided, explaining these features.
To specialize an Operating-System call with anOSExtrasinstance, just call this class'setOSExtras(...)method, with any neccessary configurations.A great tool for invoking commands to an MS-DOS operating-system. Allows a straight-forward way to make simple command-line / operating-system calls to an Microsoft DOS terminal.
WORA:
In its inception, Java made the "Write Once, Run Anywhere" (WORA) pledge. The methods in this class are not in any way "Java-ized" versions of the Operating System Calls. Rather, they are just wrappers aroundjava.lang.Processallowing a user to BOTH make Operating System calls easily AND to efficiently retrieve the generated text-output produced by the calls.
The bulk-work of the classes in this JAR Library were written and tested in a UNIX environment. While everything else in theJava HTML JAR Libraryis, indeed, O/S Independent (adhering to 'WORA'), this class and all classes which extendOSCommandsare not! In fact, they are highly O/S dependent. The methods in these classes are actually just a set of hooks into native Operating System calls.This does mean that any invocation of these methods, when running in some other Operating System Environment, will just throw exceptions.
Wild Cards:
The behavior of expanding an'*'or'?'is another somewhat Operating-System Dependent phenomenon. It may be the case that Wild-Cards are expanded by the Operating-System, before the call. In such scenarios, when Java makes a call to the O/S it must have expanded its Wild-Cards.
Logging Output:
This class inheritsOSCommands, which exports fourAppendableConfiguration-Fields for collecting the output of the Operating-System Commands in this class. These fields are all nullable, meaning that when one of these fields are assigned null, no exceptions will throw. Instead, that particular logging feature will simply be deactivated.
The table below explains whatjava.lang.ProcessOutput-Text is collected by each of thepublicConfigurable-Fields exported by (parent-class)OSCommands. All four of these are declaredpublic& non-final, so they are easily configured - just assign them to anyjava.lang.Appendableof your choosing!.
Each of these four configuration-fields will log a slightly different variant of thejava.lang.Processobject's output-text.Configurable Field Logged-Text Description / Type outputAppendableThe text that is sent to this log is the unified text produced by reading the java.lang.Processinstance's Standard-Output Stream, and its Error-Output Stream. Both are sent, as best as possible, in the order that such text is received by the reader threads.commandStrAppendableThis log simply collects the actual command being issued to the Process-Object. The text sent to this log is nothing more than the joined text of input-command'sString[]-Array.standardOutputThis log collects all text that is sent to Standard-Output when a command is executed. errorOutputThis log collects all text that is sent to Error-Output when a command is executed.
Both Command & Output:
One common trick, in Java HTML, is to provide the sameAppendableto bothoutputAppendableandcommandStrAppendable, which allows for printing a command as it is being executed, and then subsequently, all of its output.
In the two examples below, an instance of this class is built such that BOTH the command as aStringAND the process' output are logged directly to the exact sameAppendable. Here, the most commonly usedAppendableof all,java.lang.System.out, is configured. Note that these two examples are identical, and (slightly) different ways to perform the exact same configurations:
// Use a constructor to build an instance of this class. // Both the Process-Output and the Command-as-a-String are printed to System.out MSDOS dos = new MSDOS(System.out, System.out, null, null);
// Again - System.out is assigned to both "outputAppendable" and "commandStrAppendable" // The code here is functionally identical to the code in the previous example. MSDOS dos = new MSDOS(); dos.outputAppendable = System.out; dos.commandStrAppendable = System.out;
Appendable Help-Reminder: The following table provides a small reminder & description for several of the common ways to provide a Java Appendable to any method (or field) which accepts anAppendabletype. This table is re-printed in a few locations throughout the Java-HTML Library whereAppendable'sare accepted.
This expects an implementation of Java'sjava.lang.Appendableinterface which allows for a wide range of options when logging intermediate messages.Class or Interface Instance Use & Purpose 'System.out'Sends text to the standard-out terminal Torello.Java.StorageWriterSends text to System.out, and saves it, internally.FileWriter, PrintWriter, StringWriterGeneral purpose java text-output classes FileOutputStream, PrintStreamMore general-purpose java text-output classes
Checked IOException:
TheAppendableinterface requires that the Checked-ExceptionIOExceptionbe caught when using itsappend(...)methods.- See Also:
OSCommands,OSResponse
Hi-Lited Source-Code:- View Here: Torello/Java/MSDOS.java
- Open New Browser-Tab: Torello/Java/MSDOS.java
File Size: 7,516 Bytes Line Count: 206 '\n' Characters Found
-
-
Field Summary
-
Fields inherited from class Torello.Java.OSCommands
commandStrAppendable, errorOutput, osExtras, outputAppendable, standardOutput
-
-
Constructor Summary
Constructors Modifier Constructor MSDOS()MSDOS(Appendable outputAppendable)MSDOS(Appendable standardOutput, Appendable errorOutput)MSDOS(Appendable outputAppendable, Appendable commandStrAppendable, Appendable standardOutput, Appendable errorOutput)protectedMSDOS(MSDOS other)
-
Method Summary
Copying Files Modifier and Type Method OSResponseCOPY(String[] src, String target)OSResponseCOPY(String src, String target)OSResponseCOPY(Vector<String> src, String target)Deleting Files Modifier and Type Method OSResponseDEL(String s1)OSResponseDEL(String[] sArr)Methods: interface java.lang.Cloneable Modifier and Type Method MSDOSclone()-
Methods inherited from class Torello.Java.OSCommands
printAndRun, toString
-
-
-
-
Constructor Detail
-
MSDOS
public MSDOS()
This constructor assigns default values to all of the output loggingAppendable's.
Note that all of them are assigned 'null' - meaing their output will not be logged - except for the primary outputAppendablefield.this.outputAppendable. This field is assignedSystem.out, meaning that all text output by a process will be sent to the terminal window.- Code:
- Exact Constructor Body:
super();
-
MSDOS
public MSDOS(java.lang.Appendable outputAppendable)
This constructor allows a user to assign an instanceAppendableto:appendable.
The first collects all text-output generated by the processes and prints it using theAppendable'sprinting methods. This includes text generated for BOTHStandard-OutputANDError-Output. This text is collected in real time as it is output by the underlying process.Assigning null: Any of this classAppendable'smay be assigned null. When anAppendableis assigned null, it is simply ignored.NullPointerExceptionwill not throw, and any output text that would be appended, isn't appended.- Parameters:
outputAppendable- This parameter's reference is assigned directly to theOSCommandsfield:appendable. To clearly understand the use of this field, please review the documentation linked for that field. To summarize, thejava.lang.Appendablethat is synonymously namedappendablesimply collects all output text generated by a processes as it executes, and sends it to the reference'sappend(...)method. This text will include that sent to BOTHStandard-OutputAND text sent toError-Output. The text that is printed is transmitted in the order it is received from the reader-threads that read the underlying process' text-pipes.
By default this parameter is assignedSystem.out. When this default value is used, it means that as the process executes, any and all text it prints is sent to the UNIX or MS-DOS Operating System Terminal Window.This field may be assigned null, and when so it is just ignored. Assigning null toappendable. will not produce aNullPointerException.- See Also:
OSCommands.outputAppendable- Code:
- Exact Constructor Body:
super(outputAppendable);
-
MSDOS
public MSDOS(java.lang.Appendable outputAppendable, java.lang.Appendable commandStrAppendable, java.lang.Appendable standardOutput, java.lang.Appendable errorOutput)
Constructor that allows for assigning all four text-collectingAppendable's. Please review the documentation for each of these fields to understand better how theseAppendable'smay be used. There are links in the See-Also section, below.Assigning null: Any of this classAppendable'smay be assigned null. When anAppendableis assigned null, it is simply ignored.NullPointerExceptionwill not throw, and any output text that would be appended, isn't appended.- Parameters:
outputAppendable- This parameter's reference is assigned directly to theOSCommandsfield:appendable. To clearly understand the use of this field, please review the documentation linked for that field. To summarize, thejava.lang.Appendablethat is synonymously namedappendablesimply collects all output text generated by a processes as it executes, and sends it to the reference'sappend(...)method. This text will include that sent to BOTHStandard-OutputAND text sent toError-Output. The text that is printed is transmitted in the order it is received from the reader-threads that read the underlying process' text-pipes.
By default this parameter is assignedSystem.out. When this default value is used, it means that as the process executes, any and all text it prints is sent to the UNIX or MS-DOS Operating System Terminal Window.This field may be assigned null, and when so it is just ignored. Assigning null toappendable. will not produce aNullPointerException.commandStrAppendable- Thisjava.lang.Appendableallows for yet another output mechanism when executing operating system commands. The sole purpose of thisAppendableis to print the process command, itself, as a JavaString- exactly as it was issued to the operating system.
This allows a type of "Monitor Log" to store all commands being issued to the Operating-System, without having to include the full-text output that those process calls have produced. The value passed to this parameter is assigned to theOSCommandsfield namedprintCmdAppendable.This parameter may be null, and if it is, no such output will be generated.standardOutput- ThisAppendableis assigned to the internalOSCommandsfield:standardOutput. ThisAppendabe, as the name hopefully implies, allows a user to collect any / all text sent by the process toStandard-Output.Please review the documentation for that field to better understand it's use. Also, keep in mind that null may be passed to this parameter, and if / when it is the correspondingAppendablefield will simply be ignored during the process execution.errorOutput- ThisAppendableis assigned to the internalOSCommandsfield:errorOutput. ThisAppendabe, as the name hopefully implies, allows a user to collect any / all text sent by the process toError-Output.Please review the documentation for that field to better understand it's use. Also, keep in mind that null may be passed to this parameter, and if / when it is the correspondingAppendablefield will simply be ignored during the process execution.- See Also:
OSCommands.outputAppendable,OSCommands.commandStrAppendable,OSCommands.standardOutput,OSCommands.errorOutput- Code:
- Exact Constructor Body:
super(outputAppendable, commandStrAppendable, standardOutput, errorOutput);
-
MSDOS
public MSDOS(java.lang.Appendable standardOutput, java.lang.Appendable errorOutput)
This constructor assigns values to these internalAppendablefields:standardOutputanderrorOutputAssigning null: Any of this classAppendable'smay be assigned null. When anAppendableis assigned null, it is simply ignored.NullPointerExceptionwill not throw, and any output text that would be appended, isn't appended.- Parameters:
standardOutput- ThisAppendableis assigned to the internalOSCommandsfield:standardOutput. ThisAppendabe, as the name hopefully implies, allows a user to collect any / all text sent by the process toStandard-Output.Please review the documentation for that field to better understand it's use. Also, keep in mind that null may be passed to this parameter, and if / when it is the correspondingAppendablefield will simply be ignored during the process execution.errorOutput- ThisAppendableis assigned to the internalOSCommandsfield:errorOutput. ThisAppendabe, as the name hopefully implies, allows a user to collect any / all text sent by the process toError-Output.Please review the documentation for that field to better understand it's use. Also, keep in mind that null may be passed to this parameter, and if / when it is the correspondingAppendablefield will simply be ignored during the process execution.- See Also:
OSCommands.standardOutput,OSCommands.errorOutput- Code:
- Exact Constructor Body:
super(standardOutput, errorOutput);
-
MSDOS
-
-
Method Detail
-
clone
public MSDOS clone()
Creates a clone of'this'instance. Though unlikely of much use, this could conceivably have some function if similar, but non-identical, output mechanisms were being used.- Specified by:
clonein classOSCommands- Returns:
- An exact copy of
'this'instance - one in which all outputAppendable'shave had their references copied into the new instance. - Code:
- Exact Method Body:
return new MSDOS(this);
-
COPY
public OSResponse COPY(java.lang.String src, java.lang.String target) throws java.io.IOException
MS-DOS Command:COPY
UNIX or DOS Shell Command:
C:\>COPY <src> <target>- Parameters:
src- MS-DOS'COPY'command's "source" argumenttarget- The'COPY'command's "target" argument- Returns:
- Returns an instance of
OSResponse. This response-Object(data-record) class holds fourpublic, finalfields:
① Processresponse-code
② Text sent toStandard-Output(as ajava.lang.String)
③ Text sent toError-Output(also as aString)
④ The original Command-String
ThisOSResponseinstance may be discarded without any effect onProcessExecution. It is only provided as a convenience in case more information is required about the results of theO/Scommand invocation.Process Status: Upon completion of this method, theOperating-System java.lang.Processis guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-codewill indicate this using the valueINTERRUPTED - Throws:
java.io.IOException- If there are problems while invoking the operating-system command- See Also:
OSCommands.printAndRun(String[])- Code:
- Exact Method Body:
String[] command = { "COPY", src, target }; return printAndRun(command);
-
COPY
public OSResponse COPY(java.util.Vector<java.lang.String> src, java.lang.String target) throws java.io.IOException
MS-DOS Command:COPY
UNIX or DOS Shell Command:
C:\>COPY <src.elementAt(0)> <src.elementAt(1)> ... <src.elementAt(src.size()-1)> <target>- Parameters:
src- This may be a list of files to be copied to a target directorytarget- This is the target directory for the files listed.- Returns:
- Returns an instance of
OSResponse. This response-Object(data-record) class holds fourpublic, finalfields:
① Processresponse-code
② Text sent toStandard-Output(as ajava.lang.String)
③ Text sent toError-Output(also as aString)
④ The original Command-String
ThisOSResponseinstance may be discarded without any effect onProcessExecution. It is only provided as a convenience in case more information is required about the results of theO/Scommand invocation.Process Status: Upon completion of this method, theOperating-System java.lang.Processis guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-codewill indicate this using the valueINTERRUPTED - Throws:
java.io.IOException- If there are problems while invoking the operating-system command- See Also:
OSCommands.printAndRun(String[])- Code:
- Exact Method Body:
String[] command = new String[src.size() + 2]; command[0] = "COPY"; command[src.size() + 1] = target; for (int i=0; i < src.size(); i++) command[1 + i] = src.elementAt(i); return printAndRun(command);
-
COPY
public OSResponse COPY(java.lang.String[] src, java.lang.String target) throws java.io.IOException
MS-DOS Command:COPY
UNIX or DOS Shell Command:
C:\>COPY <src[0]> <src[1]> ... <src[src.length-1]> <target>- Parameters:
src- This may be a list of files to be copied to a target directorytarget- This is the target directory for the files listed.- Returns:
- Returns an instance of
OSResponse. This response-Object(data-record) class holds fourpublic, finalfields:
① Processresponse-code
② Text sent toStandard-Output(as ajava.lang.String)
③ Text sent toError-Output(also as aString)
④ The original Command-String
ThisOSResponseinstance may be discarded without any effect onProcessExecution. It is only provided as a convenience in case more information is required about the results of theO/Scommand invocation.Process Status: Upon completion of this method, theOperating-System java.lang.Processis guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-codewill indicate this using the valueINTERRUPTED - Throws:
java.io.IOException- If there are problems while invoking the operating-system command- See Also:
OSCommands.printAndRun(String[])- Code:
- Exact Method Body:
String[] command = new String[src.length + 2]; command[0] = "COPY"; command[src.length + 1] = target; for (int i=0; i < src.length; i++) command[1 + i] = src[i]; return printAndRun(command);
-
DEL
public OSResponse DEL(java.lang.String s1) throws java.io.IOException
MS-DOS Command:DEL
UNIX or DOS Shell Command:
C:\>DEL <s1>- Parameters:
s1- The item to be removed, using an MS-DOS command.- Returns:
- Returns an instance of
OSResponse. This response-Object(data-record) class holds fourpublic, finalfields:
① Processresponse-code
② Text sent toStandard-Output(as ajava.lang.String)
③ Text sent toError-Output(also as aString)
④ The original Command-String
ThisOSResponseinstance may be discarded without any effect onProcessExecution. It is only provided as a convenience in case more information is required about the results of theO/Scommand invocation.Process Status: Upon completion of this method, theOperating-System java.lang.Processis guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-codewill indicate this using the valueINTERRUPTED - Throws:
java.io.IOException- If there are problems while invoking the operating-system command- See Also:
OSCommands.printAndRun(String[])- Code:
- Exact Method Body:
String[] command = { "DEL", s1 }; return printAndRun(command);
-
DEL
public OSResponse DEL(java.lang.String[] sArr) throws java.io.IOException
MS-DOS Command:DEL
UNIX or DOS Shell Command:
C:\>DEL <sArr[0]> <sArr[1]> ... <sArr[sArr.length-1]>- Parameters:
sArr- AnString[] arraywhich is added to the end of this MS-DOS command. These will be the item(s) removed by the'DEL'command.- Returns:
- Returns an instance of
OSResponse. This response-Object(data-record) class holds fourpublic, finalfields:
① Processresponse-code
② Text sent toStandard-Output(as ajava.lang.String)
③ Text sent toError-Output(also as aString)
④ The original Command-String
ThisOSResponseinstance may be discarded without any effect onProcessExecution. It is only provided as a convenience in case more information is required about the results of theO/Scommand invocation.Process Status: Upon completion of this method, theOperating-System java.lang.Processis guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-codewill indicate this using the valueINTERRUPTED - Throws:
java.io.IOException- If there are problems while invoking the operating-system command- See Also:
OSCommands.printAndRun(String[])- Code:
- Exact Method Body:
String[] command = new String[sArr.length + 1]; command[0] = "DEL"; for (int i=0; i < sArr.length; i++) command[i + 1] = sArr[i]; return printAndRun(command);
-
-