/*
 * Copyright 1998-2009 University Corporation for Atmospheric Research/Unidata
 *
 * Portions of this software were developed by the Unidata Program at the
 * University Corporation for Atmospheric Research.
 *
 * Access and use of this software shall impose the following obligations
 * and understandings on the user. The user is granted the right, without
 * any fee or cost, to use, copy, modify, alter, enhance and distribute
 * this software, and any derivative works thereof, and its supporting
 * documentation for any purpose whatsoever, provided that this entire
 * notice appears in all copies of the software, derivative works and
 * supporting documentation.  Further, UCAR requests that the user credit
 * UCAR/Unidata in any publications that result from the use of this
 * software or in any product that includes this software. The names UCAR
 * and/or Unidata, however, may not be used in any advertising or publicity
 * to endorse or promote any products or commercial entity unless specific
 * written permission is obtained from UCAR/Unidata. The user also
 * understands that UCAR/Unidata is not obligated to provide the user with
 * any support, consulting, training or assistance of any kind with regard
 * to the use, operation and performance of this software nor to provide
 * the user with any updates, revisions, new versions or "bug fixes."
 *
 * THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package ucar.nc2.util.net;

import java.io.IOException;
import java.io.FilterInputStream;
import java.io.InputStream;

/**
 * THe goal of this class is to allow
 * other classes to access the data stream
 * associated with a method response.
 * It tracks the method and the session
 * to allow them to be closed
 * when the stream hits eof.
 * It also guarantees that any remaining unconsumed
 * input is consumed.
 *
 * Note that this class is not public in the package.
 *
 * Note that this code now includes the equivalent of
 * Tom Kunicki's proposed pull request, but with the HTTPMethod
 * close extension he proposes but does not implement.
 * Pull request: https://github.com/tkunicki-usgs/thredds/commit/3b750ec0016a137db66336adeac421a9202b9d30
 *
 */


class HTTPMethodStream extends FilterInputStream
{
    HTTPMethod method = null;
    InputStream stream = null; // in case someone wants to retrieve it

    HTTPMethodStream(InputStream stream, HTTPMethod method)
    {
	super(stream);
	this.method = method;
	this.stream = stream;
    }

    InputStream getStream() {return stream;}

    /**
       * Closes this input stream and releases any system resources associated
       * with the stream; closes the method also.
       *
       * @exception  IOException  if an I/O error occurs.
       */
    @Override
    public void close() throws IOException
    {
	try {
	    consume();
	} finally {
	    super.close();
        }
        if(method != null) method.close();
    }

    void consume() throws IOException
    {
        long consumed = 0;
        long available;
        while ((available = available()) > 0) {
	    consumed += skip(available);
        }
        if (consumed > 0) {
	    // TODO: Logging strategy? Recommend warning if consumed > 0;
        }
      }
}

@@ -2748,16 +2748,233 @@
 tream%0A%7B%0A
+    //////////////////////////////////////////////////////////////////////////%0A    static public org.slf4j.Logger log = HTTPSession.log;%0A%0A    //////////////////////////////////////////////////////////////////////////%0A
     HTTP
@@ -3065,16 +3065,44 @@
 rieve it
+%0A    boolean closed = false;
 %0A%0A    HT
@@ -3226,24 +3226,66 @@
 eam;%0A    %7D%0A%0A
+    boolean getClosed() %7Breturn closed;%7D%0A%0A
     InputStr
@@ -3520,16 +3520,84 @@
 r occurs
+; but not if close%0A       *                          is called twice
 .%0A      
@@ -3667,16 +3667,125 @@
   %7B%0A
-%09try %7B%0A%09
+        if(closed)%0A            return; /* Allow multiple close calls */%0A        closed = true;%0A        try %7B%0A        
     
@@ -3795,17 +3795,24 @@
 sume();%0A
-%09
+        
 %7D finall
@@ -3815,17 +3815,24 @@
 nally %7B%0A
-%09
+        
     supe
@@ -3924,34 +3924,33 @@
 me()
- throws IOException
+%0A    %7B%0A        try %7B
 %0A    
-%7B%0A
     
@@ -3976,24 +3976,28 @@
  0;%0A        
+    
 long availab
@@ -4008,16 +4008,20 @@
         
+ 
+   
 while ((
@@ -4048,25 +4048,32 @@
 e()) %3E 0) %7B%0A
-%09
+        
     consumed
@@ -4101,26 +4101,32 @@
         
-%7D%0A
+    %7D%0A  
         
 if (cons
@@ -4117,16 +4117,18 @@
         
+  
 if (cons
@@ -4143,84 +4143,132 @@
 ) %7B%0A
-%09
     
-// TODO: Logging strategy? Recommend warning if consumed %3E 0;%0A        %7D
+            log.warn(%22HTTPMethodStream: unconsumed data%22);%0A            %7D%0A        %7D catch (IOException ioe) %7B/*ignore*/%7D;
 %0A   

Allow multiple closes on HTTPMethodStream.close()