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