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
:
ClassOSCommands
is the parent class for all classes which invoke Operating-System Processes using the Java-Standardjava.lang.Process
Object-Utility.OSCommands
has several constructors inherited by this class which allow a user to configure this class for sending its output in any number of ways toAppendable
instances / 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
.
ClassOSResponse
also 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.Extra's from
:java.lang.Process
in OSExtrasjava.lang.Process
provides several additional-features that allow a user to redirect output, and collect-input to and from any number of additional sources, among others. There is copious documentation aboutOSExtras
, explaining these features.
To specialize an Operating-System call with anOSExtras
instance, 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.Process
allowing 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 Library
is, indeed, O/S Independent (adhering to 'WORA'), this class and all classes which extendOSCommands
are 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 fourAppendable
Configuration-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.Process
Output-Text is collected by each of thepublic
Configurable-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.Appendable
of your choosing!.
Each of these four configuration-fields will log a slightly different variant of thejava.lang.Process
object's output-text.Configurable Field Logged-Text Description / Type outputAppendable
The text that is sent to this log is the unified text produced by reading the java.lang.Process
instance'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.commandStrAppendable
This 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.standardOutput
This log collects all text that is sent to Standard-Output when a command is executed. errorOutput
This 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 sameAppendable
to bothoutputAppendable
andcommandStrAppendable
, 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 aString
AND the process' output are logged directly to the exact sameAppendable
. Here, the most commonly usedAppendable
of 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 anAppendable
type. This table is re-printed in a few locations throughout the Java-HTML Library whereAppendable's
are accepted.
This expects an implementation of Java'sjava.lang.Appendable
interface 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.StorageWriter
Sends text to System.out
, and saves it, internally.FileWriter, PrintWriter, StringWriter
General purpose java text-output classes FileOutputStream, PrintStream
More general-purpose java text-output classes
Checked IOException:
TheAppendable
interface requires that the Checked-ExceptionIOException
be 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,504 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)
protected
MSDOS(MSDOS other)
-
Method Summary
Copying Files Modifier and Type Method OSResponse
COPY(String[] src, String target)
OSResponse
COPY(String src, String target)
OSResponse
COPY(Vector<String> src, String target)
Deleting Files Modifier and Type Method OSResponse
DEL(String s1)
OSResponse
DEL(String[] sArr)
Methods: interface java.lang.Cloneable Modifier and Type Method MSDOS
clone()
-
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 outputAppendable
field.this.outputAppendable
. This field is assignedSystem.out
, meaning that all text output by a process will be sent to the terminal window.
-
MSDOS
public MSDOS(java.lang.Appendable outputAppendable)
This constructor allows a user to assign an instanceAppendable
to:appendable
.
The first collects all text-output generated by the processes and prints it using theAppendable's
printing methods. This includes text generated for BOTHStandard-Output
ANDError-Output
. This text is collected in real time as it is output by the underlying process.
Assigning null:
Any of this classAppendable's
may be assigned null. When anAppendable
is assigned null, it is simply ignored.NullPointerException
will not throw, and any output text that would be appended, isn't appended.- Parameters:
outputAppendable
- This parameter's reference is assigned directly to theOSCommands
field:appendable
. To clearly understand the use of this field, please review the documentation linked for that field. To summarize, thejava.lang.Appendable
that is synonymously namedappendable
simply 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-Output
AND 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's
may be used. There are links in the See-Also section, below.
Assigning null:
Any of this classAppendable's
may be assigned null. When anAppendable
is assigned null, it is simply ignored.NullPointerException
will not throw, and any output text that would be appended, isn't appended.- Parameters:
outputAppendable
- This parameter's reference is assigned directly to theOSCommands
field:appendable
. To clearly understand the use of this field, please review the documentation linked for that field. To summarize, thejava.lang.Appendable
that is synonymously namedappendable
simply 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-Output
AND 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.Appendable
allows for yet another output mechanism when executing operating system commands. The sole purpose of thisAppendable
is 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 theOSCommands
field namedprintCmdAppendable
.
This parameter may be null, and if it is, no such output will be generated.standardOutput
- ThisAppendable
is assigned to the internalOSCommands
field: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 correspondingAppendable
field will simply be ignored during the process execution.errorOutput
- ThisAppendable
is assigned to the internalOSCommands
field: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 correspondingAppendable
field 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 internalAppendable
fields:standardOutput
anderrorOutput
Assigning null:
Any of this classAppendable's
may be assigned null. When anAppendable
is assigned null, it is simply ignored.NullPointerException
will not throw, and any output text that would be appended, isn't appended.- Parameters:
standardOutput
- ThisAppendable
is assigned to the internalOSCommands
field: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 correspondingAppendable
field will simply be ignored during the process execution.errorOutput
- ThisAppendable
is assigned to the internalOSCommands
field: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 correspondingAppendable
field 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:
clone
in classOSCommands
- Returns:
- An exact copy of
'this'
instance - one in which all outputAppendable's
have 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, final
fields:
① Processresponse-code
② Text sent toStandard-Output
(as ajava.lang.String
)
③ Text sent toError-Output
(also as aString
)
④ The original Command-String
ThisOSResponse
instance may be discarded without any effect onProcess
Execution. It is only provided as a convenience in case more information is required about the results of theO/S
command invocation.
Process Status: Upon completion of this method, theOperating-System java.lang.Process
is guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-code
will 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, final
fields:
① Processresponse-code
② Text sent toStandard-Output
(as ajava.lang.String
)
③ Text sent toError-Output
(also as aString
)
④ The original Command-String
ThisOSResponse
instance may be discarded without any effect onProcess
Execution. It is only provided as a convenience in case more information is required about the results of theO/S
command invocation.
Process Status: Upon completion of this method, theOperating-System java.lang.Process
is guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-code
will 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, final
fields:
① Processresponse-code
② Text sent toStandard-Output
(as ajava.lang.String
)
③ Text sent toError-Output
(also as aString
)
④ The original Command-String
ThisOSResponse
instance may be discarded without any effect onProcess
Execution. It is only provided as a convenience in case more information is required about the results of theO/S
command invocation.
Process Status: Upon completion of this method, theOperating-System java.lang.Process
is guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-code
will 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, final
fields:
① Processresponse-code
② Text sent toStandard-Output
(as ajava.lang.String
)
③ Text sent toError-Output
(also as aString
)
④ The original Command-String
ThisOSResponse
instance may be discarded without any effect onProcess
Execution. It is only provided as a convenience in case more information is required about the results of theO/S
command invocation.
Process Status: Upon completion of this method, theOperating-System java.lang.Process
is guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-code
will 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[] array
which 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, final
fields:
① Processresponse-code
② Text sent toStandard-Output
(as ajava.lang.String
)
③ Text sent toError-Output
(also as aString
)
④ The original Command-String
ThisOSResponse
instance may be discarded without any effect onProcess
Execution. It is only provided as a convenience in case more information is required about the results of theO/S
command invocation.
Process Status: Upon completion of this method, theOperating-System java.lang.Process
is guaranteed to have run to completion (or to have failed and been interrupted). If the process was interrupted, theresponse-code
will 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);
-
-