tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steffen Heil" <li...@steffen-heil.de>
Subject AW: DefaultServlet and getOutputStream() / getWriter()
Date Tue, 12 Oct 2004 17:43:26 GMT
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

Mime
View raw message