Friday, August 15, 2014

How to read data and download file from internet in java

Java provides classes for network programming in the package java.net. This article shows you how to read data and download file from the Internet in Java using the class URL
Computers connected to a network can communicate with each other only if they agree on the rules of communication, called protocols, that define how to request the data. The World Wide Web (WWW) uses uniform resource locators (URLs) to identify online resources. For example, the following URL says that there is a file called training.html located at the remote host known as google.com, and that the program should use the HTTP protocol to request this file. It also states that this request has to be sent via port 80. www.google.com:80/training.html.
The hostname must be unique and it is automatically converted to the IP address of the physical server by your Internet service provider (ISP).

Finding a resource online is somewhat similar to finding a person by his or her address. The role of an IP address is similar to the role of a street number of a building, and a port plays the role of an apartment number in that building. Many people can live in the same building, just as many programs can run on the same server. A port is simply a unique number assigned to a server program running on the machine.


URL
A URL has two main components:


Note that the protocol identifier and the resource name are separated by a colon and two forward slashes.

How to create a URL
The easiest way to create a URL object is from a String that represents the human-readable form of the URL address. This is typically the form that another person will use for a URL. In your Java program, you can use a String containing this text to create a URL object:
URL myURL = new URL("http://google.com/");
The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question. You can also create URL objects from a relative URL address.

How to create URL Relative to Another
In your Java programs, you can create a URL object from a relative URL specification. For example, suppose you know two URLs at the site java-latte.blogspot.in:

You can create URL objects for these pages relative to their common base URL: http://java-latte.blogspot.in/pages/ like this:

URL myURL = new URL("http://java-latte.blogspot.in/pages/");
URL page1URL = new URL(myURL, "page1.html");
URL page2URL = new URL(myURL, "page2.html");

In order to specify these URL, URL class provides a different constructor for them. For instance,
new URL("http", "example.com", "/pages/page1.html");
This is equivalent to
new URL("http://example.com/pages/page1.html");

MalformedURLException
Each of the four URL constructors throws a MalformedURLException if the arguments to the constructor refer to a null or unknown protocol. Typically, you want to catch and handle this exception by embedding your URL constructor statements in a try/catch pair
Note: URLs are "write-once" objects. Once you've created a URL object, you cannot change any of its attributes (protocol, host name, filename, or port number).

Parsing a URL
The URL class provides several methods that let you query URL objects. You can get the protocol, authority, host name, port number, path, query, filename, and reference from a URL using these accessor methods. For more detail on these method check URL class.
Output:
Protocol : http
Port : 80
Host : java-latte.blogspot.in
Path : /2014/07/how-java-program.html

Connecting to a URL
After you've successfully created a URL object, you can call the URL object's openConnection method to get a URLConnection object, or one of its protocol specific subclasses, e.g. java.net.HttpURLConnection You can use this URLConnection object to setup parameters and general request properties that you may need before connecting. Connection to the remote object represented by the URL is only initiated when the URLConnection.connect method is called. When you do this you are initializing a communication link between your Java program and the URL over the network. For example, the following code opens a connection to the site google.com:

  • A new URLConnection object is created every time by calling the openConnection method of the protocol handler for this URL.
  • You are not always required to explicitly call the connect method to initiate the connection. Operations that depend on being connected, like getInputStream, getOutputStream, etc, will implicitly perform the connection, if necessary.

Reading Data from the Internet
There are two ways to read data from internet.
  1. Reading Directly from a URL
  2. Read from URLConnection
Reading Directly from a URL
After you've successfully created a URL, you can call the URL's openStream() method to get a stream from which you can read the contents of the URL. The openStream() method returns a java.io.InputStream object, so reading from a URL is as easy as reading from an input stream. The following small Java program uses openStream() to get an input stream on the URL http://www.google.com/. It then opens a BufferedReader on the input stream and reads from the BufferedReader thereby reading from the URL


When you run the program, you should see, scrolling by in your command window, the HTML commands and textual content from the HTML file located at http://www.oracle.com/

