001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/impl/io/IdentityOutputStream.java $
003: * $Revision: 560343 $
004: * $Date: 2007-07-27 20:18:19 +0200 (Fri, 27 Jul 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.io;
033:
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.apache.http.io.SessionOutputBuffer;
038:
039: /**
040: * A stream for writing with an "identity" transport encoding.
041: *
042: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
043: *
044: * @version $Revision: 560343 $
045: *
046: * @since 4.0
047: */
048: public class IdentityOutputStream extends OutputStream {
049:
050: /**
051: * Wrapped session output buffer.
052: */
053: private final SessionOutputBuffer out;
054:
055: /** True if the stream is closed. */
056: private boolean closed = false;
057:
058: public IdentityOutputStream(final SessionOutputBuffer out) {
059: super ();
060: if (out == null) {
061: throw new IllegalArgumentException(
062: "Session output buffer may not be null");
063: }
064: this .out = out;
065: }
066:
067: /**
068: * <p>Does not close the underlying socket output.</p>
069: *
070: * @throws IOException If an I/O problem occurs.
071: */
072: public void close() throws IOException {
073: if (!this .closed) {
074: this .closed = true;
075: this .out.flush();
076: }
077: }
078:
079: public void flush() throws IOException {
080: this .out.flush();
081: }
082:
083: public void write(byte[] b, int off, int len) throws IOException {
084: if (this .closed) {
085: throw new IOException("Attempted write to closed stream.");
086: }
087: this .out.write(b, off, len);
088: }
089:
090: public void write(byte[] b) throws IOException {
091: write(b, 0, b.length);
092: }
093:
094: public void write(int b) throws IOException {
095: if (this .closed) {
096: throw new IOException("Attempted write to closed stream.");
097: }
098: this.out.write(b);
099: }
100:
101: }
|