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