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