Sunday, February 22, 2009

PDF Search Engine



Download PDF



Download E-Books

Free e-Book: Linux Command Line and Shell Scripting Bible



Covering the most popular Linux shells (such as bash, ash, tcsh, ksh, korn, and zsh), this reference book shows how to use commands to create scripts within each shell and demonstrates practical applications for shell scripts (including retrieving information from Web sites and sending automated reports via e-mail). For each shell, the author discusses the commands available and explains how to use these commands to create scripts that can automate common functions and reports. It also features advanced topics such as using a database and Web programming.

Features in a Nutshell

  • Understand the Linux desktop and various command-line parameters
  • Learn filesystem navigation, file handling, and the basics of bash shell commands
  • Write shell scripts to automate routine functions and reports
  • Harness nesting loops and structured commands
  • Monitor programs, master file permissions, and make queries
  • Run scripts in background mode and schedule jobs
  • Use sed, gawk, and regular expressions
  • Explore all alternate shells, including ash, tcsh, ksh, korn, and zsh

Download

[Source: bigdownload.blogspot.com]


Thursday, February 19, 2009

How To Swap Integer (and String) Variables Without Using a Temporary Variable

Say x & y are the integer variables. The challenge is to swap them without using a temporary variable.

The solution is as simple as the problem itself:
x = x + y;
y = x - y;
x = x - y;

Update 1: Jack and Alexey pointed out a typo in my solution which has since been corrected. Please see their comments below.

The simplicity of the solution appeals to me. It clearly demonstrates the meaning of assignment operator ( "=" ).

What if they are Strings?
Note: You can use String methods in Java API.

Here is the solution:
x = x + y;
y = x.substring(0, x.indexOf(y));
x = x.substring(x.indexOf(y) + y.length());

Can you see the similarity?

Can you provide a simpler solution to either of the above?

Update 2: Robert just did (see his comment below). He pointed out that the String solution doesn't work when one string is contained in another. His solution is:

x = x + y;
y = x.substring(0, x.length() - y.length());
x = x.substring(y.length());

Free Java Utility To Touch Files (Cross Platform)

This is a simple commandline Java utility which I wrote down in under 5 minutes to help in checking-in (svn commit) over 500 files which were modified but the dates weren't changed due to an error in our settings. So subversion failed to recognize it. Anyway this simple utility updates the timestamp of any file(s) and directories recursively to current time. It is extremely fast and cross-platform. It does one job and does it well. It is named after the unix utility touch, with similar functionality.


You can run it as follows:
java -classpath . Touch *.html

Replace *.html with file name(s) and directories you want to update. This requires JDK 1.5 or later.







import java.io.File;

/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}
/** Super-fast file / directory(recursive) touch.
* It doesn't ask for confirmation.
* Arguments: File / Directories to touch to current time.
*/
public class Touch {
public static void main(String ... args) {
long time = System.currentTimeMillis();
for(String fileName:args) touch(new File(fileName), time);
}

/** Recursively touch file and directories.
* @param File (file or directory) for touching.
*/
public static void touch(File file, long time) {
if(file.isDirectory()) for(File childFile:file.listFiles()) touch(childFile, time);
file.setLastModified(time);
}
}

How To Flip English Text Using Unicode - ǝpoɔıun ƃuısn ʇxǝʇ ɥsılƃuǝ dılɟ oʇ ʍoɥ

Enter Your String and Click On FLIP

How To Split Java String By Newlines



Splitting a String by newline is a common requirement. When processing textual data from files or from web we need to split the data by newline boundaries. The solution aren't hard either. Let's see a simple one liner solution using String.split().

String[] poList = pos.split("\r\n|\r|\n");

What did I do?
I am splitting the String based on a regular expression which looks for carriage return (\r) or line feed (\n) or carriage return immediately followed by line feed. These takes care of all types of newlines you may encounter. It returns, as you can see, a String array of results. Trailing empty strings are not included in the resulting array.

What's interesting?
The following will include a whole bunch of empty Strings in the result.
String[] poList = pos.split("\r|\n|\r\n");

Can you tell why?
How many alternatives can you tell like using StringTokenizer for example?

Java Runtime.Exec() Guide




Everything you ever wanted to know and should know about Java Runtime.exec().

This old but still golden article is an excellent guide to using Runtime.exec(). The key points he discusses are:
1. You need to drain the input stream to prevent because failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

2. Use waitFor() instead of exitValue() when you want to wait for the process to finish.

3. Runtime.exec() wouldn't directly execute shell commands like dir / ls, copy / cp etc. You need to invoke the shell cmd.exe / bash / sh and pass the shell commands. For example in windows your command array to execute dir would be as follows:
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir";

Personally I have successfully used Runtime.exec() on several occasions. Any C programmer should quickly find equivalence with fork and system calls in C language. As always RTFM.

AJAX-Javascript File Upload Form



Ext extension UploadForm is a new extension to easily upload multiple files from web interface to a server.

The files are added to a queue first and then uploaded. The live file upload status and statistics are shown. The extension is pretty stable and usable. You can see a demo here. It is based on high quality Ext framework and hence is cross browser compatible.

The server side of the code is not provided. So it is for you to explore and find. A sample server side implementation say in PHP or Java is what is missing from this otherwise excellent piece of work.

Details here.

How To Speedup Java Over 100%



Java is used either for long running server side applications / application servers or for running short scripts. Let's look at how you can speed-up both these type of applications.


How to speed-up server-side Java applications

It is very simple really. Just add -server after the java command like this:
java -server HelloWorld

Replace HelloWorld with your application name. That wasn't so hard was it?


How to speed-up client-side Java applications

Download and install nailgun. You may want to read the quickstart guide for details.

Nailgun is a simple application server which allows you to run Java programs rapidly through the server instance. The nailgun client (ng for linux and ng.exe for windows) is a small c program which works on both windows and linux platforms. To run Java applications you just have to substitute ng (assuming it is in path) for java. ng reduces startup time by running programs from the same instance. However it uses socket connection for communication which can be further optimized.
Let's see how much ng improves the performance for simple client side applications.

Here is a simple HelloWorld program I ran using java:

[angsuman@jaguar project]$ time java HelloWorld
Hello World!

real 0m0.107s
user 0m0.049s
sys 0m0.012s

Here is the same program run using nailgun:

[angsuman@jaguar project]$ time ../software/nailgun-0.7.1/ng HelloWorld
Hello World!

real 0m0.002s
user 0m0.000s
sys 0m0.001s

Can you see the difference?

Here is the result of running helloworld in C (compiled with gcc):

[angsuman@jaguar project]$ time ./hello
Hello World!

real 0m0.001s
user 0m0.001s
sys 0m0.001s

I am using:

[angsuman@jaguar project]$ java -version
java version "1.6.0_01″
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Server VM (build 1.6.0_01-b06, mixed mode)

The Java code is:

public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}

The c code is:

#include
int main(void)
{
printf("Hello World!\n");
}

Let's finally put to rest the myth that "Java is slow".

Fun Java Programming Puzzles