Hmm...

so what about this line of code in StandardClassLoader:

    public InputStream getResourceAsStream(String name) {

        if (debug >= 2)
            log("getResourceAsStream(" + name + ")");
        InputStream stream = null;

        // (0) Check for a cached copy of this resource
        stream = findLoadedResource(name);
        if (stream != null) {
            if (debug >= 2)
                log("  --> Returning stream from cache");
            return (stream);
        }...

Looks to me that if the resource has been loaded, then it stays loaded,
even if it's been closed, at least for the StandardClassLoader...

I've got a scenario where the Context.getResourceAsStream returns me a
duff stream following a close on the stream.

I accept that your testcase proves that there must be something else hosing
me
here but it's a wee bit frustrating.

I guess I need to find out what the heck is going on with this stream
nonsense.

-Thom




-----Original Message-----
From: Remy Maucherat [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 08, 2001 3:49 PM
To: [EMAIL PROTECTED]
Subject: Re: how to reload a getResourceAsStream() object


Quoting Thom Park <[EMAIL PROTECTED]>:

> hello,
>
> I have a requirement where i need to make multiple passes over a
> resource
> stream
> obtained via ServletContext.getResourceAsStream().
>
> My problem is that as follows:
>
>       InputStream is = sc.getResourceAsStream("/my_resource_name");
>
>       .... do 1st processing pass
>
>       is.close();
>
>       is = sc.getResourceAsStream("/my_resource_name");
>
>       ... do second processing pass...
>
> The problem is that, as far as the StandardClassLoader is concerned,
> the
> cached copy of the input stream is still good (and it isn't I just
> closed
> it)
> and I get the original (now bad) input stream returned.
>
>
> Is there anyway to force tomcat to reload the resource object (without
> unloading the context).
> as, right now I'm reading the whole resource into a byte array and
> using
> [my] cached copy
> for processing.

Ok, but :
- SevletContext.getResourceAsStream != ClassLoader.getResourceAsStream. If
you
use the servlet context, it has nothing to do at all with any class loader.
- I modified the HelloWorldServlet from the examples webapp just to be sure,
and inserted :

        byte[] b = new byte[4096];
        InputStream is = getServletContext().getResourceAsStream
("/servlets/index.html");
        while (true) {
            int n = is.read(b);
            if (n == -1)
                break;
            System.out.println("Read1: " + n);
        }
        is.close();

        is =
getServletContext().getResourceAsStream("/servlets/index.html");
        while (true) {
            int n = is.read(b);
            if (n == -1)
                break;
            System.out.println("Read2: " + n);
        }
        is.close();

The display on the standard out is :

Read1: 4096
Read1: 892
Read2: 4096
Read2: 892

So it's working fine for me.

Using getResourceAsStream on the classloader should work the same (it will
end
up using URL.openStream which will in turn call Resource.streamContent, just
like ServletContext.getResourceAsStream).

Remy

Reply via email to