Creating Batch Files Part 3
PAUSE
Pauses until the user hits a key.
This displays the familiar "Press any key to continue..." message.
--------------------------------------------------------------------------------
REM
Allows a remark to be inserted in the batch script.
REM DIR C:\WINDOWS Not run as a command
DIR C:\WINDOWS Run as a command
--------------------------------------------------------------------------------
ECHO
Setting ECHO "on" will display the batch process to the screen, setting it to "off" will hide the batch process.
@ECHO OFF Commands are NOT displayed
@ECHO ON Commands are displayed
ECHO can also be used in batch file to send output to the screen: @ECHO OFF
ECHO.
ECHO Hi, this is a batch file
ECHO.
PAUSE
ECHO. sends a blank line.
To echo special characters, precede them with a caret:
ECHO ^<
ECHO ^>
Otherwise you will get an error.
The @ before ECHO OFF suppresses the display of the initial ECHO OFF command. Without the @ at the beginning of a batch file the results of the ECHO OFF command will be displayed. The @ can be placed before any DOS command to suppress the display.
Breaking long lines of code
You may break up long lines of code with the caret ^. Put it at the end of a line, the next line must have space at the begining. Example: copy file.txt file2.txt
would be: copy file.txt^
file2.txt
--------------------------------------------------------------------------------
SET
Use to view or modify environment variables. More.
--------------------------------------------------------------------------------
LASTDRIVE
Sets the last drive in the system.
lastdrive=Q
--------------------------------------------------------------------------------
MSCDEX
Loads the CD-ROM software extensions(drivers), usually so an operating system can be then loaded from CD. See the AUTOEXEC.BAT section for special instructions concerning CD ROM installation. Installing windows from a CD when the CDROM is not yet configured
--------------------------------------------------------------------------------
The AUTOEXEC.BAT file
AUTOEXEC.BAT stands for automatic execution batch file, as in start-up automatically when the computer is turned on. Once a very important part of the operating system, it is being less used and is slowly disapearing from Windows. It is still powerful and useful. In NT versions it is called AUTOEXEC.NT, click here for more information.
Before the graphical user interface(GUI, "gooey") of Windows, turning on a PC would display an enegmatic C:\> and not much else. Most computer users used the same programs over-and-over, or only one program at all. DOS had a batch file which set certain system environments on boot-up. Because this was a batch file, it was possible to edit it and add a line to start-up the user's programs automatically.
When the first version of Windows was released users would turn their PCs on, and then type: WIN or WINDOWS at the prompt invoking the Windows interface. The next version of Windows added a line to the AUTOEXEC to start Windows right away. Exiting from Windows, brought one to the DOS prompt. This automatic invocation of Windows made a lot of people mad. Anyone who knew how to edit batch files would remove that line from the AUTOEXEC to keep Windows from controling the Computer. Most users do not even know that DOS is there now and have never seen it as Windows hides the any scrolling DOS script with their fluffy-cloud screen. At work I will often have to troubleshoot a PC by openning a DOS shell, the user's often panic, believing that I have broken their machine because the screen "turns black".
Most current versions of Windows have a folder called "Start-up." Any program or shortcut to a program placed in this folder will start automatically when the computer is turned on. This is much easier for most users to handle than editing batch files.
Old versions of DOS had a AUTOEXEC that looked like this:
@echo off
prompt $p$g
All this really did way set the DOS prompt to ">"
Later versions looked like this:
cls
@echo off
path c:\dos;c:\windows
set temp=c:\temp
Lh mouse
Lh doskey
Lh mode LPT1 retry
This AUTOEXEC.BAT loads DOS & then Windows. Sets up a "temp" directory. Loads the mouse driver, sets DOSKEY as the default and sets the printer retry mode. "Lh" stands for Load High, as in high memory.
An AUTOEXEC.BAT from a Windows 3.11 Machine
@ECHO On
rem C:\WINDOWS\SMARTDRV.EXE
C:\WINDOWS\SMARTDRV.EXE 2038 512
PROMPT $p$g
PATH C:\DOS;C:\WINDOWS;C:\LWORKS;C:\EXPLORER.4LC
SET TEMP=C:\DOS
MODE LPT1:,,P >nul
C:\DOS\SHARE.EXE /F:150 /L:1500
C:\WINDOWS\mouse.COM /Y
cd windows
WIN
This version simply sets DOS to boot to Windows.
SET HOMEDRIVE=C:
SET HOMEPATH=\WINDOWS
Whenever a program is installed on a computer, the setup program or wizard will often edit the AUTOEXEC. Many developer studios will have to "set a path" so programs can be compiled or run from any folder. This AUTOEXEC is an example of that: SET PATH=C:\FSC\PCOBOL32;C:\SPRY\BIN
SET PATH=C:\Cafe\BIN;C:\Cafe\JAVA\BIN;%PATH%
SET HOMEDRIVE=C:
SET HOMEPATH=\WINDOWS
This AUTOEXEC sets the path for COBOL and JAVA development BINs. This way, the computer knows where to look for associated files for COBOL and JAVA files if they are not located directly in a BIN folder.
Sets all the devices and boots to Windows.
When the "REM" tags are removed the device commands become visible.
@SET PATH=C:C:\PROGRA~1\MICROS~1\OFFICE;%PATH%
REM [Header]
@ECHO ON
REM [CD-ROM Drive]
REM MSCDEX.EXE /D:OEMCD001 /L:Z
REM [Display]
REM MODE CON: COLS=80 LINES=25
REM [Sound, MIDI, or Video Capture Card]
REM SOUNDTST.COM
REM [Mouse]
REM MOUSE.COM
REM [Miscellaneous]
REM FACTORY.COM
For loading Windows from a CD @echo off
MSCDEX.EXE /D:OEMCD001 /L:D
d:
cd \win95
oemsetup /k "a:\drvcopy.inf"
For loading CDROM drivers
Removing the "REM" tags uncomments the commands and runs them.
REM MSCDEX.EXE /D:OEMCD001 /l:d
REM MOUSE.EXE
Creating Batch Files Part 2
Put a disk in the drive and double-click the .bat file icon.
Batch File Utilities and Commands
Any valid DOS command may be placed in a batch file, these commands are for setting-up the structure and flow of a batch file.
CLS
Clears the screen
--------------------------------------------------------------------------------
EXIT
Exits the command-line process when the batch file terminates
EXIT
--------------------------------------------------------------------------------
BREAK
When turned on, batch file will stop if the user presses < Ctrl >-< Break > when turned off, the script will continue until done.
BREAK=ON
BREAK=OFF
--------------------------------------------------------------------------------
CALL
Calls another batch file and then returns control to the first when done.
CALL C:\WINDOWS\NEW_BATCHFILE.BAT
Call another program
CALL C:\calc.exe
Details.
--------------------------------------------------------------------------------
CHOICE
Allows user input. Default is Y or N.
You may make your own choice with the /C: switch. This batch file displays a menu of three options. Entering 1, 2 or 3 will display a different row of symbols. Take note that the IF ERRORLEVEL statements must be listed in the reverse order of the selection. CHOICE is not recognized in some versions of NT. @ECHO OFF
ECHO 1 - Stars
ECHO 2 - Dollar Signs
ECHO 3 - Crosses
CHOICE /C:123
IF errorlevel 3 goto CRS
IF errorlevel 2 goto DLR
IF errorlevel 1 goto STR
:STR
ECHO *******************
ECHO.
PAUSE
CLS
EXIT
:DLR
ECHO $$$$$$$$$$$$$$$$$$$$
ECHO.
PAUSE
CLS
EXIT
:CRS
ECHO +++++++++++++++++++++
ECHO.
PAUSE
CLS
EXIT
--------------------------------------------------------------------------------
FOR...IN...DO
Runs a specified command for each file in a set of files. FOR %%dosvar IN (set of items) DO command or command strcuture.
%%dosvar is the variable that will hold items in the list, usually a single leter: %%a or %%b. Case sensitive, %%a is different from %A. The items in the (set) are assigned to this variable each time the loop runs.
(set of items) is one item or multiple items seperated by commas that determine how many times the loop runs.
command or command strcuture is the operation you want to perform for each item in the list.
This code will run through the set (A, B, C), when it gets to B it will print the message: "B is in the set!"
FOR %%b in (A, B, C) DO IF %%b == B echo B is in the set!
This line will print the contents of C:\windows\desktop
FOR %%c in (C:\windows\desktop\*.*) DO echo %%c
So, you may create your own list or use various objects like files to determine the loop run.
Details.
--------------------------------------------------------------------------------
GOTO
To go to a different section in a batch file. You may create different sections by preceding the name with a colon. :SUBSECTION
Programmers may find this similar to funtions or sub-routines.
@ECHO OFF
:FIRSTSECTION
ECHO This is the first section
PAUSE
GOTO SUBSECTION
:SUBSECTION
ECHO This is the subsection
PAUSE
Skip sections of a batch file
@ECHO OFF
:ONE
ECHO This is ONE, we'll skip TWO
PAUSE
GOTO THREE
:TWO
ECHO This is not printed
:THREE
ECHO We skipped TWO!
PAUSE
GOTO END
:END
CLS
EXIT
Looping with GOTO
:BEGIN
REM Endless loop, Help!!
GOTO BEGIN
Use with CHOICE
--------------------------------------------------------------------------------
IF, IF EXIST, IF NOT EXIST
IF EXIST C:\tempfile.txt
DEL C:\tempfile.txt
IF NOT EXIST C:\tempfile.txt
COPY C:\WINDOWS\tempfile.txt C:\tempfile.txt
Use with "errorlevel"
The generic paramater errorlevel refers to the output another program or command and is also used with the CHOICE structure. If you try and run a command in a batch file and produces an error, you can use errorlevel to accept the returned code and take some action. For example, let's say you have a batch file that deletes some file. COPY C:\file.txt C:\file2.txt
If "file.txt" doesn't exist, you will get the error: COULD NOT FIND C:\FILE.TXT. Instead, use a structure like this to create the file, then copy it by accepting the error.
@ECHO OFF
:START
COPY file.txt file2.txt
IF errorlevel 1 GOTO MKFILE
GOTO :END
:MKFILE
ECHO file text>file.txt
GOTO START
:END
ECHO Quitting
PAUSE
an errorlevel of 1 means there was an error, errorlevel of 0 means there was no error. You can see these levels by adding this line after any line of commands: ECHO errorlevel: %errorlevel%
Creating Batch Files Part 1
Batch Files
What are batch files? Batch files are not programs, pre se, they are lists of command line instructions that are batched together in one file. For the most part, you could manually type in the lines of a batch file and get the same results, but batch files make this work easy. Batch files do not contain "compiled" code like C++ so they can be opened, copied and edited. They are usually used for simple routines and low-level machine instruction, but they can be very powerful. If you look in your C:\, C:\WINDOWS, or C:\WINNT folder you will see a multitude of .BAT, .SYS, .CFG, .INF and other types. These are all kinds of batch files. This may shock you, but while most applications are writen in Basic or C++ they sit on a mountain of batch files. Batch files are the backbone of the Windows operating system, delete them and you've effectively disabled the OS. There is a reason for this. The system batch files on each computer are unique the that computer and change each time a program is loaded. The operating system must have access to these files and be able to add and delete instructions from them.
Creating Batch files
Simple instructions
Open a text editor like notepad(NOT word or wordpad)
Type or copy this text: @ECHO OFF
ECHO.
ECHO This is a batch file
ECHO.
PAUSE
CLS
EXIT
Save this as batchfile.bat, make sure there is no .txt extension after the .bat
Double-click the file icon
This is a little batch file I wrote that I use every day. It deletes the cookies that get dumped to my hard drive every time I go online. I could set my browser preferences not to accept cookies, but sometimes cookies are useful. Some CGI pages are unusable with cookies, sometimes when you enter a password for a Website, the site uses a cookie to remember your password. I just do not need hundreds of cookie files taking up space after I close my browser. With this batch file, all I have to do is double-click it and it deletes my cookies. Feel free to cut and paste this code to your Notepad or Wordpad. Save it as cookiekill.bat on your Desktop.
cls
REM *******************************************
REM **Cookie Kill Program Will not work in NT**
REM *******************************************
deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*
pause
cls
REM Cookies deleted!
What does the batch file do? The first line has the command cls. cls clears the screen window of any previous data. The next three lines start with REM for "remark." Lines begining with REM do not contain commands, but instructions or messages that will be displayed for the user. The next two lines begin with the command deltree, deltree not only deletes files but directories and sub-directories. In this case the file is deleting the directory cookies and all the files inside. This directory is automatically rebuilt. The deltree has been passed the parameter /y, this informs the process to answer "YES" to any confirmation questions. Sometimes you type the DEL or one of its cousins, the system will ask "Are sure you want to do this?" setting /y answers these prompts without interupting the process. The pause command halts the process temporarily and shows the users a list of all the files being deleted. cls clears the screen again, another REM line tells the user that the files are deleted. The last line contains only :end and returns the process to the command prompt. This version was created to show the user everything that is taking place in the process. The version bellow does the same thing without showing the user any details.
cls
@echo off
deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*
cls
Without REM lines there are no comments. The @echo off command keeps the process from being "echoed" in the DOS window, and without the pause and :end lines, the process runs and exits without prompting the user. In a process this small it is okay to have it be invisible to the user. With more a complex process, more visual feedback is needed. In computing there is fine line between too much and too little information. When in doubt give the user the oportunity to see what is going on.
This version is a little more thurough, deletes alot of junk cls
@ECHO OFF
ECHO. ***********************************
ECHO. ** Clean Cookies and Temp Files **
ECHO. ** Will not work in NT **
ECHO. *******************************
deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.jpg
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.gif
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.htm
deltree /y c:\progra~1\Netscape\Users\default\archive\*.htm
deltree /y c:\progra~1\Netscape\Users\default\archive\*.gif
deltree /y c:\progra~1\Netscape\Users\default\archive\*.jpg
deltree /y c:\windows\temp\*.*
deltree /y c:\temp\*.*
deltree /y c:\windows\Recent\*.*
deltree /y c:\recycled\*.*
cls
EXIT
"C:\windows\history\today" will rebuld itself if you delete it. It's not a file, it's a specially configured directory structure that DOS doesn't see the same way that windows does. C:\windows\history\today doesn't actually exist as DOS sees it. Go into the C:\windows\history directory and type DIR/A this will show you the hidden directories and how they are named.
WINNT Version @ECHO OFF
ECHO **************************************************
ECHO ** DEL replaces DELTREE, /Q replaces /Y **
ECHO **************************************************
del /Q c:\docume~1\alluse~1\Cookies\*.*
REM Change alluse~1 in the above line to your userID
del /q c:\winnt\temp\*.*
del /q c:\temp\*.*
del /q c:\winnt\Recent\*.*
del /q c:\*.chk
EXIT
Add these lines for XP - Provided by Patrick R. del /q C:\Windows\Temp\Adware\*.*
del /q C:\Windows\Temp\History\*.*
del /q C:\Windows\Temp\Tempor~1\*.*
del /q C:\Windows\Temp\Cookies\*.*
One thing I do quite often is erase old floppy disks. I might have a stack of them and I don't care what's on them, but I want all the files gone including potential virii(everyone says "viruses" but "virii" is the proper term. Snob!). But I get tired of opening a DOS prompt and typing in the command to format the disk. So I wrote a one line batch file that does it for me. Save it as: "disk_wipe.bat"
format a: /u
Put a disk in the drive and double-click the .bat file icon.
Making Classes Unit-Testable
I had been working on the code review of one of our Java projects, when the following snippet struck me as a definite smell in one of the POJOs :
class TradeValueCalculator {
// ..
// ..
public BigDecimal calculateTradeValue(final Trade trade, ..) {
// ..
BigDecimal tax = TradeUtils.calculateTax(trade);
BigDecimal commission = TradeUtils.calculateCommission(trade);
// .. other business logic to compute net value
}
// ..
// ..
}
What is the problem with the above two innocuous looking Java lines of code ? The answer is very simple - Unit Testability of the POJO class TradeValueCalculator ! Yes, this post is about unit testability and some tips that we can follow to design classes that can be easily unit tested. I encountered many of these problems while doing code review of a live Java project in recent times.
Avoid Statics
When it comes to testability, statics are definitely not your friends. In the above code snippet, the class TradeValueCalculator depends on the implementation of the static methods like TradeUtils.calculateTax(..) and TradeUtils.calculateCommission(..). Any change in these static methods can lead to failures of unit tests of class TradeValueCalculator. Hence statics introduce unwanted coupling between classes, thereby violating the principle of easy unit-testability of POJOs. Avoid them, if you can, and use standard design idioms like composition-with-dependency-injection instead. And while using composition with service components, make sure they are powered by interfaces. Interfaces provide the right level of abstraction for multiple implementations and are much easier to mock while testing. Let us refactor the above snippet to compose using service components for calculating tax and commission :
class TradeValueCalculator {
// .. to be dependency injected
private ITaxCalculator taxCalculator;
private ICommissionCalculator commissionCalculator;
// ..
// ..
public BigDecimal calculateTradeValue(final Trade trade, ..) {
// ..
BigDecimal tax = taxCalculator.calculateTax(trade);
BigDecimal commission = commissionCalculator.calculateCommission(trade);
// .. other business logic to compute net value
}
// ..
// ..
}
interface ITaxCalculator {
BigDecimal calculateTax(..);
}
interface ICommissionCalculator {
BigDecimal calculateCommission(..);
}
We can then have concrete instances of these service contracts and inject them into the POJO TradeValueCalculator :
class DefaultTaxCalculator implements ITaxCalculator {
// ..
}
class DefaultCommissionCalculator implements ICommissionCalculator {
// ..
}
Using standard IoC containers like Guice or Spring, we can inject concrete implementations into our POJO non-invasively through configuration code. In Guice we can define Modules that bind interfaces to concrete implementations and use Java 5 annotation to inject those bindings in appropriate places.
// define module to configure bindings
class TradeModule extends AbstractModule {
@Override
protected void configure() {
bind(ITaxCalculator .class)
.to(DefaultTaxCalculator .class)
.in(Scopes.SINGLETON);
bind(ICommissionCalculator .class)
.to(DefaultCommissionCalculator .class)
.in(Scopes.SINGLETON);
}
}
and then inject ..
class TradeValueCalculator {
// ..
@Inject private ITaxCalculator taxCalculator;
@Inject private ICommissionCalculator commissionCalculator;
// ..
// ..
}
How does this improve testability of our class TradeValueCalculator ?
Just replace the defined Module by another one for unit testing :
// define module to configure bindings
class TestTradeModule extends AbstractModule {
@Override
protected void configure() {
bind(ITaxCalculator .class)
.to(MockTaxCalculator .class)
.in(Scopes.SINGLETON);
bind(ICommissionCalculator .class)
.to(MockCommissionCalculator .class)
.in(Scopes.SINGLETON);
}
}
What we have done just now is mocked out the service interfaces for tax and commission calculation. And that too without a single line of code being changed in the actual class! TradeValueCalculator can now be unit-tested without having any dependency on other classes.
Extreme Encapsulation
I have come across many abuses of FluentInterfaces, where developers use chained method invocations involving multiple classes. Take this example from this Mock Objects paper, which discusses this same problem :
dog.getBody().getTail().wag();
The problem here is that the main class Dog is indirectly coupled with multiple classes, thereby violating the Law of Demeter and making it totally unsuitable for unit testing. The situation is typically called "The Train Wreck" and has been discussed extensively in the said paper. The takeway from this situation is to minimize coupling with neighbouring classes - couple only with the class directly associated with you. Think in terms of abstracting the behavior *only* with respect to the class with which you collaborate directly - leave implementation of the rest of the behavior to the latter.
Privates also need to be Unit-Tested
There is a school of thought which espouses the policy that *only* public api s need to be unit-tested. This is not true - I firmly believe that all your methods and behaviors need unit testing. Strive to achieve the maximum coverage of unit testing in your classes. Roy Osherove thinks that we may have to bend some of the rules of pure OOD to make our design implementations more testable e.g. by exposing or replacing private instances of objects using interfaces, injection patterns, public setters etc. Or by discouraging default sealing of classes allowing overriding in unit tests. Or by allowing singletons to be replaced in tests to break dependencies. I think, I agree to many of these policies.
Fortunately Java provides a useful access specifier that comes in handy here - the package private scope of access. Instead of making your implementation members *private*, make them *package private* and implement unit test classes in the same package. Doing this, you do not expose the private parts to the public, while allowing access to all unit test classes. Crazy Bob has more details on this. Another useful trick to this may be usage of AOP. As part of unit test classes, you can introduce additional getters through AOP to access the implementation artifacts of your class. This can be done through inter-type declarations, and the test classes can access all private data at gay abandon.
Look out for Instantiations
There are many cases where the class that is being unit tested needs to create / instantiate objects of the collaborating class. e.g.
class TradeController {
// ..
// ..
public void doTrade(TradeDTO dto, ..) {
Trade trade = new Trade(dto);
// .. logic for trade
}
// ..
}
Increase the testability of the class TradeController by separating out all creation into appropriate factory methods. These methods can then be overridden in test cases to inject creation of Mock objects.
class TradeController {
TradeDTO dto;
// ..
// ..
public void doTrade() {
Trade trade = createTrade(dto);
// .. logic for trade
}
// ..
protected Trade createTrade(TradeDTO dto) {
return new Trade(dto);
}
}
and create MockTrade in test cases ..
class TradeControllerTest extends TestCase {
// ..
public void testTradeController(..) {
TradeController tc = new TradeController() {
protected Trade createTrade(TradeDTO dto) {
return new MockTrade(dto);
}
}
tc.doTrade();
}
}
The Factory Method pattern proves quite helpful in such circumstances. However, there are some design patterns like The Abstract Factory, which can potentially introduce unwanted coupling between classes, thereby making them difficult to unit-test. Most of the design patterns in GOF are built on composition - try implementing them using Interfaces in Java, so that they can be easily mocked out. Another difficult pattern is the Singleton - I usually employ the IoC container to manage and unit-test classes that collaborate with Singletons. Apart from static methods, which I have already mentioned above, static members are also problematic cases for unit testing. In many applicatiosn they are used for caching (e.g. ORMs) - hence an obvious problem child for unit testing.
Data Representations Part 1
All data on digital computers is represented as a sequence of 0s and 1s. This includes numeric data, text, executable files, images, audio, and video. The ASCII standard associates a seven bit binary number with each of 128 distinct characters. The MP3 file format rigidly specifies how to encode each raw audio file as a sequence of 0s and 1s. All data are numbers, and all numbers are data.
In this section we describe how to represent integers in binary, decimal, and hexadecimal and how to convert between different representations. We also describe how to represent negative integers.
Number systems.There are many ways to represent integers: the number of days in the month of October can be represented as 31 in decimal, 11111 in binary, 1F in hexadecimal, or XXXI in Roman Numerals. It is important to remember than an integer is an integer, no matter whether it is represented in decimal or with Roman Numerals.
Decimal numbers. We are most familiar with performing arithmetic with the decimal (base 10) number system. This number system has been widely adopted, in large part because we have 10 fingers. However, other number systems still persist in modern society.
Sexagecimal numbers. The Sumerians uses a sexagecimal (base 60) number system. We speculate that 60 was chosen since it is divisible by many integers: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, and 30. Most clocks are based on the sexagecimal system. The Babylonians inherited sexagecimal numbers from the Sumerians. They divided a circle into 360 degrees since they believed the Sun rotated around the Earth in about 360 days. Ptolemy tabulated trigonometric tables using base 360, and, even today, we still often use degrees instead of radians when doing geometry.
Binary numbers. Computers are based on the binary (base 2) number system because each wire can be in one of two states (on or off).
Hexadecimal numbers. Writing numbers in binary is tedious since this representation uses between 3 and 4 times as many digits as the decimal representation. The hexadecimal (base 16) number system is often used as a shorthand for binary. Base 16 is useful because 16 is a power of 2, and numbers have roughly has many digits as in the corresponding decimal representation.
Beginning with the Babylonians, we represented numbers in these systems using positional notation. A sequence of digits x = xn, xn-1, ..., x1, x0 in base b integer
x = xn bn + xn-1 bn-1 + ... + x1 b1 + x0 b0.
The xi terms are the positional digits, and each digit is required to be an integer between 0 and b - 1. In binary, the two digits (also referred to as bits) are 0 and 1; in decimal, the ten digits are 0 through 9; in hexadecimal, the sixteen digits are 0 through 9 and the letters A through F. Every nonnegative integer can be expressed using positional notation, and the representation is unique (up to an arbitrary number of leading 0s). As an example the number of days in a leap year is 36610 = 1011011102 = 16E16 since:
366 = 3×102 + 6×101 + 6× 100
366 = 1× 28 + 0× 27 + 1× 26 + 1×25 + 0×24 + 1×23 + 1×22 + 1×21 + 0×20
366 = 1×162 + 6×161 + E×160
The following table gives the binary, decimal, and hexadecimal representations of the first 48 integers.
BIN DEC HEX BIN DEC HEX BIN DEC HEX
0 0 0 10000 16 10 100000 32 20
1 1 1 10001 17 11 100001 33 21
10 2 2 10010 18 12 100010 34 22
11 3 3 10011 19 13 100011 35 23
100 4 4 10100 20 14 100100 36 24
101 5 5 10101 21 15 100101 37 25
110 6 6 10110 22 16 100110 38 26
111 7 7 10111 23 17 100111 39 27
1000 8 8 11000 24 18 101000 40 28
1001 9 9 11001 25 19 101001 41 29
1010 10 A 11010 26 1A 101010 42 2A
1011 11 B 11011 27 1B 101011 43 2B
1100 12 C 11100 28 1C 101100 44 2C
1101 13 D 11101 29 1D 101101 45 2D
1110 14 E 11110 30 1E 101110 46 2E
1111 15 F 11111 31 1F 101111 47 2F
Number conversion.You need to know how to convert from a number represented in one system to another.
Converting from base b to decimal. To convert from an integer represented in base b to decimal, multiply the ith digit by the ith power of b, and sum up the results. For example, the binary number 101101110 is 366 in decimal.
1 0 1 1 0 1 1 1 0 (binary)
256 128 64 32 16 8 4 2 1 (powers of 16)
-------------------------------------------
256 + 64 + 32 + 8 + 4 + 2 (multiplied by corresponding digit)
366
The hexadecimal number 16E is 366 in decimal.
1 6 E (hex)
256 16 1 (powers of 16)
-------------
256 + 96 + 14 (multiplied by corresponding digit)
366

