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