tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Souther <bsout...@fwdco.com>
Subject Re: AW: DefaultServlet and getOutputStream() / getWriter()
Date Tue, 12 Oct 2004 18:45:53 GMT





Steffen,

Compile, run, and view the output from this program.
I think you'll see the difference :o)


public class Loop{
    public static void main(String[] args){
        System.out.println("Try-Catch inside loop:");
        for(int i = 0; i < 10; i++){
            try{
                System.out.println(String.valueOf(i));
                if(i == 5) i = i / 0;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("\nLoop inside try-catch");
        try{
            for(int i = 0; i < 10; i++){
                System.out.println(String.valueOf(i));
                if(i == 5) i = i /0;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}









On Tue, 2004-10-12 at 13:43, Steffen Heil wrote:
> Hi
> 
>  
> > The rewritten while{} patch you suggested definitely changed behavior
> significantly, as I and others pointed out ;)
> 
> Ähm, no.
> Sorry to say that, but I think, you didn't review the code for that
> statement:
> One example taken from DefaultServlet.java, lines 2030 to 2054:
> 
>         IOException exception = null;
>         long bytesToRead = end - start + 1;
> 
>         char buffer[] = new char[input];
>         int len = buffer.length;
>         while ( (bytesToRead > 0) && (len >= buffer.length)) {
>             try {
>                 len = reader.read(buffer);
>                 if (bytesToRead >= len) {
>                     writer.write(buffer, 0, len);
>                     bytesToRead -= len;
>                 } else {
>                     writer.write(buffer, 0, (int) bytesToRead);
>                     bytesToRead = 0;
>                 }
>             } catch (IOException e) {
>                 exception = e;
>                 len = -1;
>             }
>             if (len < buffer.length)
>                 break;
>         }
> 
>         return exception;
> 
> THIS IS EQUAL TO:
> 
>         IOException exception = null;
>         long bytesToRead = end - start + 1;
> 
>         char buffer[] = new char[input];
>         int len = buffer.length;
>         try {
>            while ( (bytesToRead > 0) && (len >= buffer.length)) {
>                 len = reader.read(buffer);
>                 if (bytesToRead >= len) {
>                     writer.write(buffer, 0, len);
>                     bytesToRead -= len;
>                 } else {
>                     writer.write(buffer, 0, (int) bytesToRead);
>                     bytesToRead = 0;
>                 }
>             if (len < buffer.length)
>                 break;
>            }
>         } catch (IOException e) {
>            exception = e;
>            len = -1;
>         }
> 
>         return exception;
> 
> OR EVEN:
> 
>         long bytesToRead = end - start + 1;
> 
>         char buffer[] = new char[input];
>         int len = buffer.length;
>         try {
>            while ( (bytesToRead > 0) && (len >= buffer.length)) {
>                 len = reader.read(buffer);
>                 if (bytesToRead >= len) {
>                     writer.write(buffer, 0, len);
>                     bytesToRead -= len;
>                 } else {
>                     writer.write(buffer, 0, (int) bytesToRead);
>                     bytesToRead = 0;
>                 }
>            }
>            return null;
>         } catch (IOException e) {
>            return e;
>         }
> 
> I am very sure about this.
> And I also do NOT understand, why the exception is reported as result and
> not really thrown.
> The caller always uses:
> 
>         IOException exception = null;
> 
>         while ( (exception == null) && (ranges.hasMoreElements()) ) {
> 
>             ....
>             exception = copyRange(istream, ostream, currentRange.start,
>                                   currentRange.end);
>             ...
> 
>         }
> 
>         ostream.println();
>         ostream.print("--" + mimeSeparation + "--");
> 
>         // Rethrow any exception that has occurred
>         if (exception != null)
>             throw exception;
> 
> Whereas it would absolutely make more sense to me NOT to catch the Exception
> but rather use:
> 
>         try {
>             while ( ranges.hasMoreElements() ) {
> 
>                 ....
>                 copyRange(istream, ostream, currentRange.start,
>                                       currentRange.end);
>                 ...
>  
>             }
>         } finally {
>             // if nessesary, put code to ensure istream is closed here.
>             ostream.println();
>             ostream.print("--" + mimeSeparation + "--");
>         }
> 
> This is what try-finally is all about, isn't it?
> 
> 
> I agree, that I am new to this and I might be wrong, but this leads me back
> right to where I started. Whom to ask to understand the existing code?
> 
> 
> > When you're looking at the code, keep in mind that Tomcat's DefaultServlet
> (like virtually every other Tomcat component) can be extended or wrapped.
> Such wrappers or extenders could call getWriter first.  
> 
> Ok, I forgot about includes and such.
> 
> > I'm happy you're looking at the code.  If I were in your position (and I
> definitely was, although that seems long ago now ;)), I would look instead
> at Bugzilla, take a bug, and try to fix it.
> 
> I will propably do this some time soon. However, right now I am evaluating
> the Default Servlet code because I need to build my customized one and I
> thought a lot of work is already done. Not trying to reinvent the wheel
> though. That's why I am staring at DefaultServlet.java only right now.
> 
> Regards,
>   Steffen


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message