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