Path: blob/master/src/java.base/share/classes/sun/net/www/http/PosterOutputStream.java
41161 views
/*1* Copyright (c) 2001, 2013, 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.www.http;2627import java.io.*;28import java.net.*;2930/**31* Instances of this class are returned to applications for the purpose of32* sending user data for a HTTP request (excluding TRACE). This class is used33* when the content-length will be specified in the header of the request.34* The semantics of ByteArrayOutputStream are extended so that35* when close() is called, it is no longer possible to write36* additional data to the stream. From this point the content length of37* the request is fixed and cannot change.38*39* @author Michael McMahon40*/4142public class PosterOutputStream extends ByteArrayOutputStream {4344private boolean closed;4546/**47* Creates a new output stream for POST user data48*/49public PosterOutputStream () {50super (256);51}5253/**54* Writes the specified byte to this output stream.55*56* @param b the byte to be written.57*/58public synchronized void write(int b) {59if (closed) {60return;61}62super.write (b);63}6465/**66* Writes <code>len</code> bytes from the specified byte array67* starting at offset <code>off</code> to this output stream.68*69* @param b the data.70* @param off the start offset in the data.71* @param len the number of bytes to write.72*/73public synchronized void write(byte b[], int off, int len) {74if (closed) {75return;76}77super.write (b, off, len);78}7980/**81* Resets the <code>count</code> field of this output82* stream to zero, so that all currently accumulated output in the83* output stream is discarded. The output stream can be used again,84* reusing the already allocated buffer space. If the output stream85* has been closed, then this method has no effect.86*87* @see java.io.ByteArrayInputStream#count88*/89public synchronized void reset() {90if (closed) {91return;92}93super.reset ();94}9596/**97* After close() has been called, it is no longer possible to write98* to this stream. Further calls to write will have no effect.99*/100public synchronized void close() throws IOException {101closed = true;102super.close ();103}104}105106107