Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/FixedLengthOutputStream.java
66646 views
/*1* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.httpserver;2627import java.io.*;28import java.net.*;29import com.sun.net.httpserver.*;30import com.sun.net.httpserver.spi.*;3132/**33* a class which allows the caller to write up to a defined34* number of bytes to an underlying stream. The caller *must*35* write the pre-defined number or else an exception will be thrown36* and the whole request aborted.37* normal close() does not close the underlying stream38*/3940class FixedLengthOutputStream extends FilterOutputStream41{42private long remaining;43private boolean eof = false;44private boolean closed = false;45ExchangeImpl t;4647FixedLengthOutputStream (ExchangeImpl t, OutputStream src, long len) {48super (src);49if (len < 0) {50throw new IllegalArgumentException("Content-Length: " + len);51}52this.t = t;53this.remaining = len;54}5556public void write (int b) throws IOException {57if (closed) {58throw new IOException ("stream closed");59}60eof = (remaining == 0);61if (eof) {62throw new StreamClosedException();63}64out.write(b);65remaining --;66}6768public void write (byte[]b, int off, int len) throws IOException {69if (closed) {70throw new IOException ("stream closed");71}72eof = (remaining == 0);73if (eof) {74throw new StreamClosedException();75}76if (len > remaining) {77// stream is still open, caller can retry78throw new IOException ("too many bytes to write to stream");79}80out.write(b, off, len);81remaining -= len;82}8384public void close () throws IOException {85if (closed) {86return;87}88closed = true;89if (remaining > 0) {90t.close();91throw new IOException ("insufficient bytes written to stream");92}93flush();94eof = true;95LeftOverInputStream is = t.getOriginalInputStream();96if (!is.isClosed()) {97try {98is.close();99} catch (IOException e) {}100}101WriteFinishedEvent e = new WriteFinishedEvent (t);102t.getHttpContext().getServerImpl().addEvent (e);103}104105// flush is a pass-through106}107108109