Is the behavior of the directory in the getResourceAsStream directory described somewhere?

Various answers (for example, Get a list of resources from the classpath directory ) indicate that if you call getResourceAsStreaminto the directory, the returned stream will contain a list of elements in the directory, one per line. This is not like the documentation in the ClassLoader Javadoc. Is this indicated elsewhere, or is it just a implementation detail that people rely on?

+4
source share
1 answer

This doesn't seem to be properly documented somewhere.

Javadoc ClassLoader.getResourceAsStream , getResourceAsStream , getResource, URL.openStream URL-, Javadoc getResource, URL. .

public InputStream getResourceAsStream(String name)

. getResource(String).

URL.openStream :

public final InputStream openStream() throws IOException

URL- InputStream . :      openConnection().getInputStream()

, URL.openConnection() URLConnection , , , FileURLConnection, getInputStream.

, URL file:/// , InputStream . ( , - , )

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;
}

:

, , , .

+3

Source: https://habr.com/ru/post/1677070/


All Articles