0N/A/*
3261N/A * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * Open an file input stream given a URL.
0N/A * @author James Gosling
0N/A * @author Steven B. Byrne
0N/A */
0N/A
0N/Apackage sun.net.www.protocol.file;
0N/A
0N/Aimport java.net.URL;
0N/Aimport java.net.FileNameMap;
0N/Aimport java.io.*;
0N/Aimport java.text.Collator;
0N/Aimport java.security.Permission;
0N/Aimport sun.net.*;
0N/Aimport sun.net.www.*;
0N/Aimport java.util.*;
0N/Aimport java.text.SimpleDateFormat;
0N/A
0N/Aimport sun.security.action.GetPropertyAction;
0N/Aimport sun.security.action.GetIntegerAction;
0N/Aimport sun.security.action.GetBooleanAction;
0N/A
0N/Apublic class FileURLConnection extends URLConnection {
0N/A
0N/A static String CONTENT_LENGTH = "content-length";
0N/A static String CONTENT_TYPE = "content-type";
0N/A static String TEXT_PLAIN = "text/plain";
0N/A static String LAST_MODIFIED = "last-modified";
0N/A
0N/A String contentType;
0N/A InputStream is;
0N/A
0N/A File file;
0N/A String filename;
0N/A boolean isDirectory = false;
0N/A boolean exists = false;
2675N/A List<String> files;
0N/A
0N/A long length = -1;
0N/A long lastModified = 0;
0N/A
0N/A protected FileURLConnection(URL u, File file) {
0N/A super(u);
0N/A this.file = file;
0N/A }
0N/A
0N/A /*
0N/A * Note: the semantics of FileURLConnection object is that the
0N/A * results of the various URLConnection calls, such as
0N/A * getContentType, getInputStream or getContentLength reflect
0N/A * whatever was true when connect was called.
0N/A */
0N/A public void connect() throws IOException {
0N/A if (!connected) {
0N/A try {
0N/A filename = file.toString();
0N/A isDirectory = file.isDirectory();
0N/A if (isDirectory) {
2675N/A String[] fileList = file.list();
2675N/A if (fileList == null)
2675N/A throw new FileNotFoundException(filename + " exists, but is not accessible");
2675N/A files = Arrays.<String>asList(fileList);
0N/A } else {
0N/A
0N/A is = new BufferedInputStream(new FileInputStream(filename));
0N/A
0N/A // Check if URL should be metered
0N/A boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
0N/A if (meteredInput) {
0N/A ProgressSource pi = new ProgressSource(url, "GET", file.length());
0N/A is = new MeteredStream(is, pi, file.length());
0N/A }
0N/A }
0N/A } catch (IOException e) {
0N/A throw e;
0N/A }
0N/A connected = true;
0N/A }
0N/A }
0N/A
0N/A private boolean initializedHeaders = false;
0N/A
0N/A private void initializeHeaders() {
0N/A try {
0N/A connect();
0N/A exists = file.exists();
0N/A } catch (IOException e) {
0N/A }
0N/A if (!initializedHeaders || !exists) {
0N/A length = file.length();
0N/A lastModified = file.lastModified();
0N/A
0N/A if (!isDirectory) {
0N/A FileNameMap map = java.net.URLConnection.getFileNameMap();
0N/A contentType = map.getContentTypeFor(filename);
0N/A if (contentType != null) {
0N/A properties.add(CONTENT_TYPE, contentType);
0N/A }
0N/A properties.add(CONTENT_LENGTH, String.valueOf(length));
0N/A
0N/A /*
0N/A * Format the last-modified field into the preferred
0N/A * Internet standard - ie: fixed-length subset of that
0N/A * defined by RFC 1123
0N/A */
0N/A if (lastModified != 0) {
0N/A Date date = new Date(lastModified);
0N/A SimpleDateFormat fo =
0N/A new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
0N/A fo.setTimeZone(TimeZone.getTimeZone("GMT"));
0N/A properties.add(LAST_MODIFIED, fo.format(date));
0N/A }
0N/A } else {
0N/A properties.add(CONTENT_TYPE, TEXT_PLAIN);
0N/A }
0N/A initializedHeaders = true;
0N/A }
0N/A }
0N/A
0N/A public String getHeaderField(String name) {
0N/A initializeHeaders();
0N/A return super.getHeaderField(name);
0N/A }
0N/A
0N/A public String getHeaderField(int n) {
0N/A initializeHeaders();
0N/A return super.getHeaderField(n);
0N/A }
0N/A
0N/A public int getContentLength() {
0N/A initializeHeaders();
0N/A if (length > Integer.MAX_VALUE)
0N/A return -1;
0N/A return (int) length;
0N/A }
0N/A
0N/A public long getContentLengthLong() {
0N/A initializeHeaders();
0N/A return length;
0N/A }
0N/A
0N/A public String getHeaderFieldKey(int n) {
0N/A initializeHeaders();
0N/A return super.getHeaderFieldKey(n);
0N/A }
0N/A
0N/A public MessageHeader getProperties() {
0N/A initializeHeaders();
0N/A return super.getProperties();
0N/A }
0N/A
0N/A public long getLastModified() {
0N/A initializeHeaders();
0N/A return lastModified;
0N/A }
0N/A
0N/A public synchronized InputStream getInputStream()
0N/A throws IOException {
0N/A
0N/A int iconHeight;
0N/A int iconWidth;
0N/A
0N/A connect();
0N/A
0N/A if (is == null) {
0N/A if (isDirectory) {
0N/A FileNameMap map = java.net.URLConnection.getFileNameMap();
0N/A
0N/A StringBuffer buf = new StringBuffer();
0N/A
0N/A if (files == null) {
0N/A throw new FileNotFoundException(filename);
0N/A }
0N/A
0N/A Collections.sort(files, Collator.getInstance());
0N/A
0N/A for (int i = 0 ; i < files.size() ; i++) {
2675N/A String fileName = files.get(i);
0N/A buf.append(fileName);
0N/A buf.append("\n");
0N/A }
0N/A // Put it into a (default) locale-specific byte-stream.
0N/A is = new ByteArrayInputStream(buf.toString().getBytes());
0N/A } else {
0N/A throw new FileNotFoundException(filename);
0N/A }
0N/A }
0N/A return is;
0N/A }
0N/A
0N/A Permission permission;
0N/A
0N/A /* since getOutputStream isn't supported, only read permission is
0N/A * relevant
0N/A */
0N/A public Permission getPermission() throws IOException {
0N/A if (permission == null) {
0N/A String decodedPath = ParseUtil.decode(url.getPath());
0N/A if (File.separatorChar == '/') {
0N/A permission = new FilePermission(decodedPath, "read");
0N/A } else {
0N/A permission = new FilePermission(
0N/A decodedPath.replace('/',File.separatorChar), "read");
0N/A }
0N/A }
0N/A return permission;
0N/A }
0N/A}