Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/FixedLengthOutputStream.java
41159 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);49this.t = t;50this.remaining = len;51}5253public void write (int b) throws IOException {54if (closed) {55throw new IOException ("stream closed");56}57eof = (remaining == 0);58if (eof) {59throw new StreamClosedException();60}61out.write(b);62remaining --;63}6465public void write (byte[]b, int off, int len) throws IOException {66if (closed) {67throw new IOException ("stream closed");68}69eof = (remaining == 0);70if (eof) {71throw new StreamClosedException();72}73if (len > remaining) {74// stream is still open, caller can retry75throw new IOException ("too many bytes to write to stream");76}77out.write(b, off, len);78remaining -= len;79}8081public void close () throws IOException {82if (closed) {83return;84}85closed = true;86if (remaining > 0) {87t.close();88throw new IOException ("insufficient bytes written to stream");89}90flush();91eof = true;92LeftOverInputStream is = t.getOriginalInputStream();93if (!is.isClosed()) {94try {95is.close();96} catch (IOException e) {}97}98WriteFinishedEvent e = new WriteFinishedEvent (t);99t.getHttpContext().getServerImpl().addEvent (e);100}101102// flush is a pass-through103}104105106