Saturday, January 9, 2010

Regular Expressions

Regular Expressions are a way to describe a set of strings, in general terms, based on some common characteristics. It is supported by the java.util.regex package. This package primarily consists of three classes: Pattern, Matcher and PatternSyntaxException

Metacharacters
A metacharacter is a character with a special meaning interpreted by the matcher. Metacharacter are also used in Path class as Globs. ([{\^-$|]})?*+. this are the metacharacters supported by this package.

It is very interesting to use regular expressions in java programs. All the principles of Mathematical Sets such as Negation, Ranges, unions, Intersections etc are applied.

tutorials

Platform Environment

Properties
Properties are configuration values manages as key/value pairs. In each pair, the key and value are both String values. To manage properties, create instances of java.util.Properties. Properties extends java.util.Hashtable

Quering Environment Variables
On the Java platform, an application uses System.getEnv to retrieve environment varibale values. Without an argument, getEnv returns a read-only instance of java.util.Map.

Passing Environment Variables to new process
Environment variables can be passed to a new process by passing it to a Process Builder object

System Properties
The System class maintains a Properties object that describes the configuration of the current working environment. The system properties can be read and written as well

The Security ManageA security manager object can be defined on every application to have a security policy. If any action or event not allowed by security policy is performed, then the Security Exception is thrown.

Concurrency

Java supports concurrecy with the support of java.util.concurrency package.

Process and Threads


In concurrent programming, there are two basic units of execution: processes and threads. In Java programming language, concurrent programming is mostly concerned with threads.

Threads can exists in more than one number. Every applicatoin has at least one thread -- or several, if you count "system" threads that do things like memory management and signal handling

Each thread is associated with an instance of the class Thread.
Defining and Starting a Thread
An applicaiton that crates an instance of Thread must provide the code that will run in that thread. There are two ways to do this:
  • Provide a Runnable object: The Runnable interface defines a single method, run, meant to contain the code executed in the thread. The Runnable object is passed to the Thread, as shown below:
    public class HelloRunnable implements Runnable {

       public void run() {
          System.out.println("Hello from a thread!");
       }

       public static void main(String args[]) {
          (new Thread(new HelloRunnable())).start();
       }

    }
  • Subclass Thread. The Thread class itself implements Runnable, through its run method does nothing. An application can subclass Thread, providing its own implementation of run as follows:
    public class HelloThread extends Thread {

       public void run() {
          System.out.println("Hello from a thread!");
       }

       public static void main(String args[]) {
          (new HelloThread()).start();
       }

    }
Pausing Execution with Sleep
Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. There are two overloaded methods of sleep.

It is important to declare that the main method throws an InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active.

Interrupts
Interrupts are supported by threads with the help of interrupt method.

Joins
The join method allows one thread to wait for the completion of another.
Synchronization
Threads communicate by sharing access to fields and the objects reference fields refer to. This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors.

Thread Interference
This is about how errors are introduced when multiple threads access shared data. This situation occurs when a single object is referenced by 2 or more threads.

Memory Consistency Errors
Memory consistency errors occur when differenct threads have inconsistent views of what should be the same data. The key to avoiding memory consistency errors is understanding the happens-before relationship. This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement.

Two statements that we know, which create a happens-before relationships are:
  • Thread.start
  • Thread.join


Synchornized Methods and Statements
Synchronized methods allow only execution of a single object at a time while all other objects are blocked. Constructors cannot be synchronized. Methods and statements are declared as synchronized by having the keyword synchronized added in the declaration.

Some other important concepts that are part of this Concurrency section are given belows:

tutorials

Thursday, January 7, 2010

Basic I/O: File I/O

A file system stores and organizes files on some form of media, generally one or more hard drives. Most file systems have a tree or hierarchial structure. At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories. Path is defined as the route from the root node to the target file or folder. Paths can be Relative or Absolute.

Symbolic Links
These are also referred to as a symlink or a soft link, in short. A symbolic link is a special file that serves as a reference to another file. In normal words, it is a substitute to a long file name that includes the path. Symbolic links are transparent to applications, and operations on symbolic links are automatically redirected to the target of the link.

The Path Class

The Path class, is introduced in the JDK7 release belongs to the java.nio.file package. As its name implies, the Path class is a programmatic representation of a path in the file system. A Path oject contains the file name and directory list used to construct the path.

The Path class offers many features, that fall in either of the 2 categories.
  • Path operations
  • File operations
