Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/FixedLengthOutputStream.java
66646 views
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
if (len < 0) {
51
throw new IllegalArgumentException("Content-Length: " + len);
52
}
53
this.t = t;
54
this.remaining = len;
55
}
56
57
public void write (int b) throws IOException {
58
if (closed) {
59
throw new IOException ("stream closed");
60
}
61
eof = (remaining == 0);
62
if (eof) {
63
throw new StreamClosedException();
64
}
65
out.write(b);
66
remaining --;
67
}
68
69
public void write (byte[]b, int off, int len) throws IOException {
70
if (closed) {
71
throw new IOException ("stream closed");
72
}
73
eof = (remaining == 0);
74
if (eof) {
75
throw new StreamClosedException();
76
}
77
if (len > remaining) {
78
// stream is still open, caller can retry
79
throw new IOException ("too many bytes to write to stream");
80
}
81
out.write(b, off, len);
82
remaining -= len;
83
}
84
85
public void close () throws IOException {
86
if (closed) {
87
return;
88
}
89
closed = true;
90
if (remaining > 0) {
91
t.close();
92
throw new IOException ("insufficient bytes written to stream");
93
}
94
flush();
95
eof = true;
96
LeftOverInputStream is = t.getOriginalInputStream();
97
if (!is.isClosed()) {
98
try {
99
is.close();
100
} catch (IOException e) {}
101
}
102
WriteFinishedEvent e = new WriteFinishedEvent (t);
103
t.getHttpContext().getServerImpl().addEvent (e);
104
}
105
106
// flush is a pass-through
107
}
108
109