Converting from decimal to base b. It is slightly more difficult to convert an integer represented in decimal to one in base b because we are accustomed to performing arithmetic in base 10. The easiest way to convert from decimal to base b by hand is to repeatedly divide by the base b, and read the remainder upwards. For example, the calculations below convert from the decimal integer 366 to binary (101101110) and to hexadecimal (16E).
Converting between base b1 and b2. One way is to convert the base b1 integer to decimal (using the first algorithm described above) and then to convert the resulting decimal integer to base b2 (using the second algorithm described above).
Converting between binary and hexadecimal. We describe a fast and elegant way to convert directly from the binary to hexadecimal representation of an integer: First, group the digits 4 at a time starting from the right; then convert each group to a single hexadecimal digit, padding 0s to the very last group if necessary. For example, the hexadecimal representation of 111010111001110001 is 3AE71.
0011 1010 1110 0111 0001
3 A E 7 1
To convert from hexadecimal to binary: convert each hexadecimal digit individually into its corresponding 4 digit binary number, removing any leading 0's.
9 F 0 3
1001 1111 0000 0011
This works because one base (16) is a power of the other (2). Likewise, it would be easy to convert between the base 125 and base 5 representations.
Number conversion in Java. Converting a string to another type is called parsing, and can be a significant computational burden, since there are normally numerous cases to consider. We have already used one such built-in static method extensively: Integer.parseInt() converts from a string of decimal digits to an integer. Program BinaryConverter.java defines a static method fromBinaryString(s) that converts a string s of binary digits to an integer, and another static method toBinaryString(n) that converts an int n to a binary string. We note that Java's library calls Integer.parseInt(s, 2) and Integer.toBinaryString(n) accomplish the same goal.
Program HexInOut.java reads in a hexadecimal integer from standard input and prints it out in hexadecimal to standard output.
Arithmetic in other number systems.One way to perform arithmetic is to convert all of the numbers to base 10, perform arithmetic as usual, and then convert back. In many cases, it is easier to perform the arithmetic directly in the given number system.
Addition. In grade school you learned how to add two decimal integers: add the two least significant digits (rightmost digits); if the sum is more than 10, then carry a 1 and write down the sum modulo 10. Repeat with the next digit, but this time include the carry bit in the addition. The same procedure generalizes to base b by replacing the 10 with the base b. For example, if you are working in base 16 and the two summand digits are 7 and E, then you should carry a 1 and write down a 5 because 7 + E = 1516. Below, we compute 456710 + 36610 = 493310 in binary (left), decimal (middle) and hexadecimal (right).
1 0 0 0 1 1 1 0 1 0 1 1 1
+ 0 0 0 0 1 0 1 1 0 1 1 1 0
-------------------------
1 0 0 1 1 0 1 0 0 0 1 0 1
4 5 6 7
+ 3 6 6
---------
4 9 3 3
1 1 D 7
+ 1 6 E
---------
1 3 4 5
FAQ on Viruses
How do I turn these .ASM files into a real virus? / Where are the "live" viruses?
If you read anything above, you know that the goal of this site is to help you learn about how things work... not to make things work for you. The ASM files contain the source code for viruses. These files let you look at how each virus operates and learn from them. If these were live virus executables, then you wouldn't be able to inspect them and learn from them on my site. When you have learned what the virus does, then you can assemble them using your favorite assembler, which will convert them into a format suitable for execution by a machine. If you don't know what an assembler is or what it does, you probably shouldn't be here unless you want to learn. There are many assembly language tutorials on the web, and many assembler how-to books available at your local book store or library. For starters, I've put together an Assembly Language Resources page with tutorials and links to good info and tools to get you started. Obviously, if you don't know assembly language, the source code files will do you no good anyway, so here's your excuse to go learn a some low-level programming.
top
- I don't know assembly language, can I program viruses in Q basic, or pascal, or C++?
Learn assembly language. (Check out the Assembly Resources page.) There is a possible time and place for higher-level languages when coding viruses (like maybe C), but 99% of all real viruses will be largely assembly language. And no... no Q basic.
top
- But what about VBS (visual basic script) viruses or Word Macro viruses? Aren't they viruses?
[Okay, I've deleted the long explanation because it just caused more flames. Here's the direct version.]
Visual Basic Scripts, Word Macros, or anything else written in VBA (visual basic for applications) are malicious scripts, which this virus site isn't particularly interested in. I don't want to hear any more flames about this. I'm not denying that there are many dedicated script writers who can be very creative when exploiting Micro$oft's naivety. But the purpose of this site's virus section is to discuss low-level computer viruses that operate at an opcode level. If you're fascinated by what a VBS e-mail worm did, great... but that's a script worm. If you want to write that in assembly and let it become a viable virus entity on it's own, then you're getting what this site is about. But no, I don't have any scripts or macros. If I started dealing with those, I feel that I would also have to create an archive of creative UNIX shell scripts and DOS batch files that had names like "ClickMe.bat" or "Hey_Root_Run_Me.sh" ...not what I'm interested in. Yes many people argue with me about this... I get enough e-mails about it. Call me a purist, or whatever, but please don't send more flames about it.
What platforms do the viruses on your site target? / I can't find the XX virus, why don't you have it?
The viruses on this site are primarily legacy viruses that target the Intel/DOS platform. Yes, there are great and interesting viruses for other platforms out there. The virus repository on this site was only meant to give examples to look at while working through some tutorials, or to learn from on your own. I never intended on housing an end-all repository of every virus in existence. If you have the source code to a virus that you think is particularly interesting or famous, then send it to me and I will include it in the repository. (No binaries please, just source.
Java Data Objects(JDO) Part 3
JDOEnhancer: Create a JDO Descriptor for the JDOEnhancer
We now have all the code for our application. The next step that we need is to create a JDO descriptor that the JDOEnhancer will use. "What is the JDOEnhancer?", I hear you scream. The JDO architecture is built with the idea that a JDO implementation can take the bytecode for your classes and manipulate them to add needed functionality. For example, the JDOEnhancer will make the class implement the PersistanceCapable interface (so you don't have to), and may implement some of the methods in that interface. So we will see that after we compile our code, we will have to run the JDOEnhancer to do the bytecode manipulation (this is something that Thought Inc. doesn't like about JDO). We need to create a descriptor file that gives information about the classes that we wish to persist. The file looks like this:
"file:/D:/Apps/OpenFusionJDO/xml/schema/jdo.dtd">
This is a basic file, but works for our needs. There is more complicated mapping available, and you can check the spec at Section 18: XML Metadata. Here is a slightly more complex mapping from the OpenFusion examples:
type="com.prismt.j2ee.jdo.examples.appKeyDepartment.Employee">
Now that we have the code and the JDO descriptor, let's put it all together and see how to build the system.
Build: Go Through the Steps to Build and Run the System
There are only a couple of steps to build the system that we have created:
Compile the code.
Run the JDOEnhancer.
Set up the database (using output from the JDOEnhancer).
Run the application.
Step One: Compile the Code
We all know how to run javac right? We just have to make sure that we set up the CLASSPATH correctly before we run it. Here is an example running Windows:
% set OPENFUSION_DIR=D:\Apps\OpenFusionJDO
% set
CLASSPATH=%OPENFUSION_DIR%\lib\connector.jar;%OPENFUSION_DIR%\
lib\jndi.jar;%OPENFUSION_DIR%\lib\log4j.jar;%OPENFUSION_DIR%\l
ib\xerces.jar;%OPENFUSION_DIR%\lib\classes12.zip;%OPENFUSION_D
IR%\lib\jdo.jar;%OPENFUSION_DIR%\lib\jta-
spec1_0_1.jar;%OPENFUSION_DIR%\lib\ofjdo.jar;.
% javac –d . Person*.javaStep Two: Run the JDOEnhancer
The JDOEnhancer takes the compiled code that we just created and the JDO descriptor file that we built previously. Here is the full syntax of the OpenFusion JDOEnhancer:
java com.prismt.j2ee.jdo.enhancer.JDOEnhancer
Mandatory Options:
-cp base directory to begin searching for classes to be
enhanced. This is not the CLASSPATH, just where our
compiled persistent classes are
-oc directory to place the enhanced classes
-pd JDO descriptor file(s)
Optional:
-db specific target database [oracle, sybase, etc]
-od directory to generate SQL scripts toHere is an example of running the JDOEnhancer for our application:
% java com.prismt.j2ee.jdo.enhancer.JDOEnhancer -oc . -pd
person.jdo -db oracle -od db -cp .Step Three: Set Up the Database (Using Output From the JDOEnhancer)
The JDOEnhancer can create database scripts to set up the database for us, as long as we give it the –db and –od switches. It will create lots of separate scripts, but one will be called load_all.sql. Open that file and load it into your favorite SQL prompt (e.g., sqlplus for Oracle).
Take a peek at the Oracle version that is created for our application:
CREATE SEQUENCE instid_seq INCREMENT BY 1
;
CREATE TABLE JDO_addressbook_Person_SCO
(
inst_id INTEGER NOT NULL,
class INTEGER NOT NULL,
JDO_address VARCHAR2(255),
JDO_email VARCHAR2(255),
JDO_homePhone VARCHAR2(255),
JDO_name VARCHAR2(255),
JDO_ssn VARCHAR2(255),
JDO_workPhone VARCHAR2(255)
)
;
CREATE TABLE JDO_addressbook_Person
(
inst_id INTEGER NOT NULL,
class INTEGER NOT NULL,
JDO_address VARCHAR2(255),
JDO_email VARCHAR2(255),
JDO_homePhone VARCHAR2(255),
JDO_name VARCHAR2(255),
JDO_ssn VARCHAR2(255),
JDO_workPhone VARCHAR2(255)
)
;
CREATE TABLE prismjdoProp
(
name VARCHAR2(255) PRIMARY KEY,
value VARCHAR2(255)
)
;
CREATE TABLE prismjdoExtents
(
class_id NUMBER(38,0) PRIMARY KEY,
class_name VARCHAR2(255) UNIQUE,
app_key VARCHAR2(255)
)
;
ALTER TABLE JDO_addressbook_Person_SCO ADD PRIMARY KEY
(inst_id, class)
;
ALTER TABLE JDO_addressbook_Person ADD PRIMARY KEY (inst_id,
class)
;
INSERT INTO prismjdoExtents VALUES(0, 'addressbook.Person',
'com.prismt.j2ee.jdo.spi.DBKey')
;
COMMIT WORK
;
INSERT INTO prismjdoProp VALUES('USE.RDBMS.TRIGGERS', 'true')
;
COMMIT WORK
;Step Four: Run the Application
Now the database is set up; we can run the application and see it roll!
% java addressbook.PersonPersistGrab the code and try it out!
Conclusion
We have shown how to work with the new JDO standard, using the OpenFusion JDO implementation. This is a new world, where developers can focus on their business needs and work with objects without having to be SQL gurus. I hope JDO takes off, even though some people in the industry think that the spec isn't quite there yet.
Java Data Objects(JDO) Part 2
Step Two: Persist Three "People" to the Database
Here we have some real meat. The persistPeople() method creates three people, using the constructor that we saw in the Person.java file. Then we see JDO at work. The first thing we do is get a persistence manager via getPersistenceManager(). Then we create a transaction where we will do our work (and we commit that work after we do it). To persist this object graph we simply call the makePersistentAll( Object[] ) method. The for() loop at the bottom of the code gets the unique ID for the persistent objects, and saves them away for later use.
public void persistPeople() {
// create an array of Person's
people = new Person[SIZE];
// create three people
people[0] = new Person("Gary Segal", "123 Foobar Lane",
"123-123-1234", "gary@segal.com",
"(608) 294-0192", "(608) 029-4059");
people[1] = new Person("Michael Owen",
"222 Bazza Lane, Liverpool, MN",
"111-222-3333", "michael@owen.com",
"(720) 111-2222", "(303) 222-3333");
people[2] = new Person("Roy Keane",
"222 Trafford Ave, Manchester, MN",
"234-235-3830", "roy@keane.com",
"(720) 940-9049", "(303) 309-7599)");
// persist the array of people
pm = pmf.getPersistenceManager();
transaction = pm.currentTransaction();
pm.makePersistentAll(people);
transaction.commit();
// retrieve the object ids for the persisted objects
for(int i = 0; i < people.length; i++) {
id.add(pm.getObjectId(people[i]));
}
// close current persistence manager to ensure that
// objects are read from the db not the persistence
// manager's memory cache.
pm.close();
}Here are some of the other methods that you can call on the persistence manager. The three categories are:
Make instances persistent. Take a transient object and persist it.
Delete persistent instances. Delete the information from the datastore.
Make instances transient. Disassociate the instances from the persistence manager. The datastore doesn't have its information deleted.
Make instances persistent Delete persistent instances Make instances transient
makePersistent(Object o) deletePersistent(Object o) makeTransient(Object o)
makePersistentAll(Object[] os) deletePersistentAll(Object[] os) makeTransientAll(Object[] os)
makePersistentAll(Collection os) deletePersistentAll(Collection os) makeTransientAll(Collection os)
Step Three: Display the "People" From the Database
Our display code starts by getting the persistence manager (as all the code will do). We use the object IDs that we saved in the persistPeople() method above to give us our object back. Once we have our object, we can call the methods that the object implements -- in this case gets to give us our data back. At this point you are probably seeing there isn't a lot of code needed to persist your objects.
public void display(int end) {
Person person;
int max = end <= SIZE ? end : SIZE;
// get a new persistence manager
pm = pmf.getPersistenceManager();
// retrieve objects from datastore and display
for(int i = 0; i < max; i++) {
person = (Person) pm.getObjectById(id.elementAt(i),
false);
System.out.println("Name : " + person.getName());
System.out.println("Address : " +
person.getAddress());
System.out.println("SSN : " + person.getSsn());
System.out.println("Email : " + person.getEmail());
System.out.println("Home Phone: " +
person.getHomePhone());
System.out.println("Work Phone: " +
person.getWorkPhone());
}
pm.close();
}Step Four: Change the Name of One of the People
The code to change a Person that exists in the datastore is simple, too. It should look very similar to the code to display the "people." Here we are creating a transaction (since we are modifying the row), changing the name using the setName() method that we defined, and finally, committing the transaction to save the changes back. The only real difference between this operation and working with transient objects is that we are thinking about transactions.
public void change() {
Person person;
// retrieve objects from datastore
pm = pmf.getPersistenceManager();
transaction = pm.currentTransaction();
// change DataString field of the second persisted object
person = (Person) pm.getObjectById(id.elementAt(1),
false);
person.setName("Steve Gerrard");
// commit the change and close the persistence manager
transaction.commit();
pm.close();
}Step Five: Delete a Person
Could you have guessed the code needed to delete the second person from the datastore? You know all of the information. Looking at the code below you will see that we are using the deletePersistent() method mentioned in the persistence manager methods in Step Two.
public void delete() {
// retrieve objects from datastore
pm = pmf.getPersistenceManager();
transaction = pm.currentTransaction();
// delete the 2nd persisted object from the datastore and
// its id from Vector id.
pm.deletePersistent(pm.getObjectById(id.remove(1),
false));
// commit the change and close the persistence manager
transaction.commit();
pm.close();
}Step Five: Run Through These Things in the main() method
Finally, this program has a main() to run through, persist the people, change one, and then delete it. If you run this, you will see the address book displayed at each point.
public static void main(String[] args) {
System.out.println("Create PersonPersist");
PersonPersist personPersist = new PersonPersist();
System.out.println("Setup and persist a group of people");
personPersist.persistPeople();
System.out.println("Display the persisted people");
personPersist.display(SIZE);
System.out.println("Change a name ");
personPersist.change();
personPersist.display(SIZE);
System.out.println("Delete a person ");
personPersist.delete();
personPersist.display(SIZE - 1);
}
JDO (java data objects) Part 1
Sun's Java Data Objects (JDO) standard. JDO allows you to persist Java objects, supporting transactions and multiple users. It differs from JDBC in that you don't have to think about SQL and "all that database stuff." It differs from serialization as it allows multiple users and transactions. This standard allows Java developers to use their object model as a data model, too. There is no need to spend time going between the "data" side and the "object" side.
Various products -- including CocoBase, WebGain TOPLink, and Castor JDO -- try to do this for you. Now that there is a standard way to do this, we get the benefit of only having to learn one way to do it. This is just like when JDBC came along and allowed us to work with any database that provided a driver. Great stuff!
We will take the Address Book example from my XML data binding article and "port" it over the JDO world. The JDO implementation that I will use is OpenFusion JDO from Prism Technologies. As you'll see, there is only one part of the code that uses a PrismTech API; everything else uses the standard JDO API (with the implementation hidden behind the scenes).
The code for this article can be downloaded as a zip file here.
We will walk through the following tasks:
Data Object: Create the Person object that we wish to persist in the database.
Persist: Create a PersonPersist object that will take care of persisting, reading, and updating the Persons in the datastore.
JDOEnhancer: Create a JDO descriptor to tell the JDOEnhancer what we are doing.
Build: Go through the steps to build and run the system.
Data Object: Create the Person Object
We'll start with the same Person object we defined in the XML article. This object follows the standard JavaBean conventions of get and set on the attributes. Notice that although we are persisting this class, there is nothing special about it. It doesn't have to inherit or implement any persistent interface/base class. The requirements for a class that can be persisted are:
Fields must be accessible to the JDO classes (public, or set* methods).
The field's data type must be permitted in accordance with the JDO spec (see section 6.4 for detailed information).
Certain fields can not be supported (unserializable ones like Thread, File, Socket).
With these requirements in mind, let's look at Person.java:
public class Person {
private String name;
private String address;
private String ssn;
private String email;
private String homePhone;
private String workPhone;
// -- allows us to create a Person via the constructor
public Person(String name, String address, String ssn,
String email, String homePhone, String workPhone) {
this.name = name;
this.address = address;
this.ssn = ssn;
this.email = email;
this.homePhone = homePhone;
this.workPhone = workPhone;
}
// -- accessors
public String getName() { return name; }
public String getAddress() { return address; }
public String getSsn() { return ssn; }
public String getEmail() { return email; }
public String getHomePhone() { return homePhone; }
public String getWorkPhone() { return workPhone; }
// -- mutators
public void setName(String name) { this.name = name; }
public void setAddress(String address) {
this.address = address;
}
public void setSsn(String ssn) { this.ssn = ssn; }
public void setEmail(String email) { this.email = email; }
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
}Persist: Create a PersonPersist Object to Manage Persistence
So we have a Person object that we want to work with. Now we need to create something that will manage the persistence. Let's walk through the code, and see how we:
Initialize the JDO Persistence Manager.
Persist three "people" to the database.
Display the people from the database.
Change the name of one of the people.
Delete a person.
Run through these things in the main() method.
Step One: Initialize the JDO Persistence Manager
Here we have the beginning of the PersonPersist object. We import the standard JDO classes and the ManagedConnectionFactory from the OpenFusion implementation. We could have abstracted that out to a separate class, of course. The constructor sets the connection factory, using the javax.jdo.PersistenceManagerFactoryClass property. This is like setting the database driver property in the JDBC world.
package addressbook;
import java.util.*;
import javax.jdo.*;
import
com.prismt.j2ee.connector.jdbc.ManagedConnectionFactoryImpl;
public class PersonPersist
{
private final static int SIZE = 3;
private PersistenceManagerFactory pmf = null;
private PersistenceManager pm = null;
private Transaction transaction = null;
// Array of people to persist
private Person[] people;
// Vector of current object identifiers
private Vector id = new Vector(SIZE);
public PersonPersist() {
try {
Properties props = new Properties();
props.setProperty("javax.jdo.PersistenceManagerFactoryClass",
"com.prismt.j2ee.jdo.PersistenceManagerFactoryImpl");
pmf = JDOHelper.getPersistenceManagerFactory(props);
pmf.setConnectionFactory( createConnectionFactory() );
} catch(Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}The connection factory is created in a static method named createConnectionFactory(). This factory needs the JDBC URL, the JDBC driver, a username, and a password. OpenFusion JDO also comes with a DBHelper that looks for a properties file containing the database settings to use.
public static Object createConnectionFactory() {
ManagedConnectionFactoryImpl mcfi = new
ManagedConnectionFactoryImpl();
Object connectionFactory = null;
try {
mcfi.setUserName("scott");
mcfi.setPassword("tiger");
mcfi.setConnectionURL(
"jdbc:oracle:thin:@localhost:1521:thedb");
mcfi.setDBDriver("oracle.jdbc.driver.OracleDriver");
connectionFactory = mcfi.createConnectionFactory();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
return connectionFactory;
}
Here we have some real meat. The persistPeople() method creates three people, using the constructor that we saw in the Person.java file. Then we see JDO at work. The first thing we do is get a persistence manager via getPersistenceManager(). Then we create a transaction where we will do our work (and we commit that work after we do it). To persist this object graph we simply call the makePersistentAll( Object[] ) method. The for() loop at the bottom of the code gets the unique ID for the persistent objects, and saves them away for later use.
Will be Continue....