tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: DefaultServlet and getOutputStream() / getWriter()
Date Tue, 12 Oct 2004 17:46:19 GMT

Hi,
I didn't look at the DefaultServlet code at all, there's no need.  The post I was referring
to was one from you that said
Try { while { ... } } catch { ... } 
Where either ... can throw an exception is the same as
While { ... try { ... } catch { ... } }

And obviously the two are not equal, because the second construct would keep working the while
loop and the first one would abort on the first error.  That's a critical difference.  Now
you're quoting specific DefaultServlet code, and that's fine, I don't care to look at it now
because it obviously works and I have more important things to do.  But your original message
had just an abstract while/try/catch comparison question, nothing specific to DefaultServlet,
and that original message was wrong, and that's the one I was responding to.

Yoav Shapira http://www.yoavshapira.com
 

>-----Original Message-----
>From: Steffen Heil [mailto:lists@steffen-heil.de]
>Sent: Tuesday, October 12, 2004 1:43 PM
>To: 'Tomcat Developers List'
>Subject: AW: DefaultServlet and getOutputStream() / getWriter()
>
>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



This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.


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