1 /*
2  * Copyright (c) 2001, 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.www.http;
27 
28 import java.io.*;
29 import java.net.*;
30 
31 /**
32  * Instances of this class are returned to applications for the purpose of
33  * sending user data for a HTTP POST or PUT request. This class is used
34  * when the content-length will be specified in the header of the request.
35  * The semantics of ByteArrayOutputStream are extended so that
36  * when close() is called, it is no longer possible to write
37  * additional data to the stream. From this point the content length of
38  * the request is fixed and cannot change.
39  *
40  * @author Michael McMahon
41  */
42 
43 public class PosterOutputStream extends ByteArrayOutputStream {
44 
45     private boolean closed;
46 
47     /**
48      * Creates a new output stream for POST user data
49      */
PosterOutputStream()50     public PosterOutputStream () {
51         super (256);
52     }
53 
54     /**
55      * Writes the specified byte to this output stream.
56      *
57      * @param   b   the byte to be written.
58      */
write(int b)59     public synchronized void write(int b) {
60         if (closed) {
61             return;
62         }
63         super.write (b);
64     }
65 
66     /**
67      * Writes <code>len</code> bytes from the specified byte array
68      * starting at offset <code>off</code> to this output stream.
69      *
70      * @param   b     the data.
71      * @param   off   the start offset in the data.
72      * @param   len   the number of bytes to write.
73      */
write(byte b[], int off, int len)74     public synchronized void write(byte b[], int off, int len) {
75         if (closed) {
76             return;
77         }
78         super.write (b, off, len);
79     }
80 
81     /**
82      * Resets the <code>count</code> field of this output
83      * stream to zero, so that all currently accumulated output in the
84      * ouput stream is discarded. The output stream can be used again,
85      * reusing the already allocated buffer space. If the output stream
86      * has been closed, then this method has no effect.
87      *
88      * @see     java.io.ByteArrayInputStream#count
89      */
reset()90     public synchronized void reset() {
91         if (closed) {
92             return;
93         }
94         super.reset ();
95     }
96 
97     /**
98      * After close() has been called, it is no longer possible to write
99      * to this stream. Further calls to write will have no effect.
100      */
close()101     public synchronized void close() throws IOException {
102         closed = true;
103         super.close ();
104     }
105 }
106