001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.internal.net.www.protocol.file;
019:
020: import java.io.IOException;
021: import java.net.Proxy;
022: import java.net.URL;
023: import java.net.URLConnection;
024: import java.net.URLStreamHandler;
025:
026: import org.apache.harmony.luni.util.Msg;
027:
028: /**
029: * This is the handler that is responsible for reading files from the file
030: * system.
031: */
032: public class Handler extends URLStreamHandler {
033:
034: /**
035: * Answers a connection to the a file pointed by this <code>URL</code> in
036: * the file system
037: *
038: * @return A connection to the resource pointed by this url.
039: * @param url
040: * URL The URL to which the connection is pointing to
041: *
042: */
043: @Override
044: public URLConnection openConnection(URL url) {
045: return new FileURLConnection(url);
046: }
047:
048: /**
049: * The behaviour of this method is the same as openConnection(URL).
050: * <code>proxy</code> is not used in FileURLConnection.
051: *
052: * @param url
053: * the URL which the connection is pointing to
054: * @param proxy
055: * Proxy
056: * @return a connection to the resource pointed by this url.
057: *
058: * @throws IOException
059: * if this handler fails to establish a connection.
060: * @throws IllegalArgumentException
061: * if any argument is null or of an invalid type.
062: * @throws UnsupportedOperationException
063: * if the protocol handler doesn't support this method.
064: */
065: @Override
066: public URLConnection openConnection(URL url, Proxy proxy)
067: throws IOException {
068: if (null == url || null == proxy) {
069: // K034b=url and proxy can not be null
070: throw new IllegalArgumentException(Msg.getString("K034b")); //$NON-NLS-1$
071: }
072: return new FileURLConnection(url);
073: }
074:
075: /**
076: * Parse the <code>string</code>str into <code>URL</code> u which
077: * already have the context properties. The string generally have the
078: * following format: <code><center>/c:/windows/win.ini</center></code>.
079: *
080: * @param u
081: * The URL object that's parsed into
082: * @param str
083: * The string equivalent of the specification URL
084: * @param start
085: * The index in the spec string from which to begin parsing
086: * @param end
087: * The index to stop parsing
088: *
089: * @see java.net.URLStreamHandler#toExternalForm(URL)
090: * @see java.net.URL
091: */
092: @Override
093: protected void parseURL(URL u, String str, int start, int end) {
094: if (end < start) {
095: return;
096: }
097: String parseString = ""; //$NON-NLS-1$
098: if (start < end) {
099: parseString = str.substring(start, end).replace('\\', '/');
100: }
101: super .parseURL(u, parseString, 0, parseString.length());
102: }
103: }
|