20-CS-694 Advanced Programming Techniques Spring 2012
Networking - Content Handler

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Next     Tar.java     x_tar.java      All lectures

Application of content handler for x-tar files

This application makes use of the x-tar content handler given in the next slide. Java natively has some handlers that support processing certain kinds of content such as text and gif files. This example adds a content handler for tar files. The simple handler, presented on the next slide, lists the names of files contained in a tar file. The application on this slide makes use of that handler. Additionally, it shows how gif and text content are handled natively.

Compile Tar.java and run it like this after the handler is placed in its proper directory:

    java -Djava.content.handler.pkgs=contenthandlers Tar
and then click the "Run It" button. Use this command to see what happens if the content handler is not added:
    java Tar
import java.lang.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import contenthandlers.application.*;
import java.applet.*;

class MyFrame extends JFrame {
   Image image;

   MyFrame(Image image) {  this.image = image;  }

   public void paint(Graphics g) {
      g.drawImage(image, 0, 0, this);

class YourFrame extends JFrame {
   Image image;
   YourFrame(ImageProducer obj) {  image = createImage(obj);  }

   public void paint(Graphics g) {
      g.drawImage(image, 0, 0, this);

class TarFrame extends JFrame 
   implements URLStreamHandlerFactory, ActionListener {
   JButton doit;
   JTextArea text;
   JTextField msg;

   public TarFrame () {
      setLayout(new BorderLayout());
      add("Center", new JScrollPane(text = new JTextArea()));
      JPanel p = new JPanel();
      p.setLayout(new FlowLayout());
      p.add(new JLabel("java.content.handler.pkgs:", JLabel.RIGHT));
      p.add(msg = new JTextField(10));
      p.add(doit = new JButton("Run it"));
      add("South", p);

   public URLStreamHandler createURLStreamHandler(String scheme) { return null;}

   public void start () {
      try {
      } catch (Exception e) { 
	 text.append("Error: "+e.toString()+"\n");

   public void actionPerformed (ActionEvent evt) {  doit();  }

   public void doit () {
      try {
         URL url = new URL("http://gauss.ececs.uc.edu/index.html");
	 text.append("Hitting: http://gauss.ececs.uc.edu/index.html\n");
         InputStream is = (InputStream)url.getContent();
         text.append("  "+is.toString()+"\n");
         InputStreamReader isr = new InputStreamReader(is);
         BufferedReader bis = new BufferedReader(isr);
         try {
            String line;
            while ((line = bis.readLine()) != null) text.append("  "+line+"\n");
         } catch (Exception e) { }

         url = new URL("http://gauss.ececs.uc.edu/franco_files/gif/portrait.gif");
	 text.append("\nHitting: http://gauss.ececs.uc.edu/franco_files/gif/portrait.gif\n");
         Image image = Toolkit.getDefaultToolkit().getImage(url);
         MyFrame frame = new MyFrame(image);
         YourFrame frame2 = new YourFrame((ImageProducer)url.getContent());


         url = new URL("http://gauss.ececs.uc.edu/test.tar");
	 text.append("\nHitting: http://gauss.ececs.uc.edu/test.tar\n");
         Object tarfile = (String)url.getContent();
      catch (Exception e) {
         text.append("Exception: "+e.toString()+"\n");

public class Tar {
   public static void main (String args[]) {
      TarFrame tf = new TarFrame();