jdk/src/share/classes/java/net/URLConnection.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 83 34040312032d
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The abstract class <code>URLConnection</code> is the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * of all classes that represent a communications link between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * application and a URL. Instances of this class can be used both to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * read from and to write to the resource referenced by the URL. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * general, creating a connection to a URL is a multistep process:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <center><table border=2 summary="Describes the process of creating a connection to a URL: openConnection() and connect() over time.">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <tr><th><code>openConnection()</code></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     <th><code>connect()</code></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <tr><td>Manipulate parameters that affect the connection to the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         resource.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     <td>Interact with the resource; query header fields and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *         contents.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * ----------------------------&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <br>time</center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li>The connection object is created by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     <code>openConnection</code> method on a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <li>The setup parameters and general request properties are manipulated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <li>The actual connection to the remote object is made, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *    <code>connect</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li>The remote object becomes available. The header fields and the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     of the remote object can be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The setup parameters are modified using the following methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   <li><code>setAllowUserInteraction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   <li><code>setDoInput</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   <li><code>setDoOutput</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   <li><code>setIfModifiedSince</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   <li><code>setUseCaches</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * and the general request properties are modified using the method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   <li><code>setRequestProperty</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Default values for the <code>AllowUserInteraction</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>UseCaches</code> parameters can be set using the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <code>setDefaultAllowUserInteraction</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>setDefaultUseCaches</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * Each of the above <code>set</code> methods has a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <code>get</code> method to retrieve the value of the parameter or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * general request property. The specific parameters and general
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * request properties that are applicable are protocol specific.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * The following methods are used to access the header fields and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * the contents after the connection is made to the remote object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   <li><code>getContent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   <li><code>getHeaderField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   <li><code>getInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <li><code>getOutputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Certain header fields are accessed frequently. The methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   <li><code>getContentEncoding</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   <li><code>getContentLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   <li><code>getContentType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   <li><code>getDate</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   <li><code>getExpiration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *   <li><code>getLastModifed</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * provide convenient access to these fields. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <code>getContentType</code> method is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <code>getContent</code> method to determine the type of the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * object; subclasses may find it convenient to override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <code>getContentType</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * In the common case, all of the pre-connection parameters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * general request properties can be ignored: the pre-connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * parameters and request properties default to sensible values. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * most clients of this interface, there are only two interesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * methods: <code>getInputStream</code> and <code>getContent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * which are mirrored in the <code>URL</code> class by convenience methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * More information on the request properties and header fields of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * an <code>http</code> connection can be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <a href="http://www.ietf.org/rfc/rfc2616.txt">http://www.ietf.org/rfc/rfc2616.txt</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Note about <code>fileNameMap</code>: In versions prior to JDK 1.1.6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * field <code>fileNameMap</code> of <code>URLConnection</code> was public.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * In JDK 1.1.6 and later, <code>fileNameMap</code> is private; accessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * and mutator methods {@link #getFileNameMap() getFileNameMap} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * {@link #setFileNameMap(java.net.FileNameMap) setFileNameMap} are added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * to access it.  This change is also described on the <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * "http://java.sun.com/products/jdk/1.2/compatibility.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Compatibility</a> page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * Invoking the <tt>close()</tt> methods on the <tt>InputStream</tt> or <tt>OutputStream</tt> of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <tt>URLConnection</tt> after a request may free network resources associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * instance, unless particular protocol specifications specify different behaviours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * @see     java.net.URL#openConnection()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * @see     java.net.URLConnection#connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * @see     java.net.URLConnection#getContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * @see     java.net.URLConnection#getContentEncoding()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * @see     java.net.URLConnection#getContentLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * @see     java.net.URLConnection#getContentType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * @see     java.net.URLConnection#getDate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * @see     java.net.URLConnection#getExpiration()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * @see     java.net.URLConnection#getHeaderField(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * @see     java.net.URLConnection#getInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * @see     java.net.URLConnection#getLastModified()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @see     java.net.URLConnection#getOutputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @see     java.net.URLConnection#setAllowUserInteraction(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @see     java.net.URLConnection#setDefaultUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * @see     java.net.URLConnection#setDoInput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @see     java.net.URLConnection#setDoOutput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @see     java.net.URLConnection#setIfModifiedSince(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @see     java.net.URLConnection#setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * @see     java.net.URLConnection#setUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
public abstract class URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * The URL represents the remote object on the World Wide Web to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * which this connection is opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * The value of this field can be accessed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>getURL</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The default value of this variable is the value of the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * argument in the <code>URLConnection</code> constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see     java.net.URLConnection#getURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see     java.net.URLConnection#url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    protected URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * This variable is set by the <code>setDoInput</code> method. Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * value is returned by the <code>getDoInput</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * A URL connection can be used for input and/or output. Setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>doInput</code> flag to <code>true</code> indicates that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * the application intends to read data from the URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * The default value of this field is <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see     java.net.URLConnection#getDoInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see     java.net.URLConnection#setDoInput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected boolean doInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This variable is set by the <code>setDoOutput</code> method. Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * value is returned by the <code>getDoOutput</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * A URL connection can be used for input and/or output. Setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>doOutput</code> flag to <code>true</code> indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * that the application intends to write data to the URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The default value of this field is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see     java.net.URLConnection#getDoOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see     java.net.URLConnection#setDoOutput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    protected boolean doOutput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static boolean defaultAllowUserInteraction = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * If <code>true</code>, this <code>URL</code> is being examined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * a context in which it makes sense to allow user interactions such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * as popping up an authentication dialog. If <code>false</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * then no user interaction is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * The value of this field can be set by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>setAllowUserInteraction</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Its value is returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <code>getAllowUserInteraction</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Its default value is the value of the argument in the last invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * of the <code>setDefaultAllowUserInteraction</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see     java.net.URLConnection#getAllowUserInteraction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see     java.net.URLConnection#setAllowUserInteraction(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see     java.net.URLConnection#setDefaultAllowUserInteraction(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    protected boolean allowUserInteraction = defaultAllowUserInteraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private static boolean defaultUseCaches = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * If <code>true</code>, the protocol is allowed to use caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * whenever it can. If <code>false</code>, the protocol must always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * try to get a fresh copy of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * This field is set by the <code>setUseCaches</code> method. Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * value is returned by the <code>getUseCaches</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Its default value is the value given in the last invocation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <code>setDefaultUseCaches</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see     java.net.URLConnection#setUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see     java.net.URLConnection#getUseCaches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see     java.net.URLConnection#setDefaultUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    protected boolean useCaches = defaultUseCaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Some protocols support skipping the fetching of the object unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the object has been modified more recently than a certain time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * A nonzero value gives a time as the number of milliseconds since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * January 1, 1970, GMT. The object is fetched only if it has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * modified more recently than that time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * This variable is set by the <code>setIfModifiedSince</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * method. Its value is returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <code>getIfModifiedSince</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The default value of this field is <code>0</code>, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * that the fetching must always occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see     java.net.URLConnection#getIfModifiedSince()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see     java.net.URLConnection#setIfModifiedSince(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    protected long ifModifiedSince = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * If <code>false</code>, this connection object has not created a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * communications link to the specified URL. If <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * the communications link has been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    protected boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private int connectTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private int readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private MessageHeader requests;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static FileNameMap fileNameMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since 1.2.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static boolean fileNameMapLoaded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Loads filename map (a mimetable) from a data file. It will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * first try to load the user-specific table, defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * by &quot;content.types.user.table&quot; property. If that fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * it tries to load the default built-in table at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * lib/content-types.properties under java home.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return the FileNameMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see #setFileNameMap(java.net.FileNameMap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public static synchronized FileNameMap getFileNameMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if ((fileNameMap == null) && !fileNameMapLoaded) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            fileNameMap = sun.net.www.MimeTable.loadTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            fileNameMapLoaded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return new FileNameMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            private FileNameMap map = fileNameMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            public String getContentTypeFor(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                return map.getContentTypeFor(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Sets the FileNameMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param map the FileNameMap to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *             <code>checkSetFactory</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @see #getFileNameMap()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public static void setFileNameMap(FileNameMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (sm != null) sm.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        fileNameMap = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Opens a communications link to the resource referenced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * URL, if such a connection has not already been established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * If the <code>connect</code> method is called when the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * has already been opened (indicated by the <code>connected</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * field having the value <code>true</code>), the call is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * URLConnection objects go through two phases: first they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * created, then they are connected.  After being created, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * before being connected, various options can be specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * (e.g., doInput and UseCaches).  After connecting, it is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * error to try to set them.  Operations that depend on being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * connected, like getContentLength, will implicitly perform the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * connection, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws SocketTimeoutException if the timeout expires before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *               the connection can be established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @exception  IOException  if an I/O error occurs while opening the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *               connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see java.net.URLConnection#connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see #getConnectTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see #setConnectTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    abstract public void connect() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Sets a specified timeout value, in milliseconds, to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * when opening a communications link to the resource referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * by this URLConnection.  If the timeout expires before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * connection can be established, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * java.net.SocketTimeoutException is raised. A timeout of zero is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p> Some non-standard implmentation of this method may ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * the specified timeout. To see the connect timeout set, please
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * call getConnectTimeout().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param timeout an <code>int</code> that specifies the connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *               timeout value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws IllegalArgumentException if the timeout parameter is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @see #getConnectTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see #connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (timeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new IllegalArgumentException("timeout can not be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns setting for connect timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * 0 return implies that the option is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return an <code>int</code> that indicates the connect timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #setConnectTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see #connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        return connectTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Sets the read timeout to a specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * milliseconds. A non-zero value specifies the timeout when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * reading from Input stream when a connection is established to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * resource. If the timeout expires before there is data available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * for read, a java.net.SocketTimeoutException is raised. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *<p> Some non-standard implementation of this method ignores the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * specified timeout. To see the read timeout set, please call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * getReadTimeout().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param timeout an <code>int</code> that specifies the timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * value to be used in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @throws IllegalArgumentException if the timeout parameter is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @see #getReadTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @see InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (timeout < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throw new IllegalArgumentException("timeout can not be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns setting for read timeout. 0 return implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return an <code>int</code> that indicates the read timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         value in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see #setReadTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Constructs a URL connection to the specified URL. A connection to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * the object referenced by the URL is not created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param   url   the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    protected URLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns the value of this <code>URLConnection</code>'s <code>URL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return  the value of this <code>URLConnection</code>'s <code>URL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *          field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see     java.net.URLConnection#url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public URL getURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Returns the value of the <code>content-length</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <B>Note</B>: {@link #getContentLengthLong() getContentLengthLong()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * should be preferred over this method, since it returns a {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * instead and is therefore more portable.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return  the content length of the resource that this connection's URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *          references, {@code -1} if the content length is not known,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *          or if the content length is greater than Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public int getContentLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        long l = getContentLengthLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (l > Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return (int) l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * Returns the value of the <code>content-length</code> header field as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @return  the content length of the resource that this connection's URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *          references, or <code>-1</code> if the content length is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *          not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @since 7.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public long getContentLengthLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return getHeaderFieldLong("content-length", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns the value of the <code>content-type</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @return  the content type of the resource that the URL references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *          or <code>null</code> if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public String getContentType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return getHeaderField("content-type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Returns the value of the <code>content-encoding</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @return  the content encoding of the resource that the URL references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *          or <code>null</code> if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public String getContentEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return getHeaderField("content-encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Returns the value of the <code>expires</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @return  the expiration date of the resource that this URL references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          or 0 if not known. The value is the number of milliseconds since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *          January 1, 1970 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public long getExpiration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return getHeaderFieldDate("expires", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Returns the value of the <code>date</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return  the sending date of the resource that the URL references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *          or <code>0</code> if not known. The value returned is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *          number of milliseconds since January 1, 1970 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public long getDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return getHeaderFieldDate("date", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Returns the value of the <code>last-modified</code> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * The result is the number of milliseconds since January 1, 1970 GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return  the date the resource referenced by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *          <code>URLConnection</code> was last modified, or 0 if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @see     java.net.URLConnection#getHeaderField(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public long getLastModified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return getHeaderFieldDate("last-modified", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Returns the value of the named header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * If called on a connection that sets the same header multiple times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * with possibly different values, only the last value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param   name   the name of a header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @return  the value of the named header field, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *          if there is no such field in the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public String getHeaderField(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Returns an unmodifiable Map of the header fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * The Map keys are Strings that represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * response-header field names. Each Map value is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * unmodifiable List of Strings that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * the corresponding field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return a Map of header fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    public Map<String,List<String>> getHeaderFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return Collections.EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * Returns the value of the named field parsed as a number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * This form of <code>getHeaderField</code> exists because some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * connection types (e.g., <code>http-ng</code>) have pre-parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * headers. Classes for that connection type can override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * and short-circuit the parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param   name      the name of the header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param   Default   the default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return  the value of the named field, parsed as an integer. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *          <code>Default</code> value is returned if the field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *          missing or malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public int getHeaderFieldInt(String name, int Default) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        String value = getHeaderField(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Returns the value of the named field parsed as a number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * This form of <code>getHeaderField</code> exists because some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * connection types (e.g., <code>http-ng</code>) have pre-parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * headers. Classes for that connection type can override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * and short-circuit the parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param   name      the name of the header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param   Default   the default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @return  the value of the named field, parsed as a long. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *          <code>Default</code> value is returned if the field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *          missing or malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @since 7.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public long getHeaderFieldLong(String name, long Default) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        String value = getHeaderField(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return Long.parseLong(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Returns the value of the named field parsed as date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * The result is the number of milliseconds since January 1, 1970 GMT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * represented by the named field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * This form of <code>getHeaderField</code> exists because some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * connection types (e.g., <code>http-ng</code>) have pre-parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * headers. Classes for that connection type can override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * and short-circuit the parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @param   name     the name of the header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @param   Default   a default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @return  the value of the field, parsed as a date. The value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *          <code>Default</code> argument is returned if the field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *          missing or malformed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    public long getHeaderFieldDate(String name, long Default) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        String value = getHeaderField(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return Date.parse(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        } catch (Exception e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return Default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Returns the key for the <code>n</code><sup>th</sup> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * It returns <code>null</code> if there are fewer than <code>n+1</code> fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param   n   an index, where n>=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return  the key for the <code>n</code><sup>th</sup> header field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *          or <code>null</code> if there are fewer than <code>n+1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *          fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public String getHeaderFieldKey(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns the value for the <code>n</code><sup>th</sup> header field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * It returns <code>null</code> if there are fewer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <code>n+1</code>fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * This method can be used in conjunction with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * {@link #getHeaderFieldKey(int) getHeaderFieldKey} method to iterate through all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * the headers in the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param   n   an index, where n>=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @return  the value of the <code>n</code><sup>th</sup> header field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *          or <code>null</code> if there are fewer than <code>n+1</code> fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @see     java.net.URLConnection#getHeaderFieldKey(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public String getHeaderField(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Retrieves the contents of this URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * This method first determines the content type of the object by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * calling the <code>getContentType</code> method. If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * the first time that the application has seen that specific content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * type, a content handler for that content type is created:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <li>If the application has set up a content handler factory instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *     using the <code>setContentHandlerFactory</code> method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *     <code>createContentHandler</code> method of that instance is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *     with the content type as an argument; the result is a content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *     handler for that content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <li>If no content handler factory has yet been set up, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *     factory's <code>createContentHandler</code> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *     <code>null</code>, then the application loads the class named:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *     <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *         sun.net.www.content.&lt;<i>contentType</i>&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *     </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *     where &lt;<i>contentType</i>&gt; is formed by taking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *     content-type string, replacing all slash characters with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *     <code>period</code> ('.'), and all other non-alphanumeric characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *     with the underscore character '<code>_</code>'. The alphanumeric
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *     characters are specifically the 26 uppercase ASCII letters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *     '<code>A</code>' through '<code>Z</code>', the 26 lowercase ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *     letters '<code>a</code>' through '<code>z</code>', and the 10 ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *     digits '<code>0</code>' through '<code>9</code>'. If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *     class does not exist, or is not a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *     <code>ContentHandler</code>, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *     <code>UnknownServiceException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @return     the object fetched. The <code>instanceof</code> operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *               should be used to determine the specific kind of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *               returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @exception  IOException              if an I/O error occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *               getting the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @exception  UnknownServiceException  if the protocol does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *               the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @see        java.net.ContentHandlerFactory#createContentHandler(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @see        java.net.URLConnection#getContentType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @see        java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    public Object getContent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // Must call getInputStream before GetHeaderField gets called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // so that FileNotFoundException has a chance to be thrown up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // from here without being caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return getContentHandler().getContent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Retrieves the contents of this URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @param classes the <code>Class</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * indicating the requested types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return     the object fetched that is the first match of the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *               specified in the classes array. null if none of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *               the requested types are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *               The <code>instanceof</code> operator should be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *               determine the specific kind of object returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @exception  IOException              if an I/O error occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *               getting the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @exception  UnknownServiceException  if the protocol does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *               the content type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @see        java.net.URLConnection#getContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @see        java.net.ContentHandlerFactory#createContentHandler(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @see        java.net.URLConnection#getContent(java.lang.Class[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @see        java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    public Object getContent(Class[] classes) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        // Must call getInputStream before GetHeaderField gets called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // so that FileNotFoundException has a chance to be thrown up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // from here without being caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return getContentHandler().getContent(this, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Returns a permission object representing the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * necessary to make the connection represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * object. This method returns null if no permission is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * required to make the connection. By default, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * returns <code>java.security.AllPermission</code>. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * should override this method and return the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * that best represents the permission required to make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * a connection to the URL. For example, a <code>URLConnection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * representing a <code>file:</code> URL would return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * <code>java.io.FilePermission</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <p>The permission returned may dependent upon the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * connection. For example, the permission before connecting may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * different from that after connecting. For example, an HTTP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * sever, say foo.com, may redirect the connection to a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * host, say bar.com. Before connecting the permission returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * the connection will represent the permission needed to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * to foo.com, while the permission returned after connecting will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * be to bar.com.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <p>Permissions are generally used for two purposes: to protect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * caches of objects obtained through URLConnections, and to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * the right of a recipient to learn about a particular URL. In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * the first case, the permission should be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <em>after</em> the object has been obtained. For example, in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * HTTP connection, this will represent the permission to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * to the host from which the data was ultimately fetched. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * second case, the permission should be obtained and tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <em>before</em> connecting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @return the permission object representing the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * necessary to make the connection represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * URLConnection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @exception IOException if the computation of the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * requires network or file I/O and an exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * computing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    public Permission getPermission() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return SecurityConstants.ALL_PERMISSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * Returns an input stream that reads from this open connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * A SocketTimeoutException can be thrown when reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * returned input stream if the read timeout expires before data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * is available for read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return     an input stream that reads from this open connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * @exception  IOException              if an I/O error occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *               creating the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @exception  UnknownServiceException  if the protocol does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *               input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @see #setReadTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @see #getReadTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        throw new UnknownServiceException("protocol doesn't support input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Returns an output stream that writes to this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @return     an output stream that writes to this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @exception  IOException              if an I/O error occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *               creating the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @exception  UnknownServiceException  if the protocol does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *               output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        throw new UnknownServiceException("protocol doesn't support output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * Returns a <code>String</code> representation of this URL connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @return  a string representation of this <code>URLConnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        return this.getClass().getName() + ":" + url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Sets the value of the <code>doInput</code> field for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <code>URLConnection</code> to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * A URL connection can be used for input and/or output.  Set the DoInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * flag to true if you intend to use the URL connection for input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * false if not.  The default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param   doinput   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * @see     java.net.URLConnection#doInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @see #getDoInput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    public void setDoInput(boolean doinput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        doInput = doinput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Returns the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <code>doInput</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @return  the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *          <code>doInput</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @see     #setDoInput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    public boolean getDoInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        return doInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * Sets the value of the <code>doOutput</code> field for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <code>URLConnection</code> to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * A URL connection can be used for input and/or output.  Set the DoOutput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * flag to true if you intend to use the URL connection for output,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * false if not.  The default is false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @param   dooutput   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @see #getDoOutput()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public void setDoOutput(boolean dooutput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        doOutput = dooutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * <code>doOutput</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @return  the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *          <code>doOutput</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * @see     #setDoOutput(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public boolean getDoOutput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return doOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Set the value of the <code>allowUserInteraction</code> field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * this <code>URLConnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @param   allowuserinteraction   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @see     #getAllowUserInteraction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    public void setAllowUserInteraction(boolean allowuserinteraction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        allowUserInteraction = allowuserinteraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Returns the value of the <code>allowUserInteraction</code> field for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @return  the value of the <code>allowUserInteraction</code> field for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *          this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @see     #setAllowUserInteraction(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public boolean getAllowUserInteraction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        return allowUserInteraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Sets the default value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * <code>allowUserInteraction</code> field for all future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * <code>URLConnection</code> objects to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @param   defaultallowuserinteraction   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @see     #getDefaultAllowUserInteraction()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        defaultAllowUserInteraction = defaultallowuserinteraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Returns the default value of the <code>allowUserInteraction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Ths default is "sticky", being a part of the static state of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * URLConnections.  This flag applies to the next, and all following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * URLConnections that are created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @return  the default value of the <code>allowUserInteraction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *          field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @see     #setDefaultAllowUserInteraction(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    public static boolean getDefaultAllowUserInteraction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return defaultAllowUserInteraction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Sets the value of the <code>useCaches</code> field of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <code>URLConnection</code> to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Some protocols do caching of documents.  Occasionally, it is important
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * to be able to "tunnel through" and ignore the caches (e.g., the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * "reload" button in a browser).  If the UseCaches flag on a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * is true, the connection is allowed to use whatever caches it can.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *  If false, caches are to be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *  The default value comes from DefaultUseCaches, which defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param usecaches a <code>boolean</code> indicating whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * or not to allow caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @see #getUseCaches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public void setUseCaches(boolean usecaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        useCaches = usecaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Returns the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * <code>useCaches</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return  the value of this <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *          <code>useCaches</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @see #setUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public boolean getUseCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return useCaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Sets the value of the <code>ifModifiedSince</code> field of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * this <code>URLConnection</code> to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @param   ifmodifiedsince   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @see     #getIfModifiedSince()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    public void setIfModifiedSince(long ifmodifiedsince) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        ifModifiedSince = ifmodifiedsince;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * Returns the value of this object's <code>ifModifiedSince</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @return  the value of this object's <code>ifModifiedSince</code> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @see #setIfModifiedSince(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public long getIfModifiedSince() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        return ifModifiedSince;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Returns the default value of a <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <code>useCaches</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Ths default is "sticky", being a part of the static state of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * URLConnections.  This flag applies to the next, and all following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * URLConnections that are created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return  the default value of a <code>URLConnection</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *          <code>useCaches</code> flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @see     #setDefaultUseCaches(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    public boolean getDefaultUseCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return defaultUseCaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Sets the default value of the <code>useCaches</code> field to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @param   defaultusecaches   the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @see     #getDefaultUseCaches()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    public void setDefaultUseCaches(boolean defaultusecaches) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        defaultUseCaches = defaultusecaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Sets the general request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * <p> NOTE: HTTP requires all request properties which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * legally have multiple instances with the same key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * to use a comma-seperated list syntax which enables multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * properties to be appended into a single property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param   value   the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @throws NullPointerException if key is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @see #getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    public void setRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (requests == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        requests.set(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * Adds a general request property specified by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * key-value pair.  This method will not overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * existing values associated with the same key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @param   value  the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * @throws NullPointerException if key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @see #getRequestProperties()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    public void addRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            throw new NullPointerException ("key is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        if (requests == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            requests = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        requests.add(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Returns the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @return  the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *           connection. If key is null, then null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * @see #setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public String getRequestProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        if (requests == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        return requests.findValue(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * Returns an unmodifiable Map of general request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * properties for this connection. The Map keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * are Strings that represent the request-header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * field names. Each Map value is a unmodifiable List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * of Strings that represents the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @return  a Map of the general request properties for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    public Map<String,List<String>> getRequestProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if (connected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            throw new IllegalStateException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        if (requests == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return Collections.EMPTY_MAP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return requests.getHeaders(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Sets the default value of a general request property. When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <code>URLConnection</code> is created, it is initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * these properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @param   value   the value associated with the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @see java.net.URLConnection#setRequestProperty(java.lang.String,java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @deprecated The instance specific setRequestProperty method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * should be used after an appropriate instance of URLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * is obtained. Invoking this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * @see #getDefaultRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    public static void setDefaultRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * Returns the value of the default request property. Default request
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * properties are set for every connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @return  the value of the default request property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @see java.net.URLConnection#getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @deprecated The instance specific getRequestProperty method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * should be used after an appropriate instance of URLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * is obtained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @see #setDefaultRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    public static String getDefaultRequestProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * The ContentHandler factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    static ContentHandlerFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Sets the <code>ContentHandlerFactory</code> of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * application. It can be called at most once by an application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * The <code>ContentHandlerFactory</code> instance is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * construct a content handler from a content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @exception  Error  if the factory has already been defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *             <code>checkSetFactory</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @see        java.net.ContentHandlerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @see        java.net.URLConnection#getContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @see        SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            throw new Error("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    private static Hashtable handlers = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    private static final ContentHandler UnknownContentHandlerP = new UnknownContentHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * Gets the Content Handler appropriate for this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @param connection the connection to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    synchronized ContentHandler getContentHandler()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    throws UnknownServiceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        String contentType = stripOffParameters(getContentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        ContentHandler handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        if (contentType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            throw new UnknownServiceException("no content-type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            handler = (ContentHandler) handlers.get(contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (handler != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        if (factory != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            handler = factory.createContentHandler(contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                handler = lookupContentHandlerClassFor(contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                handler = UnknownContentHandlerP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            handlers.put(contentType, handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * Media types are in the format: type/subtype*(; parameter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * For looking up the content handler, we should ignore those
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    private String stripOffParameters(String contentType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        if (contentType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        int index = contentType.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (index > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            return contentType.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            return contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    private static final String contentClassPrefix = "sun.net.www.content";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    private static final String contentPathProp = "java.content.handler.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * Looks for a content handler in a user-defineable set of places.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * By default it looks in sun.net.www.content, but users can define a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * vertical-bar delimited set of class prefixes to search through in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * addition by defining the java.content.handler.pkgs property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * The class name must be of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     *     {package-prefix}.{major}.{minor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     *     YoyoDyne.experimental.text.plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    private ContentHandler lookupContentHandlerClassFor(String contentType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        String contentHandlerClassName = typeToPackageName(contentType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        String contentHandlerPkgPrefixes =getContentHandlerPkgPrefixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        StringTokenizer packagePrefixIter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            new StringTokenizer(contentHandlerPkgPrefixes, "|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        while (packagePrefixIter.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            String packagePrefix = packagePrefixIter.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                String clsName = packagePrefix + "." + contentHandlerClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                Class cls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    cls = Class.forName(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    ClassLoader cl = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                        cls = cl.loadClass(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                if (cls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    ContentHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                        (ContentHandler)cls.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        return UnknownContentHandlerP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * Utility function to map a MIME content type into an equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * pair of class name components.  For example: "text/html" would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * be returned as "text.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    private String typeToPackageName(String contentType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        // make sure we canonicalize the class name: all lower case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        contentType = contentType.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        int len = contentType.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        char nm[] = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        contentType.getChars(0, len, nm, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            char c = nm[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            if (c == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                nm[i] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            } else if (!('A' <= c && c <= 'Z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                       'a' <= c && c <= 'z' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                       '0' <= c && c <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                nm[i] = '_';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        return new String(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Returns a vertical bar separated list of package prefixes for potential
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * content handlers.  Tries to get the java.content.handler.pkgs property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * to use as a set of package prefixes to search.  Whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * that property has been defined, the sun.net.www.content is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * the last one on the returned package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    private String getContentHandlerPkgPrefixes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        String packagePrefixList = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            new sun.security.action.GetPropertyAction(contentPathProp, ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        if (packagePrefixList != "") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            packagePrefixList += "|";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        return packagePrefixList + contentClassPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * Tries to determine the content type of an object, based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * on the specified "file" component of a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * This is a convenience method that can be used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * subclasses that override the <code>getContentType</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @param   fname   a filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @return  a guess as to what the content type of the object is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *          based upon its file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * @see     java.net.URLConnection#getContentType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    public static String guessContentTypeFromName(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        return getFileNameMap().getContentTypeFor(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Tries to determine the type of an input stream based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * characters at the beginning of the input stream. This method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * be used by subclasses that override the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * <code>getContentType</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * Ideally, this routine would not be needed. But many
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * <code>http</code> servers return the incorrect content type; in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * addition, there are many nonstandard extensions. Direct inspection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * of the bytes to determine the content type is often more accurate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * than believing the content type claimed by the <code>http</code> server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @param      is   an input stream that supports marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * @return     a guess at the content type, or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *             can be determined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * @exception  IOException  if an I/O error occurs while reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *               input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * @see        java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * @see        java.io.InputStream#markSupported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * @see        java.net.URLConnection#getContentType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    static public String guessContentTypeFromStream(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // If we can't read ahead safely, just give up on guessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        if (!is.markSupported())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        is.mark(12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        int c1 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        int c2 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        int c3 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        int c4 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        int c5 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        int c6 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        int c7 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        int c8 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        int c9 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        int c10 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        int c11 = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        if (c1 == 0xCA && c2 == 0xFE && c3 == 0xBA && c4 == 0xBE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            return "application/java-vm";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        if (c1 == 0xAC && c2 == 0xED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // next two bytes are version number, currently 0x00 0x05
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            return "application/x-java-serialized-object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (c1 == '<') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            if (c2 == '!'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                || ((c2 == 'h' && (c3 == 't' && c4 == 'm' && c5 == 'l' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                                   c3 == 'e' && c4 == 'a' && c5 == 'd') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                (c2 == 'b' && c3 == 'o' && c4 == 'd' && c5 == 'y'))) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                ((c2 == 'H' && (c3 == 'T' && c4 == 'M' && c5 == 'L' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                                c3 == 'E' && c4 == 'A' && c5 == 'D') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                (c2 == 'B' && c3 == 'O' && c4 == 'D' && c5 == 'Y')))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                return "text/html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            if (c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l' && c6 == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                return "application/xml";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        // big and little endian UTF-16 encodings, with byte order mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        if (c1 == 0xfe && c2 == 0xff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            if (c3 == 0 && c4 == '<' && c5 == 0 && c6 == '?' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                c7 == 0 && c8 == 'x') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                return "application/xml";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        if (c1 == 0xff && c2 == 0xfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            if (c3 == '<' && c4 == 0 && c5 == '?' && c6 == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                c7 == 'x' && c8 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                return "application/xml";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        if (c1 == 'G' && c2 == 'I' && c3 == 'F' && c4 == '8') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            return "image/gif";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (c1 == '#' && c2 == 'd' && c3 == 'e' && c4 == 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            return "image/x-bitmap";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        if (c1 == '!' && c2 == ' ' && c3 == 'X' && c4 == 'P' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                        c5 == 'M' && c6 == '2') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            return "image/x-pixmap";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        if (c1 == 137 && c2 == 80 && c3 == 78 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                c4 == 71 && c5 == 13 && c6 == 10 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                c7 == 26 && c8 == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            return "image/png";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        if (c1 == 0xFF && c2 == 0xD8 && c3 == 0xFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            if (c4 == 0xE0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                return "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
             * File format used by digital cameras to store images.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
             * Exif Format can be read by any application supporting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
             * JPEG. Exif Spec can be found at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
             * http://www.pima.net/standards/it10/PIMA15740/Exif_2-1.PDF
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            if ((c4 == 0xE1) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                (c7 == 'E' && c8 == 'x' && c9 == 'i' && c10 =='f' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                 c11 == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                return "image/jpeg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            if (c4 == 0xEE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                return "image/jpg";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        if (c1 == 0xD0 && c2 == 0xCF && c3 == 0x11 && c4 == 0xE0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            c5 == 0xA1 && c6 == 0xB1 && c7 == 0x1A && c8 == 0xE1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            /* Above is signature of Microsoft Structured Storage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
             * Below this, could have tests for various SS entities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
             * For now, just test for FlashPix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            if (checkfpx(is)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                return "image/vnd.fpx";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        if (c1 == 0x2E && c2 == 0x73 && c3 == 0x6E && c4 == 0x64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            return "audio/basic";  // .au format, big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        if (c1 == 0x64 && c2 == 0x6E && c3 == 0x73 && c4 == 0x2E) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            return "audio/basic";  // .au format, little endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        if (c1 == 'R' && c2 == 'I' && c3 == 'F' && c4 == 'F') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            /* I don't know if this is official but evidence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
             * suggests that .wav files start with "RIFF" - brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            return "audio/x-wav";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * Check for FlashPix image data in InputStream is.  Return true if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * the stream has FlashPix data, false otherwise.  Before calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * method, the stream should have already been checked to be sure it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * contains Microsoft Structured Storage data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    static private boolean checkfpx(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        /* Test for FlashPix image data in Microsoft Structured Storage format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
         * In general, should do this with calls to an SS implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * Lacking that, need to dig via offsets to get to the FlashPix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         * ClassID.  Details:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * Offset to Fpx ClsID from beginning of stream should be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
         * FpxClsidOffset = rootEntryOffset + clsidOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
         * where: clsidOffset = 0x50.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
         *        rootEntryOffset = headerSize + sectorSize*sectDirStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
         *                          + 128*rootEntryDirectory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         *        where:  headerSize = 0x200 (always)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         *                sectorSize = 2 raised to power of uSectorShift,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         *                             which is found in the header at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         *                             offset 0x1E.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         *                sectDirStart = found in the header at offset 0x30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         *                rootEntryDirectory = in general, should search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         *                                     directory labelled as root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
         *                                     We will assume value of 0 (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
         *                                     rootEntry is in first directory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        // Mark the stream so we can reset it. 0x100 is enough for the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        // few reads, but the mark will have to be reset and set again once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // the offset to the root directory entry is computed. That offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        // can be very large and isn't know until the stream has been read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        is.mark(0x100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        // Get the byte ordering located at 0x1E. 0xFE is Intel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        // 0xFF is other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        long toSkip = (long)0x1C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        long posn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        if ((posn = skipForward(is, toSkip)) < toSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
          is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        int c[] = new int[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        if (readBytes(c, 2, is) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        int byteOrder = c[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        posn+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        int uSectorShift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        if (readBytes(c, 2, is) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        if(byteOrder == 0xFE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            uSectorShift = c[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            uSectorShift += c[1] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            uSectorShift = c[0] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            uSectorShift += c[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        posn += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        toSkip = (long)0x30 - posn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        long skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if ((skipped = skipForward(is, toSkip)) < toSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
          is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
          return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        posn += skipped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        if (readBytes(c, 4, is) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        int sectDirStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        if(byteOrder == 0xFE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            sectDirStart = c[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            sectDirStart += c[1] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            sectDirStart += c[2] << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            sectDirStart += c[3] << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            sectDirStart =  c[0] << 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            sectDirStart += c[1] << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            sectDirStart += c[2] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            sectDirStart += c[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        posn += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        is.reset(); // Reset back to the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        toSkip = 0x200L + (long)(1<<uSectorShift)*sectDirStart + 0x50L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        // Sanity check!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (toSkip < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
         * How far can we skip? Is there any performance problem here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
         * This skip can be fairly long, at least 0x4c650 in at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
         * one case. Have to assume that the skip will fit in an int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
         * Leave room to read whole root dir
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        is.mark((int)toSkip+0x30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if ((skipForward(is, toSkip)) < toSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        /* should be at beginning of ClassID, which is as follows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
         * (in Intel byte order):
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
         *    00 67 61 56 54 C1 CE 11 85 53 00 AA 00 A1 F9 5B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
         * This is stored from Windows as long,short,short,char[8]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
         * so for byte order changes, the order only changes for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
         * the first 8 bytes in the ClassID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
         * Test against this, ignoring second byte (Intel) since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
         * this could change depending on part of Fpx file we have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        if (readBytes(c, 16, is) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        // intel byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        if (byteOrder == 0xFE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            c[0] == 0x00 && c[2] == 0x61 && c[3] == 0x56 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            c[4] == 0x54 && c[5] == 0xC1 && c[6] == 0xCE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
            c[7] == 0x11 && c[8] == 0x85 && c[9] == 0x53 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        // non-intel byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        else if (c[3] == 0x00 && c[1] == 0x61 && c[0] == 0x56 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            c[5] == 0x54 && c[4] == 0xC1 && c[7] == 0xCE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            c[6] == 0x11 && c[8] == 0x85 && c[9] == 0x53 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            c[10]== 0x00 && c[11]== 0xAA && c[12]== 0x00 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
            c[13]== 0xA1 && c[14]== 0xF9 && c[15]== 0x5B) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        is.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Tries to read the specified number of bytes from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * Returns -1, If EOF is reached before len bytes are read, returns 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    static private int readBytes(int c[], int len, InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        byte buf[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        if (is.read(buf, 0, len) < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        // fill the passed in int array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
             c[i] = buf[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * Skips through the specified number of bytes from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * until either EOF is reached, or the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * number of bytes have been skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    static private long skipForward(InputStream is, long toSkip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        long eachSkip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        long skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        while (skipped != toSkip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            eachSkip = is.skip(toSkip - skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            // check if EOF is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if (eachSkip <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                if (is.read() == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    return skipped ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                    skipped++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            skipped += eachSkip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        return skipped;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
class UnknownContentHandler extends ContentHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    public Object getContent(URLConnection uc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        return uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
}