问题 是否有可能java.net.URI对象的getPath方法返回null? (如果是的话,什么时候?)


根据 URI javadoc 该 getPath 方法返回“此URI的已解码路径组件, 如果路径未定义,则返回null“(强调补充)。这会让我相信,如果我的申请依赖于的回报价值 getPath,我可能需要检查一下 null。但是,似乎永远不会发生这种情况。

以下代码显示了我构建一个的尝试 URI 对象 getPath 返回null,但正如您所看到的,我还没有找到这样的情况。有人可以解释一下这可能会发生什么吗?

编辑:我已经注意到了 mailto URI没有路径。 但是,我真正要问的是:是否存在使用http,https,ftp或具有未定义/空路径的文件方案的URI?

import java.net.URI;
import java.net.URISyntaxException;

public class URIGetPathNullTest {

    public static void main(String []args) throws Exception {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
    }


    public static void test1() throws URISyntaxException {
        String urlString = "";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false         
    }

    public static void test2() throws URISyntaxException{
        String scheme = null;
        String ssp = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                ssp,
                fragment
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test3() throws URISyntaxException {
        String scheme = null;
        String userInfo = null;
        String host = null;
        int port = -1;
        String path = null;
        String query = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                userInfo,
                host,
                port,
                path,
                query,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false

    }

    public static void test4() throws URISyntaxException {
        String scheme = null;
        String host = null;
        String path = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                host,
                path,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test5() throws URISyntaxException {
        String scheme = null;
        String authority = null;
        String path = null;
        String query = null;
        String fragment = null;
        URI uri = new URI(
                scheme,
                authority,
                path,
                query,
                fragment             
            );
        printUri(uri);       
        // Output:
        //  toString() --> 
        //  getPath --> 
        //  getPath null? false
    }

    public static void test6() throws URISyntaxException {
        String urlString = "?some-query";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> ?some-query
        //  getPath --> 
        //  getPath null? false         
    }

    public static void test7() throws URISyntaxException {
        String urlString = "#some-fragment";
        URI uri = new URI(urlString);
        printUri(uri); 
        // Output:
        //  toString() --> #some-fragment
        //  getPath --> 
        //  getPath null? false         
    }

    public static void printUri(URI uri) {
        System.out.println("toString() --> " + uri.toString());
        System.out.println("getPath --> " + uri.getPath());
        System.out.println("getPath null? " + (uri.getPath() == null));
    }
}

9387
2017-08-29 19:12


起源



答案:


我正在读书 RFC2956 并注意到我只考虑与http / ftp / file方案对应的URI。一个 mailto URI是没有路径的示例。

public static void test8() throws URISyntaxException {
    String urlString = "mailto:mduerst@ifi.unizh.ch";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> mailto:mduerst@ifi.unizh.ch
    //  getPath --> null
    //  getPath null? true          
}

编辑:同样的 news 方案。

public static void test9() throws URISyntaxException {
    String urlString = "news:comp.infosystems.www.servers.unix";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> news:comp.infosystems.www.servers.unix
    //  getPath --> null
    //  getPath null? true          
}

(我把下面的其他答案结合起来)

有一个很好的线索 URI javadoc的开头

不透明URI是绝对URI,其特定于方案的部分不以斜杠开头   字符('/')。不透明的URI不需要进一步解析。一些不透明的例子   URI是:

邮寄地址:java-net@java.sun.com
  新闻:comp.lang.java
  金塔:ISBN:096139210x

看起来 getPath 当且仅当时,返回null URI 是不透明的,很容易检查 isOpaque 方法。因此,如果我确保URI不是不透明的,那么我不需要检查结果 getPath 对于 null

看着 java.net.URI的源代码 在openjdk 7-b147中,我注意到了一条评论 path 然后看了字段的来源 isOpaque,这似乎证实了这一点:

private transient String path;  // null ==> opaque

// ...

public boolean isOpaque() {
    return path == null;
}

15
2017-08-29 19:24



好一个。我学到了一些新东西。谢谢。 :) - Konstantin Yovkov


答案:


我正在读书 RFC2956 并注意到我只考虑与http / ftp / file方案对应的URI。一个 mailto URI是没有路径的示例。

public static void test8() throws URISyntaxException {
    String urlString = "mailto:mduerst@ifi.unizh.ch";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> mailto:mduerst@ifi.unizh.ch
    //  getPath --> null
    //  getPath null? true          
}

编辑:同样的 news 方案。

public static void test9() throws URISyntaxException {
    String urlString = "news:comp.infosystems.www.servers.unix";
    URI uri = new URI(urlString);
    printUri(uri); 
    // Output:
    //  toString() --> news:comp.infosystems.www.servers.unix
    //  getPath --> null
    //  getPath null? true          
}

(我把下面的其他答案结合起来)

有一个很好的线索 URI javadoc的开头

不透明URI是绝对URI,其特定于方案的部分不以斜杠开头   字符('/')。不透明的URI不需要进一步解析。一些不透明的例子   URI是:

邮寄地址:java-net@java.sun.com
  新闻:comp.lang.java
  金塔:ISBN:096139210x

看起来 getPath 当且仅当时,返回null URI 是不透明的,很容易检查 isOpaque 方法。因此,如果我确保URI不是不透明的,那么我不需要检查结果 getPath 对于 null

看着 java.net.URI的源代码 在openjdk 7-b147中,我注意到了一条评论 path 然后看了字段的来源 isOpaque,这似乎证实了这一点:

private transient String path;  // null ==> opaque

// ...

public boolean isOpaque() {
    return path == null;
}

15
2017-08-29 19:24



好一个。我学到了一些新东西。谢谢。 :) - Konstantin Yovkov