Read from URLConnection
However, rather than getting an input stream directly from the URL, this program explicitly retrieves a URLConnection object and gets an input stream from the connection. The connection is opened implicitly by calling getInputStream. Then, like URLReader, this program creates a BufferedReader on the input stream and reads from it. 
The output from this program is identical to the output from the program that opens a stream directly from the URL. You can use either way to read from a URL. However, reading from a URLConnection instead of reading directly from a URL might be more useful. This is because you can use the URLConnection object for other tasks (like writing to the URL) at the same time.

Connecting through HTTP Proxy Servers
For security reasons, most enterprises use firewalls to block unauthorized access to their internal networks. As a result their employees can’t directly reach the outside Internet world (or even some internal servers), but go through HTTP proxy servers.
Check the settings of your Internet browser to see if you are also sitting behind a firewall, and find out the hostname and port number of the proxy server if you are. Usually, web browsers store proxy parameters under the Advanced tabs of their Settings or Preferences menus.
For example, if the name of your proxy server is proxy.mycompany.com and it runs on port 8080.

Three method for connecting through HTTP Proxy Servers
  1. The following two lines should be added to your Java application that needs to connect to the Internet:
    System.setProperty(“http.proxyHost”,”http://proxy.mycompany.com”);
    System.setProperty(“http.proxyPort”, 8080);
  2. If you do not want to hardcode these values, pass them to your program from the command line:
    java -Dhttp.proxyHost=http://proxy.mycompany.com –Dhttp.proxyPort=8080 programName
  3. You can programmatically specifying proxy parameters is to do it via the class java.net.Proxy. The code for the same proxy server parameter would look like this (you can replace the name of the server with an IP address):
    Proxy myProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress (“http://proxy.mycompany.com”, 8080);
    url = new URL(“http://www.google.com/index.html” );
    urlConn = url.openConnection(myProxy);



How to Download Files from the Internet
Combine the class URL with the reading files techniques and you should be able to download practically any unprotected file (such as images, music, and binary files) from the Internet. The trick is in opening the file stream properly.
In order to demonstrate this program, I'm using google drive link to download txt file from internet.
After execution of this program, you will find download.txt file in your D drive.



If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

Wednesday, August 13, 2014

Basic java, javac, javadoc, jar command line options

A JDK provides several command-line tools that aid in software development. Commonly used tools include the compiler, launcher/interpreter, archiver, and documenter. In this article, you will get used to basic command options that java command line tools use. May be you are interested in reading how java program works before reading this command line tools options.



Java Compiler
The Java compiler translates Java source files into Java bytecode. The compiler creates a bytecode file with the same name as the source file but with the .class extension. Here is a list of commonly used compiler options:

javac [-options] [source files]
Compiles Java source files.

  • javac HelloWorld.java
    Compiles the program to produce HelloWorld.class.
  • javac –cp /dir/Classes/ HelloWorld.java
    The –cp and –classpath options are equivalent and identify classpath directories to utilize at compile time.
  • javac –d /opt/hwapp/classes HelloWorld.java
    The –d option places generated class files into a preexisting specified directory. If there is a package definition, the path will be included (e.g., /opt/hwapp/classes/com/oreilly/tutorial/).
  • javac –s /opt/hwapp/src HelloWorld.java
    The –s option places generated source files into a preexisting specified directory. If there is a package definition, the path will be further expanded (e.g., /opt/hwapp/src/com/oreilly/tutorial/).
  • javac –source 1.4 HelloWorld.java
    The –source option provides source compatibility with the given release, allowing unsupported keywords to be used as identifiers.
  • javac –X
    The –X option prints a synopsis of nonstandard options. For example, –Xlint:unchecked enables recommended warnings, which prints out further details for unchecked or unsafe operations.
  • javac –version
    The –version option prints the version of the javac utility.
  • javac –help
    The –help option, or the absence of arguments, will cause the help information for the javac command to be printed.

Tip :  Even though –Xlint and other -X options are commonly found among Java compilers, the –X options are not standardized, so their availability across JDKs should not be assumed.


Java Interpreter
The Java interpreter handles the program execution, including launching the application. Here is a list of commonly used interpreter options.

java [-options] class [arguments...]
Runs the interpreter.
  • java HelloWorld
    Starts the JRE, loads the class HelloWorld, and runs the main method of the class.
  • java com.java.latte.HelloWorld
    Starts the JRE, loads the HelloWorld class under the com/java/latte/ directory, and runs the main method of the class.
  • java -cp /tmp/Classes HelloWorld
    The –cp and –classpath options identify classpath directories to use at runtime.
  • java –Dsun.java2d.ddscale=true HelloWorld
    The –D option sets a system property value. Here, hardware accelerator scaling is turned on.
  • java –ea HelloWorld
    The –ea and –enableassertions options enable Java assertions. Assertions are diagnostic code that you insert in your application
  • java -da HelloWorld
    The –da and –disableassertions options disable Java assertions.
  • java –client HelloWorld
    The –client option selects the client virtual machine to enhance interactive applications such as GUIs.
  • java –server HelloWorld
    The –server option selects the server virtual machine to enhance overall system performance.
  • java –splash:images/world.gif HelloWorld
    The –splash option accepts an argument to display a splash screen of an image prior to running the application.
  • java –version
    The –version option prints the version of the Java interpreter, the JRE, and the virtual machine.
  • java [-d32 | -d64]
    The [-d32] and the [-d64] options call for the use of the 32-bit or the 64-bit data model (respectively), if available.
  • java –help
    The –help option, or the absence of arguments, will cause the help information for the java command to be printed.
  • javaw <classname>
    On the Windows OS, javaw is equivalent to the java command but without a console window. The Linux equivalent is accomplished by running the java command as a background process with the ampersand: java <classname> &


Java Program Packager
The Java Archive (JAR) utility is an archiving and compression tool, typically used to combine multiple files into a single file called a JAR file. JAR consists of a ZIP archive containing a manifest file (JAR content describer) and optional signature files (for security). Here is a list of commonly used JAR options along with examples:

jar [options] [jar-file] [manifest-files] [entry-point] [-C dir] files...
This is the usage for the JAR utility.

  • jar cf files.jar HelloWorld.java com/java/latte/ HelloWorld.class
    The c option allows for the creation of a JAR file. The f option allows for the specification of the filename. In this example, HelloWorld.java and com/java/latte/HelloWorld.class are included in the JAR file.
  • jar tfv files.jar
    The t option is used to list the table of contents of the JAR file. The f option is used to specify the filename. The v option lists the contents in verbose format.
  • jar xf files.jar
    The x option allows for the extraction of the contents of the JAR file. The f option allows for the specification of the filename.

Tip :  Several other ZIP tools (e.g., 7-Zip, WinZip, and Win-RAR) can work with JAR files.


JAR File Execution
JAR files can be created so that they are executable by specifying the file within the JAR where the "main" class resides, so the Java interpreter knows which main() method to utilize. Here is a complete example of making a JAR file executable:
  1. Create a HelloWorld.java file.
  2. Create the subfolders com/java/latte/
  3. Run javac HelloWorld.
    Use this command to compile the program and place the HelloWorld.class file into the com/java/latte directory.
  4. Create a file named Manifest.txt in the directory where the package is located. In the file, include the following line specifying where the main class is located:
    Main-Class: com.java.latte.HelloWorld
  5. Run jar cmf Manifest.txt helloWorld.jar com/java/latte.
    Use this command to create a JAR file that adds the Manifest.txt contents to the manifest file, MANIFEST.MF. The manifest file is also used to define extensions and various package-related data:
    Manifest-Version: 1.0
    Created-By: 1.7.0 (Oracle Corporation)
    Main-Class: com.java.latte.HelloWorld
  6. Run jar tf HelloWorld.jar.
    Use this command to display the contents of the JAR file:
    META-INF/
    META-INF/MANIFEST.MF
    com/
    com/java/
    com/java/latte
    com/java/latte/HelloWorld.class
  7. Finally, run java –jar HelloWorld.jar.
    Use this command to execute the JAR file.


Classpath
The classpath is an argument set used by several command-line tools that tells the JVM where to look for user-defined classes and packages. Classpath conventions differ among operating systems.

On Microsoft Windows, directories within paths are delineated with backslashes, and the semicolon is used to separate the paths:
-classpath \home\XClasses\;dir\YClasses\;.

On POSIX-compliant operations systems (e.g., Solaris, Linux,and Mac OS X), directories within paths are delineated with forward slashes and the colon is used to separate the paths:
-classpath /home/XClasses/:dir/YClasses/:.

The CLASSPATH environmental variable can also be set to tell the Java compiler where to look for class files and packages


Java Documenter
Javadoc is a command-line tool used to generate documentationon source files. The documentation is more detailed when the appropriate Javadoc comments are applied to the source code.

javadoc [options] [packagenames] [sourcefiles]
This is the usage to produce Java documentation.

  • javadoc HelloWorld.java
    The javadoc command generates HTML documentation files: HelloWorld.html, index.html, allclasses-frame.html,constant-values.html,deprecated-list.html,overview-tree.html, package-summary.html, etc.
  • javadoc –verbose HelloWorld.java
    The –verbose option provides more details while Javadoc is running.
  • javadoc –d /tmp/ HelloWorld.java
    This –d option specifies the directory where the generated HTML files will be extracted to. Without this option, the files will be placed in the current working directory.
  • javadoc –sourcespath /Classes/ Test.java
    The –sourcepath option specifies where to find user .java source files.
  • javadoc –exclude <pkglist> Test.java
    The –exclude option specifies which packages not to generate HTML documentation files for.
  • javadoc –public Test.java
    The –public option produces documentation for public classes and members.
  • javadoc –protected Test.java
    The –protected option produces documentation for protected and public classes and members. This is the default setting.
  • javadoc –package Test.java
    The –package option produces documentation for package, protected, and public classes and members.
  • javadoc –private Test.java
    The –private option produces documentation for all classes and members.
  • javadoc –help
    The –help option, or the absence of arguments, causes the help information for the javadoc command to be printed.


If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

Monday, August 11, 2014

Synchronous Queue example in Java

SynchronousQueue is a very special kind of queue - it implements a rendezvous approach in which producer waits until consumer is ready, consumer waits until producer is ready behind the interface of Queue whereas in Blocking Queue producer don't wait for consumer to ready and consumer won't waits for producer. In this article, we look in the concept of Synchronous Queue and how it differ from Blocking Queue with examples.



Synchronous Queue vs Blocking Queue
Before looking into Synchronous Queue let's see how Blocking Queue works and its approach differ from Synchronous queue.

Blocking queues are those queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

Blocking queues support the producer consumer design pattern

  • separates the identification of work to be done from the execution of that work by placing work items 
  • it removes code dependencies between producer and consumer classes
  • simplifies workload management by decoupling activities that may produce or consume data at different or variable rates.

In a producer consumer design built around a blocking queue, producers place data onto the queue as it becomes available, and consumers retrieve data from the queue when they are ready to take the appropriate action. Producers don't need to know anything about the identity or number of consumers, or even whether they are the only producer all they have to do is place data items on the queue. Similarly, consumers need not know who the producers are or where the work came from.

Example : Two people washing the dishes is an example of a producer consumer design.
One person washes the dishes and places them in the dish rack, and the other person retrieves the dishes from the rack and dries them. In this scenario, the dish rack acts as a blocking queue; if there are no dishes in the rack, the consumer waits until there are dishes to dry, and if the rack fills up, the producer has to stop washing until there is more space.
Blocking Queue - disk rack

Blocking queues simplify the coding of consumers, since take blocks until data is available. If the producers don't generate work fast enough to keep the consumers busy, the consumers just wait until more work is available. Sometimes this is perfectly acceptable (as in a server application when no client is requesting service), and sometimes it indicates that the ratio of producer threads to consumer threads should be adjusted to achieve better utilization. 

If the producers consistently generate work faster than the consumers can process it, eventually the application will run out of memory because work items will queue up without bound. 

  • The blocking nature of put greatly simplifies coding of producers; if we use a bounded queue, then when the queue fills up the producers block, giving the consumers time to catch up because a blocked producer cannot generate more work.
  • Blocking queues also provide an offer method, which returns a failure status if the item cannot be enqueued. This enables you to create more flexible policies for dealing with overload, such as shedding load, serializing excess work items and writing them to disk, reducing the number of producer threads, or throttling producers in some other manner.


Synchronous Queue
SynchronousQueue, is not really a queue at all, in that it maintains no storage space for queued elements. Instead, it maintains a list of queued threads waiting to enqueue or dequeue an element. 
In the dish washing analogy, this would be like having no dish rack, but instead handing the washed dishes directly to the next available dryer
Synchronous Queue - no dish rack

  • A synchronous queue does not have any internal capacity, not even a capacity of one.
  • Cannot peek at a synchronous queue because an element is only present when you try to remove it.
  • Cannot insert an element (using any method) unless another thread is trying to remove it
  • Cannot iterate as there is nothing to iterate
  • Does not permit null elements.
  • By default, ordering of waiting producer and consumer threads is not guaranteed.
  • It is a member of the Java Collections Framework.

While this may seem a strange way to implement a queue, it reduces the latency associated with moving data from producer to consumer because the work can be handed off directly. In a traditional queue, the enqueue and dequeue operations must complete sequentially before a unit of work can be handed off.

The direct handoff also feeds back more information about the state of the task to the producer; when the handoff is accepted, it knows a consumer has taken responsibility for it, rather than simply letting it sit on a queue somewhere much like the difference between handing a task to a colleague and merely putting it in her mailbox and hoping she gets it soon.

Since a SynchronousQueue has no storage capacity, put and take will block unless another thread is already waiting to participate in the handoff. Synchronous queues are generally suitable only when there are enough consumers that there nearly always will be one ready to take the handoff. They are well suited for handoff designs, in which an object running in one thread must sync up with an object running in another thread in order to hand it some information, event, or task. This lead to Exchanger which do similar kind of task.

Output:
Washing dish number : Washer
Drying dish number : Washer

Here the output can be first washing dish or drying dish because by default order of waiting thread is not guaranteed. 


If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

Friday, August 1, 2014

How to run a task periodically with Timer and ScheduledThreadPoolExecutor in Java

The Executor framework provides the ThreadPoolExecutor class to execute concurrent tasks using a pool of threads that avoids you all the thread creation operations. When you send a task to the executor, according to its configuration, it executes the task as soon as possible. When it ends, the task is deleted from the executor and, if you want to execute them again, you have to send it again to the executor.
But the Executor framework provides the possibility of executing periodic tasks through the ScheduledThreadPoolExecutor class. In this post, you will learn how to use this functionality of that class to schedule a periodic task.

Run task periodically with Timer class
Before that, we use Timer and TimerTask class to execute task periodically and see how it differ from ThreadPoolExecutor. In order to schedule task, you need to extend TimerTask class which provide basic 3 method : 
  • run method for the action to performed by the Timer class
  • cancel method to cancel the timer task  
  • scheduledExecutionTime to scheduled execution time of the most recent actual execution of this task.

If your program does not contain any termination point it will execute infinite time. So in order to cancel after specific time, we use sleep() method to wait for specific time and cancel() method to stop the execution of the timer task.

Run task periodically with ScheduledThreadPoolExecutor
Output

Main starting at Fri Aug 01 13:05:09 IST 2014
Main: Delay: 999
Main: Delay: 495
Periodic-Task starting at Fri Aug 01 13:05:10 IST 2014
Main: Delay: 1994
Main: Delay: 1494
Main: Delay: 993
Main: Delay: 493
Periodic-Task starting at Fri Aug 01 13:05:12 IST 2014
Main: Delay: 1992
Main: Delay: 1491
Main: Delay: 991
Main: Delay: 490
Periodic-Task starting at Fri Aug 01 13:05:14 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:16 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:18 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:20 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:22 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:24 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:26 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:28 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:30 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:32 IST 2014
Periodic-Task starting at Fri Aug 01 13:05:34 IST 2014
Main ends at Fri Aug 01 13:05:34 IST 2014

How it works...
When you want to execute a periodic task using the Executor framework, you need a ScheduledExecutorService object. To create it (as with every executor), Java recommends the use of the Executors class. This class works as a factory of executor objects. In this case, you should use the newScheduledThreadPool() method to create a ScheduledExecutorService object. That method receives as a parameter the number of threads of the pool. As you have only one task in this example, you have passed the value 1 as a parameter.

scheduledAtFixedRate() method
Once you have the executor needed to execute a periodic task, you send the task to the executor. You have used the scheduledAtFixedRate() method. 


This method accepts four parameters
  • the task you want to execute periodically, 
  • the delay of time until the first execution of the task,
  • the period between two executions, 
  • the time unit of the second and third parameters. It's a constant of the TimeUnit class. 
The TimeUnit class is an enumeration with the following constants: DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, and SECONDS.

The method scheduleAtFixedRate() returns a ScheduledFuture object, which extends the Future interface, with methods to work with scheduled tasks. ScheduledFuture is a parameterized interface. 
In this example, as your task is a Runnable object that is not parameterized, you have to parameterize them with the ? symbol as a parameterYou have used one method of the ScheduledFuture interface. The getDelay() method returns the time until the next execution of the task. This method receives a TimeUnit constant with the time unit in which you want to receive the results.

An important point to consider is that the period between two executions is the period of time between these two executions that begins. If you have a periodic task that takes 5 sceconds to execute and you put a period of 3 seconds, you will have two instances of the task executing at a time.
You can see the task executing every 2 seconds (denoted with Task: prefix) and the delay written in the console every 500 milliseconds. That's how long the main thread has been put to sleep. When you shut down the executor, the scheduled task ends its execution and you don't see more messages in the console.

scheduleWithFixedRate() vs scheduledAtFixedRate()
ScheduledThreadPoolExecutor provides other methods to schedule periodic tasks. It is the scheduleWithFixedRate() method. It has the same parameters as the scheduledAtFixedRate() method, but there is a difference worth noticing. 
In the scheduledAtFixedRate() method, the third parameter determines the period of time between the starting of two executions. In the scheduledWithFixedRate() method, parameter determines the period of time between the end of an execution of the task and the beginning of the next execution

How setContinueExistingPeriodicTasksAfterShutdownPolicy() works...
You can also configure the behavior of an instance of the ScheduledThreadPoolExecutor class with the shutdown() method. The default behavior is that the scheduled tasks finish when you call that method. You can change this behavior using the setContinueExistingPeriodicTasksAfterShutdownPolicy() method of the ScheduledThreadPoolExecutor class with a true value. The periodic tasks won't finish upon calling the shutdown() method.

TimerTask vs ScheduledThreadPoolExecutor
The Timer facility manages the execution of deferred ("run this task in 100 ms") and periodic ("run this task every 10ms") tasks. However, Timer has some drawbacks, and ScheduledThreadPoolExecutor should be thought of as its replacement.You can construct a ScheduledThreadPoolExecutor through its constructor or through the newScheduledThreadPool factory.
  • Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor is not.
  • A Timer creates only a single thread for executing timer tasks. If a timer task takes too long to run, the timing accuracy of other TimerTasks can suffer so long-running task can delay other tasks. ScheduledThreadPoolExecutor can be configured with any number of threads. Furthermore, you have full control over created threads, if you want by providing ThreadFactory.
  • Another problem with Timer is that it behaves poorly if a TimerTask throws an unchecked exception. The Timer thread doesn't catch the exception, so an unchecked exception thrown from a TimerTask terminates the timer thread. ScheduledThreadExecutor not only catches runtime exceptions, but it lets you handle them if you want (by overriding afterExecute method from ThreadPoolExecutor). Task which threw exception will be canceled, but other tasks will continue to run.


If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...