Listing 3: xyURLConnection.java
package sun.net.www.protocol.xy;

import java.net.*;
import java.io.*;
import java.util.StringTokenizer;

class xyURLConnection extends URLConnection{ 

static final int HTTPport=80; 	 //port: 80
static String[] headerfields=null;	 //header content
String file;                 		 //resource 
String host;                  		 //server host

Socket socluURL=null;
    
xyURLConnection(URL url,String mn)throws IOException //the mn argument is not used
      { 
      super(url);
      setDoOutput(true);
      file=url.getFile();
      host=url.getHost(); 
      } 
 
synchronized public void connect()throws IOException
     {  
     int port;      
     if((port=url.getPort())==-1)port=HTTPport; 
 
//open the socket                 
     socluURL=new Socket(host,port); 
 
//opening the I/O streams
     PrintWriter out=new PrintWriter(socluURL.getOutputStream(),true);
     InputStreamReader ISR=new InputStreamReader(socluURL.getInputStream());
     BufferedReader in=new BufferedReader(ISR);   

//sending a request to the server - as you can see is just a simple HTTP GET command 
     out.println( "GET "+file+" HTTP/1.1"); 
     out.println("\r\n");
       
     String answer=""; 
     headerfields=new String[3];
     int f=0;
              
     //getting the header from the server
     //this header is specific to the HTTPServer.java and it has just three lines                  
     while((answer=in.readLine())!=null && !answer.equals(""))
           {
           //you keep the three header lines into a string array
           //eventually, those lines can be used for different scopes (example: find out 
           //what is the resource length or what is the MIME type of the resource) 
           System.out.println(answer); 
           headerfields[f]=answer;
           f++;          
           }                                 
          //setting the URLConnection.connected=true
          connected=true;  
     } 

//overriding the getInputStream method
//this method is called by the getContent method from the content handler class
//also is important to know that this method returns only "clean" information - without header
synchronized public InputStream getInputStream() throws IOException
 {
 if(connected==false)connect(); 
 InputStream IS =socluURL.getInputStream();

 return IS;
 }

//overriding the getContent method
//this method reconigze only the content handler that have the xy/image MIME type (see part one of this article)
public Object getContent()throws IOException
   {  
   String contentType=getContentType();
   if(contentType.equals("image/xy"))
   {
   sun.net.www.content.image.xy contenthandler=new sun.net.www.content.image.xy();
   java.awt.image.MemoryImageSource t=(java.awt.image.MemoryImageSource)contenthandler.getContent(this);                  

   return t;   
   }else
        return null;
   }

   //overriding the getContentType method
   //this method recognize only the .xy files by "looking" at their extensions
   //another solution is to extract the MIME type from the header
   public String getContentType()
        {               
        StringTokenizer ST=new StringTokenizer(file,".");
        ST.nextToken();
        if(ST.nextToken().equals("xy"))return "image/xy";
            else return null;      
        }
        
   //overriding the getContentLength method
   public int getContentLength()
       {
       StringTokenizer ST=new StringTokenizer(headerfields[2],":");
       ST.nextToken();
        
       return Integer.parseInt(ST.nextToken().trim());       
       }       
      
//other methods
//...          
} 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.