blob: d3e2f33df5179d8edf301c101eb7c8177d949f40 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.net.httpserver;
27
28import java.io.*;
29import java.net.*;
30import com.sun.net.httpserver.*;
31import 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
41class 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}