Showing the changes to java/pkg/sun/net/httpserver/FixedLengthOutputStream.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,104 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2005, 2006, 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 up to a defined | ||
| 35 | * number of bytes to an underlying stream. The caller *must* | ||
| 36 | * write the pre-defined number or else an exception will be thrown | ||
| 37 | * and the whole request aborted. | ||
| 38 | * normal close() does not close the underlying stream | ||
| 39 | */ | ||
| 40 | |||
| 41 | class FixedLengthOutputStream extends FilterOutputStream | ||
| 42 | { | ||
| 43 | private long remaining; | ||
| 44 | private boolean eof = false; | ||
| 45 | private boolean closed = false; | ||
| 46 | ExchangeImpl t; | ||
| 47 | |||
| 48 | FixedLengthOutputStream (ExchangeImpl t, OutputStream src, long len) { | ||
| 49 | super (src); | ||
| 50 | this.t = t; | ||
| 51 | this.remaining = len; | ||
| 52 | } | ||
| 53 | |||
| 54 | public void write (int b) throws IOException { | ||
| 55 | if (closed) { | ||
| 56 | throw new IOException ("stream closed"); | ||
| 57 | } | ||
| 58 | eof = (remaining == 0); | ||
| 59 | if (eof) { | ||
| 60 | throw new StreamClosedException(); | ||
| 61 | } | ||
| 62 | out.write(b); | ||
| 63 | remaining --; | ||
| 64 | } | ||
| 65 | |||
| 66 | public void write (byte[]b, int off, int len) throws IOException { | ||
| 67 | if (closed) { | ||
| 68 | throw new IOException ("stream closed"); | ||
| 69 | } | ||
| 70 | eof = (remaining == 0); | ||
| 71 | if (eof) { | ||
| 72 | throw new StreamClosedException(); | ||
| 73 | } | ||
| 74 | if (len > remaining) { | ||
| 75 | // stream is still open, caller can retry | ||
| 76 | throw new IOException ("too many bytes to write to stream"); | ||
| 77 | } | ||
| 78 | out.write(b, off, len); | ||
| 79 | remaining -= len; | ||
| 80 | } | ||
| 81 | |||
| 82 | public void close () throws IOException { | ||
| 83 | if (closed) { | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | closed = true; | ||
| 87 | if (remaining > 0) { | ||
| 88 | t.close(); | ||
| 89 | throw new IOException ("insufficient bytes written to stream"); | ||
| 90 | } | ||
| 91 | flush(); | ||
| 92 | eof = true; | ||
| 93 | LeftOverInputStream is = t.getOriginalInputStream(); | ||
| 94 | if (!is.isClosed()) { | ||
| 95 | try { | ||
| 96 | is.close(); | ||
| 97 | } catch (IOException e) {} | ||
| 98 | } | ||
| 99 | WriteFinishedEvent e = new WriteFinishedEvent (t); | ||
| 100 | t.getHttpContext().getServerImpl().addEvent (e); | ||
| 101 | } | ||
| 102 | |||
| 103 | // flush is a pass-through | ||
| 104 | } | ||