feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
233
jdkSrc/jdk8/sun/net/www/protocol/file/FileURLConnection.java
Normal file
233
jdkSrc/jdk8/sun/net/www/protocol/file/FileURLConnection.java
Normal file
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Open an file input stream given a URL.
|
||||
* @author James Gosling
|
||||
* @author Steven B. Byrne
|
||||
*/
|
||||
|
||||
package sun.net.www.protocol.file;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.FileNameMap;
|
||||
import java.io.*;
|
||||
import java.text.Collator;
|
||||
import java.security.Permission;
|
||||
import sun.net.*;
|
||||
import sun.net.www.*;
|
||||
import java.util.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import sun.security.action.GetPropertyAction;
|
||||
import sun.security.action.GetIntegerAction;
|
||||
import sun.security.action.GetBooleanAction;
|
||||
|
||||
public class FileURLConnection extends URLConnection {
|
||||
|
||||
static String CONTENT_LENGTH = "content-length";
|
||||
static String CONTENT_TYPE = "content-type";
|
||||
static String TEXT_PLAIN = "text/plain";
|
||||
static String LAST_MODIFIED = "last-modified";
|
||||
|
||||
String contentType;
|
||||
InputStream is;
|
||||
|
||||
File file;
|
||||
String filename;
|
||||
boolean isDirectory = false;
|
||||
boolean exists = false;
|
||||
List<String> files;
|
||||
|
||||
long length = -1;
|
||||
long lastModified = 0;
|
||||
|
||||
protected FileURLConnection(URL u, File file) {
|
||||
super(u);
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Note: the semantics of FileURLConnection object is that the
|
||||
* results of the various URLConnection calls, such as
|
||||
* getContentType, getInputStream or getContentLength reflect
|
||||
* whatever was true when connect was called.
|
||||
*/
|
||||
public void connect() throws IOException {
|
||||
if (!connected) {
|
||||
try {
|
||||
filename = file.toString();
|
||||
isDirectory = file.isDirectory();
|
||||
if (isDirectory) {
|
||||
String[] fileList = file.list();
|
||||
if (fileList == null)
|
||||
throw new FileNotFoundException(filename + " exists, but is not accessible");
|
||||
files = Arrays.<String>asList(fileList);
|
||||
} else {
|
||||
|
||||
is = new BufferedInputStream(new FileInputStream(filename));
|
||||
|
||||
// Check if URL should be metered
|
||||
boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
|
||||
if (meteredInput) {
|
||||
ProgressSource pi = new ProgressSource(url, "GET", file.length());
|
||||
is = new MeteredStream(is, pi, file.length());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean initializedHeaders = false;
|
||||
|
||||
private void initializeHeaders() {
|
||||
try {
|
||||
connect();
|
||||
exists = file.exists();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
if (!initializedHeaders || !exists) {
|
||||
length = file.length();
|
||||
lastModified = file.lastModified();
|
||||
|
||||
if (!isDirectory) {
|
||||
FileNameMap map = java.net.URLConnection.getFileNameMap();
|
||||
contentType = map.getContentTypeFor(filename);
|
||||
if (contentType != null) {
|
||||
properties.add(CONTENT_TYPE, contentType);
|
||||
}
|
||||
properties.add(CONTENT_LENGTH, String.valueOf(length));
|
||||
|
||||
/*
|
||||
* Format the last-modified field into the preferred
|
||||
* Internet standard - ie: fixed-length subset of that
|
||||
* defined by RFC 1123
|
||||
*/
|
||||
if (lastModified != 0) {
|
||||
Date date = new Date(lastModified);
|
||||
SimpleDateFormat fo =
|
||||
new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
|
||||
fo.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
properties.add(LAST_MODIFIED, fo.format(date));
|
||||
}
|
||||
} else {
|
||||
properties.add(CONTENT_TYPE, TEXT_PLAIN);
|
||||
}
|
||||
initializedHeaders = true;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHeaderField(String name) {
|
||||
initializeHeaders();
|
||||
return super.getHeaderField(name);
|
||||
}
|
||||
|
||||
public String getHeaderField(int n) {
|
||||
initializeHeaders();
|
||||
return super.getHeaderField(n);
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
initializeHeaders();
|
||||
if (length > Integer.MAX_VALUE)
|
||||
return -1;
|
||||
return (int) length;
|
||||
}
|
||||
|
||||
public long getContentLengthLong() {
|
||||
initializeHeaders();
|
||||
return length;
|
||||
}
|
||||
|
||||
public String getHeaderFieldKey(int n) {
|
||||
initializeHeaders();
|
||||
return super.getHeaderFieldKey(n);
|
||||
}
|
||||
|
||||
public MessageHeader getProperties() {
|
||||
initializeHeaders();
|
||||
return super.getProperties();
|
||||
}
|
||||
|
||||
public long getLastModified() {
|
||||
initializeHeaders();
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
public synchronized InputStream getInputStream()
|
||||
throws IOException {
|
||||
|
||||
int iconHeight;
|
||||
int iconWidth;
|
||||
|
||||
connect();
|
||||
|
||||
if (is == null) {
|
||||
if (isDirectory) {
|
||||
FileNameMap map = java.net.URLConnection.getFileNameMap();
|
||||
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
if (files == null) {
|
||||
throw new FileNotFoundException(filename);
|
||||
}
|
||||
|
||||
Collections.sort(files, Collator.getInstance());
|
||||
|
||||
for (int i = 0 ; i < files.size() ; i++) {
|
||||
String fileName = files.get(i);
|
||||
buf.append(fileName);
|
||||
buf.append("\n");
|
||||
}
|
||||
// Put it into a (default) locale-specific byte-stream.
|
||||
is = new ByteArrayInputStream(buf.toString().getBytes());
|
||||
} else {
|
||||
throw new FileNotFoundException(filename);
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
Permission permission;
|
||||
|
||||
/* since getOutputStream isn't supported, only read permission is
|
||||
* relevant
|
||||
*/
|
||||
public Permission getPermission() throws IOException {
|
||||
if (permission == null) {
|
||||
String decodedPath = ParseUtil.decode(url.getPath());
|
||||
if (File.separatorChar == '/') {
|
||||
permission = new FilePermission(decodedPath, "read");
|
||||
} else {
|
||||
permission = new FilePermission(
|
||||
decodedPath.replace('/',File.separatorChar), "read");
|
||||
}
|
||||
}
|
||||
return permission;
|
||||
}
|
||||
}
|
||||
154
jdkSrc/jdk8/sun/net/www/protocol/file/Handler.java
Normal file
154
jdkSrc/jdk8/sun/net/www/protocol/file/Handler.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.net.www.protocol.file;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.Proxy;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import sun.net.www.ParseUtil;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Open an file input stream given a URL.
|
||||
* @author James Gosling
|
||||
*/
|
||||
public class Handler extends URLStreamHandler {
|
||||
|
||||
private String getHost(URL url) {
|
||||
String host = url.getHost();
|
||||
if (host == null)
|
||||
host = "";
|
||||
return host;
|
||||
}
|
||||
|
||||
|
||||
protected void parseURL(URL u, String spec, int start, int limit) {
|
||||
/*
|
||||
* Ugly backwards compatibility. Flip any file separator
|
||||
* characters to be forward slashes. This is a nop on Unix
|
||||
* and "fixes" win32 file paths. According to RFC 2396,
|
||||
* only forward slashes may be used to represent hierarchy
|
||||
* separation in a URL but previous releases unfortunately
|
||||
* performed this "fixup" behavior in the file URL parsing code
|
||||
* rather than forcing this to be fixed in the caller of the URL
|
||||
* class where it belongs. Since backslash is an "unwise"
|
||||
* character that would normally be encoded if literally intended
|
||||
* as a non-seperator character the damage of veering away from the
|
||||
* specification is presumably limited.
|
||||
*/
|
||||
super.parseURL(u, spec.replace(File.separatorChar, '/'), start, limit);
|
||||
}
|
||||
|
||||
public synchronized URLConnection openConnection(URL url)
|
||||
throws IOException {
|
||||
return openConnection(url, null);
|
||||
}
|
||||
|
||||
public synchronized URLConnection openConnection(URL url, Proxy p)
|
||||
throws IOException {
|
||||
|
||||
String path;
|
||||
String file = url.getFile();
|
||||
String host = url.getHost();
|
||||
|
||||
path = ParseUtil.decode(file);
|
||||
path = path.replace('/', '\\');
|
||||
path = path.replace('|', ':');
|
||||
|
||||
if ((host == null) || host.equals("") ||
|
||||
host.equalsIgnoreCase("localhost") ||
|
||||
host.equals("~")) {
|
||||
return createFileURLConnection(url, new File(path));
|
||||
}
|
||||
|
||||
/*
|
||||
* attempt to treat this as a UNC path. See 4180841
|
||||
*/
|
||||
path = "\\\\" + host + path;
|
||||
File f = new File(path);
|
||||
if (f.exists()) {
|
||||
return new UNCFileURLConnection(url, f, path);
|
||||
}
|
||||
|
||||
/*
|
||||
* Now attempt an ftp connection.
|
||||
*/
|
||||
URLConnection uc;
|
||||
URL newurl;
|
||||
|
||||
try {
|
||||
newurl = new URL("ftp", host, file +
|
||||
(url.getRef() == null ? "":
|
||||
"#" + url.getRef()));
|
||||
if (p != null) {
|
||||
uc = newurl.openConnection(p);
|
||||
} else {
|
||||
uc = newurl.openConnection();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
uc = null;
|
||||
}
|
||||
if (uc == null) {
|
||||
throw new IOException("Unable to connect to: " +
|
||||
url.toExternalForm());
|
||||
}
|
||||
return uc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template method to be overriden by Java Plug-in. [stanleyh]
|
||||
*/
|
||||
protected URLConnection createFileURLConnection(URL url, File file) {
|
||||
return new FileURLConnection(url, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the host components of two URLs.
|
||||
* @param u1 the URL of the first host to compare
|
||||
* @param u2 the URL of the second host to compare
|
||||
* @return <tt>true</tt> if and only if they
|
||||
* are equal, <tt>false</tt> otherwise.
|
||||
*/
|
||||
protected boolean hostsEqual(URL u1, URL u2) {
|
||||
/*
|
||||
* Special case for file: URLs
|
||||
* per RFC 1738 no hostname is equivalent to 'localhost'
|
||||
* i.e. file:///path is equal to file://localhost/path
|
||||
*/
|
||||
String s1 = u1.getHost();
|
||||
String s2 = u2.getHost();
|
||||
if ("localhost".equalsIgnoreCase(s1) && ( s2 == null || "".equals(s2)))
|
||||
return true;
|
||||
if ("localhost".equalsIgnoreCase(s2) && ( s1 == null || "".equals(s1)))
|
||||
return true;
|
||||
return super.hostsEqual(u1, u2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.net.www.protocol.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilePermission;
|
||||
import java.net.URL;
|
||||
import java.security.Permission;
|
||||
|
||||
final class UNCFileURLConnection extends FileURLConnection {
|
||||
|
||||
private final String effectivePath;
|
||||
private volatile Permission permission;
|
||||
|
||||
UNCFileURLConnection(URL u, File file, String effectivePath) {
|
||||
super(u, file);
|
||||
this.effectivePath = effectivePath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Permission getPermission() {
|
||||
Permission perm = permission;
|
||||
if (perm == null) {
|
||||
permission = perm = new FilePermission(effectivePath, "read");
|
||||
}
|
||||
return perm;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user