Path Operations
The Path class includes various methods that can be used to obtain information about the path, access elements of the path, convert the path to other forms, or extract portions of a path.

Creating a Path: A Path instance contains the information used to specify the location of a file or directory. A Path can easily be creating by using one of the following get methods:
Path p1 = Paths.get("/tmp/foo");
Path p2 = Paths.get(args[0]);
Path p3 = Paths.get("file:///Users/joe/FileTest.java");
Retrieving Information about a Path
Different information about a path can be known by using some methods given below:
  • toString
  • getName
  • getNameCount
  • subpath
  • getParent
  • getRoot
  • isHidden


Path Conversions
A Path can be converted into many different forms with simple method calls. For example, to convert it into a string that can be opened from a browser, you can use toUrl().

toAbsolutePath method returns a Path object that is an absolute path of the given input.

The toRealPath method returns the real path of an existing file. This method performs several operations in one:
  • If true is passed to this method and the file system supports symbolic links, this method resolves any symlinks
  • If the Path is relative, it returns an absolute path
  • If the Path contains any redundant elements, it returns a path with those elements removed.


Joining Two Paths with by using the resolve() method.

Creating a Path Between Two Paths using the relativize() method. The new path created will be relative to the original path.
File Operations
The Path class offers a rich set of methods for reading, writing and manipulating files and directories.

Catching Exceptions
With file I/O, unexpected conditions are a fact of life: a file exists when expected, the program doesn't have access to the file system, the default file system implementation does not support a particular function, and so on.

All the methods that access the file system can throw an IOException. It is best practice to catch these exceptions by embedding these methods into a try block and to catch any exceptions in a catch bock. If in a program any streams or channels are opened then you should close them in a finally block.

Varargs
It stands for Variable Arguments. Several Path methods accept an arbitrary number of arguments when flags are specified. For example:
Path moveTo(Path, CopyOption...)
When a method accepts a varargs argument, you can pass it a comma-separated list of values or an array(CopyOption[]) of values.

Atomic Operations
Several Path methods, such as moveTo, can perform certain operations atomically in some file systems.

Method Chaining
Many of the I/O methods support the concept of method chaining. You first invoke a method that returns an object. You then immediately invoke a method on that object, which returns yet another object, and so on.

FileRef Interface
The Path class implements the FileRef interface. The FileRef interfacce includes methods for locating a file and accessing that file.

What is a Glob
2 methods in the Path class accept a glob argument, but what is a glob?

A glob pattern is specified as a string and is matched against other strings, such as directory or file names. Glob syntax follows several simple rules:
  • An asterik, *, matches any number of characters
  • 2 asterisks, **, workds like * but crosses directory boundaries.
  • A question mark, ?, matches exactly one character.
  • Braces specify a collection of subpatterns. For example:
    • {sun,moon,stars} matches "sun","moon", or "stars."
    • {temp*,tmp*} matches all strings beginning with "temp" or "tmp"
  • Square brackets convey a set of single characters or a range of characters when hyphen(-) is used:
    • [aeiou] matches any lowercase vowel.
    • [0-9] matches any digit.
    • [A-z] matches any uppercase letter
    • [a-z,A-Z] matches any uppercase or lowercase letter
  • All other characters match themselves
  • To match *, ? or other special characters, you can escape them by using the backlash character.


Checking File Accessibility
To check the existence of a file or in which mode the file is opened, you can use the checkAccess(AccessMode...) method. The varargs argument can be any combination of the AccessMode options, that is, Read, Write & Execute

Deleting a File or Directory
This operation can be performed using the delete method. It throws an exception if the deletion fails. The deleteIfExists method deletes only if a file exists. Hence it does not throw the NoSuchFileException.

Copying a File or Directory
You can copy a file or directory by using the copyTo method. This method takes a varargs argument.

Moving a File or Directory
You can move a file or directory by using the moveTo method. This method takes a varargs argument.

There are many other manipulative methods in the Path class, which are given below:

tutorials

Wednesday, January 6, 2010

Baic I/O: Others

I/O from the Command Line

A program is often run from the command line and interacts with the user in the command line environment. In Java, this feature is supported in 2 ways, through the Standard Streams and through the Console
Standard Streams
Java supports three standard streams, namely System.in, System.out & System.err. These objects are defined automatically and do not need to be opened. Standard Output and Standard Error are both for output; having error output separately allows the user to divert regular output to a file and still be able to read error messages. System.out and Stystem.err are defined as PrintStream objects. System.in is sub-type of byte stream. To use it for character input, it has to be wrapped with InputStreamReader as follows:
InputStreamReader cin = new InputStreamReader(System.in);
The Console
Console is more advanced than Standard Streams. It is particularly useful for secure password entry, which is implemented using the readPassword method. The Console object also provides input and output streams through its reader and writer methods.

