src/jdk.httpserver/share/classes/sun/net/httpserver/UndefLengthOutputStream.java
changeset 47216 71c04702a3d5
parent 25859 3317bb8137f4
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.net.httpserver;
       
    27 
       
    28 import java.io.*;
       
    29 import java.net.*;
       
    30 import com.sun.net.httpserver.*;
       
    31 import com.sun.net.httpserver.spi.*;
       
    32 
       
    33 /**
       
    34  * a class which allows the caller to write an indefinite
       
    35  * number of bytes to an underlying stream , but without using
       
    36  * chunked encoding. Used for http/1.0 clients only
       
    37  * The underlying connection needs to be closed afterwards.
       
    38  */
       
    39 
       
    40 class UndefLengthOutputStream extends FilterOutputStream
       
    41 {
       
    42     private boolean closed = false;
       
    43     ExchangeImpl t;
       
    44 
       
    45     UndefLengthOutputStream (ExchangeImpl t, OutputStream src) {
       
    46         super (src);
       
    47         this.t = t;
       
    48     }
       
    49 
       
    50     public void write (int b) throws IOException {
       
    51         if (closed) {
       
    52             throw new IOException ("stream closed");
       
    53         }
       
    54         out.write(b);
       
    55     }
       
    56 
       
    57     public void write (byte[]b, int off, int len) throws IOException {
       
    58         if (closed) {
       
    59             throw new IOException ("stream closed");
       
    60         }
       
    61         out.write(b, off, len);
       
    62     }
       
    63 
       
    64     public void close () throws IOException {
       
    65         if (closed) {
       
    66             return;
       
    67         }
       
    68         closed = true;
       
    69         flush();
       
    70         LeftOverInputStream is = t.getOriginalInputStream();
       
    71         if (!is.isClosed()) {
       
    72             try {
       
    73                 is.close();
       
    74             } catch (IOException e) {}
       
    75         }
       
    76         WriteFinishedEvent e = new WriteFinishedEvent (t);
       
    77         t.getHttpContext().getServerImpl().addEvent (e);
       
    78     }
       
    79 
       
    80     // flush is a pass-through
       
    81 }