KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ExtractorURLConnection


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  *
15  * FIXME:
16  */

17
18
19 // ExtractorURLStreamHandler.java
20

21 // Description:
22
// A class to implement a network URL protocol for a self extracting
23
// archive.
24
//
25
// Author:
26
// Peter Pilgrim
27
// Thu Mar 11 00:06:57 GMT 1999
28
//
29
// RCS HEADER
30
//
31
// $Author$
32
// $Date$
33
// $Source$
34
// $Revision$ $State$ $Locker$
35
//
36
// History
37
// ================================================================================
38
// $Log$
39

40
41 import java.io.*; // for File I/O Classes
42
import java.net.*; // Network/Internet classes
43
import java.util.*; // for Vector/Hashtable
44

45
46 /**
47  * A class to handle the communication; opening the connection to the
48  * remote or local file using <B>extractor:</B> protocol.
49  *
50  * The universal resource locator
51  *
52  * <B>protocol</B> : &lt;TYPECODE&gt; / + / <I>&lt;FILENAME&gt;</I>
53  *
54  * @author Peter Pilgrim
55  * @version 1.0 , Mon Feb 22 23:07:13 GMT 1999
56  *
57  */

58 public class ExtractorURLConnection extends URLConnection {
59     
60     protected InputStream is; // The input stream
61

62     /**
63      * construct an <code>URLCnnection</code> which can handle
64      * <B>extractor:</B> protocol.
65      */

66     public ExtractorURLConnection( URL u )
67     {
68     super(u);
69     }
70     
71 // /** Returns the content for the extractor URL*/
72
// public Object getContent()
73
// throws IOException
74
// {
75
// Object retobj = super.getContent();
76
//
77
// System.out.println("\n++++++++++ ExtractorURLConnection.getContent() retobj = "+retobj+"\n");
78
// return (retobj);
79
// }
80
//
81
// /** Returns the content length for the extractor URL */
82
// public int getContentLength()
83
// {
84
// int length = super.getContentLength();
85
//
86
// System.out.println("\n++++++++++ ExtractorURLConnection.getContentLength() length = "+length+"\n");
87
// return (length);
88
// }
89

90     /** Returns the MIME content type for the extractor associated
91      * with the URL's file.
92      */

93     public String JavaDoc getContentType()
94     {
95     String JavaDoc file = getURL().getFile();
96     String JavaDoc ct = null;
97     
98     if ( file.endsWith(".jpg") || file.endsWith(".jpeg") ||
99          file.endsWith(".jpe") || file.endsWith(".jfif") )
100         ct = "image/jpeg";
101     else if ( file.endsWith(".gif") )
102         ct = "image/gif";
103     else if ( file.endsWith(".htm") || file.endsWith(".html") )
104         ct = "text/html";
105     else if ( file.endsWith(".rtf") || file.endsWith(".rtx" ) )
106         ct = "application/rtf";
107     else if ( file.endsWith(".eps") || file.endsWith(".ps" ) )
108         ct = "application/postscript";
109     else if ( file.endsWith(".snd") || file.endsWith(".au") )
110         ct = "audio/basic";
111     else if ( file.endsWith(".aifc") || file.endsWith(".aif") ||
112           file.endsWith(".aiff") )
113         ct = "audio/x-aiff";
114     else if ( file.endsWith(".wav") || file.endsWith(".wave") )
115         ct = "audio/x-wav";
116     else if ( file.endsWith(".gz") || file.endsWith(".tgz") )
117         ct = "audio/x-gtar";
118     else if ( file.endsWith(".zip") )
119         ct = "application/zip";
120     else if ( file.endsWith(".tar") )
121         ct = "application/x-tar";
122     else if ( file.endsWith(".cpio") )
123         ct = "application/x-cpio";
124     else
125         ct = "text/plain";
126
127     System.out.println("\n++++++++++ ExtractorURLConnection.getContentType() type = "+ct+"\n");
128     return (ct);
129     }
130     
131     /**
132      * Returns an input stream that reads from this open connection.
133      *
134      * @return an input stream that reads from this open connection.
135      * @exception IOException if an I/O error occurs while
136      * creating the input stream.
137      * @exception UnknownServiceException if the protocol does not support
138      * input.
139      */

140     public synchronized InputStream getInputStream()
141     throws IOException
142     {
143     System.out.println( "ExtractorURLOpenConnection.getInputStream() connected:"+connected);
144     if (!connected) {
145         connect();
146     }
147     
148     return (is);
149     }
150
151     /**
152      * Opens a communications link to the resource referenced by this
153      * URL, if such a connection has not already been established.
154      * <p>
155      * If the <code>connect</code> method is called when the connection
156      * has already been opened (indicated by the <code>connected</code>
157      * field having the value <code>true</code>), the call is ignored.
158      * <p>
159      */

160     public synchronized void connect()
161     throws IOException
162     {
163     System.out.println( "ExtractorURLOpenConnection.connect()");
164     if (!connected) {
165         is = Extractor.getCachedResourceAsInputStream( url );
166         connected = true;
167     }
168     }
169 }
170
171 // fini
172
Popular Tags