Inorder to use Console, its object must be invoked using System.console(). If the object is available, then this method returns it else a NULL is returned. Non-availability might be because OS doesn't support or because the program was launched in a noninteractive environment.

Data Streams

Data streams support binary I/O of primitive data type values as well as String values. All data streams implement either the DataInput interface or the DataOutput interface. DataInputStream and DataOutputStream are the most widely used implementations of these interfaces. DataStreams can only be created as a wrapper for an existing byte stream object:
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
. Some methods used in DataStreams are:
writeDouble(double);
writeInt(int);
writeUTF(string);
readDouble();
readInt();
readUTF();
It is important that a data stored with a particular write method, should be read with the corresponding read method. It is up to the programmer to make sure that output types and input types are matched in this way.

Object Stream

Object streams are used for supporting I/O of objects. Most standard classes support this. The object stream classes are ObjectInputStream and ObjectOutputStream. These classe implement ObjectInput and ObjectOutput, which are subinterface of DataInput and DataOutput.

Monday, January 4, 2010

Basic I/O: Scanning and Formatting

Scanning

The Scanner class objects are used for breaking down formatted input into tokens and translating individual tokens according to their data type.

Breaking Input into Tokens
By default, a scanner uses whitespace to separate tokens. To set a different token separator, we can use useDelimiter() method, along with a regular expression of the delimiter. To use comma (,) as a delimiter, we can use the following statement.
s.useDelimiter(",\\s*");
where 's' is a Scanner object.

Normally, all input tokens are considered to be String values. But Scanner class provides methods to support tokens of all other primitive types also.

Formatting

Stream objects that implement formatting are instances of either PrintWriter and PrintStream classes. Both the classes implement the same set of methods for converting internal data into formatted output. Two levels of formatting are provided:

print and println Methods
Used to format individual values in a standard way

format method
This method formats almost any number of values based on a format string, with any options for precise formatting. The format string consists of static text embedded with format specifiers; except for the format specifiers, the format string is output unchanged. The API specification of Formatter class gives a complete description about the various usages of format method.

There are different format specifiers for different types of data outputs. The most common of them are %d, %f, %n etc. %n is used as line separator in JAVA. The format specifier contains the following elements:
  • begin format specifier(mandatory)
  • arguement index
  • flas
  • width
  • precision
  • conversion(mandatory)
The elements inbetween first & last are optional.


tutorials

Basic of I/O: I/O Streams

An I/O Stream represents an input source or an output destination. A stream can represnt many different kinds of sources and destinations, including disk files, devices, other programs and memory arrays.

A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time. A program uses an output stream to write data to a destination, one item at a time.

Byte Streams

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream. There are many byte stream classes. The following are two such classes

FileInputStream & FileOutputStream
read() method of FileInputStream is used to read bytes from the file. The bytes read from the file are returned in the form of int values. The return-type of int allows the read() method to return -1 when the End of File is reached.

All open streams must be closed mandatorily to avoid serious resource leaks. Before calling the close() method, the stream variable must checked if it has an object reference.

Character Streams
The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.

All character stream classes are descended from Reader and Writer. FileReader and FileWriter are specialized character stream classes for File I/O. Here also, the read() method is used to return an int value.

Character I/O usually occurs in bigger units than single characters. One common unit is the line. A line terminator can be a carriage-return/line-feed sequence("\r\n"). BufferedReader is an input character stream reader, used to buffer characters from an input character stream as per requirement. The readline() method, is used to read characters from a file until a '\n' or '\r' character is encountered.

Buffered Streams
When unbuffered I/O is performed, each read or write request is handled directly by the underlying OS. This is an inefficient process, as it involves disk access each time a read or write operation is performed. To reduce this kind of overhead, buffered I/O streams are provided. Buffered input streams read data from a memory aread known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

A program can convert an unbuffered stream into a buffered stream by wrapping. During wrapping, the unbuffered stream object is passed to the constructor for a buffered stream class. Example:
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
Some buffered output classes support autoflush, specified by an optional constructor argument. PrintWriter object flushes the buffer on every invocation of println or format methods.