001: /*
002: * @(#)PosterOutputStream.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.net.www.http;
029:
030: import java.io.*;
031: import java.net.*;
032:
033: /**
034: * Instances of this class are returned to applications for the purpose of
035: * sending user data for a HTTP POST or PUT request. This class is used
036: * when the content-length will be specified in the header of the request.
037: * The semantics of ByteArrayOutputStream are extended so that
038: * when close() is called, it is no longer possible to write
039: * additional data to the stream. From this point the content length of
040: * the request is fixed and cannot change.
041: *
042: * @version 1.1, 07/26/01
043: * @author Michael McMahon
044: */
045:
046: public class PosterOutputStream extends ByteArrayOutputStream {
047:
048: private boolean closed;
049:
050: /**
051: * Creates a new output stream for POST user data
052: */
053: public PosterOutputStream() {
054: super (256);
055: }
056:
057: /**
058: * Writes the specified byte to this output stream.
059: *
060: * @param b the byte to be written.
061: */
062: public synchronized void write(int b) {
063: if (closed) {
064: return;
065: }
066: super .write(b);
067: }
068:
069: /**
070: * Writes <code>len</code> bytes from the specified byte array
071: * starting at offset <code>off</code> to this output stream.
072: *
073: * @param b the data.
074: * @param off the start offset in the data.
075: * @param len the number of bytes to write.
076: */
077: public synchronized void write(byte b[], int off, int len) {
078: if (closed) {
079: return;
080: }
081: super .write(b, off, len);
082: }
083:
084: /**
085: * Resets the <code>count</code> field of this output
086: * stream to zero, so that all currently accumulated output in the
087: * ouput stream is discarded. The output stream can be used again,
088: * reusing the already allocated buffer space. If the output stream
089: * has been closed, then this method has no effect.
090: *
091: * @see java.io.ByteArrayInputStream#count
092: */
093: public synchronized void reset() {
094: if (closed) {
095: return;
096: }
097: super .reset();
098: }
099:
100: /**
101: * After close() has been called, it is no longer possible to write
102: * to this stream. Further calls to write will have no effect.
103: */
104: public synchronized void close() throws IOException {
105: closed = true;
106: super.close();
107: }
108: }
|