Hi all,

While scouting through the source code I stumbled across
org.apache.http.conn.EofSensorInputStream. You have there three methods
along the lines of:

    protected void checkEOF(int eof) throws IOException {

       if ((wrappedStream != null) && (eof < 0)) {
           try {
               boolean scws = true; // should close wrapped stream?
               if (eofWatcher != null)
                   scws = eofWatcher.eofDetected(wrappedStream);
               if (scws)
                   wrappedStream.close();
           } finally {
               wrappedStream = null;
           }
       }
   }

This could be simplified by introducing:

   private static final EofSensorWatcher NULL_EOF_WATCHER = new
EofSensorWatcher() {
       public boolean eofDetected(InputStream wrapped) throws IOException {
           return true;
       }

       public boolean streamAbort(InputStream wrapped) throws IOException {
           return true;
       }

       public boolean streamClosed(InputStream wrapped) throws IOException
{
           return true;
       }
   };

which can be assigned in the constructor if there's no default
EofSensorWatcher submitted. This changes the implementation to the
following:

   protected void checkEOF(int eof) throws IOException {

       if ((wrappedStream != null) && (eof < 0)) {
           try {
               if (eofWatcher.eofDetected(wrappedStream))
                   wrappedStream.close();
           } finally {
               wrappedStream = null;
           }
       }
   }

which I think reads itself nicer. I avoid null checks in code when I can. I
like NULL implementations much more.

Just some thoughts.

Cheers,
Daniel

Reply via email to