1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.impl.io;
29
30 import java.io.IOException;
31 import java.io.OutputStream;
32
33 import org.apache.http.annotation.NotThreadSafe;
34 import org.apache.http.io.SessionOutputBuffer;
35
36 /**
37 * Output stream that cuts off after a defined number of bytes. This class
38 * is used to send content of HTTP messages where the end of the content entity
39 * is determined by the value of the <code>Content-Length header</code>.
40 * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE}
41 * long.
42 * <p>
43 * Note that this class NEVER closes the underlying stream, even when close
44 * gets called. Instead, the stream will be marked as closed and no further
45 * output will be permitted.
46 *
47 * @since 4.0
48 */
49 @NotThreadSafe
50 public class ContentLengthOutputStream extends OutputStream {
51
52 /**
53 * Wrapped session output buffer.
54 */
55 private final SessionOutputBuffer out;
56
57 /**
58 * The maximum number of bytes that can be written the stream. Subsequent
59 * write operations will be ignored.
60 */
61 private final long contentLength;
62
63 /** Total bytes written */
64 private long total = 0;
65
66 /** True if the stream is closed. */
67 private boolean closed = false;
68
69 /**
70 * Wraps a session output buffer and cuts off output after a defined number
71 * of bytes.
72 *
73 * @param out The session output buffer
74 * @param contentLength The maximum number of bytes that can be written to
75 * the stream. Subsequent write operations will be ignored.
76 *
77 * @since 4.0
78 */
79 public ContentLengthOutputStream(final SessionOutputBuffer out, long contentLength) {
80 super();
81 if (out == null) {
82 throw new IllegalArgumentException("Session output buffer may not be null");
83 }
84 if (contentLength < 0) {
85 throw new IllegalArgumentException("Content length may not be negative");
86 }
87 this.out = out;
88 this.contentLength = contentLength;
89 }
90
91 /**
92 * <p>Does not close the underlying socket output.</p>
93 *
94 * @throws IOException If an I/O problem occurs.
95 */
96 @Override
97 public void close() throws IOException {
98 if (!this.closed) {
99 this.closed = true;
100 this.out.flush();
101 }
102 }
103
104 @Override
105 public void flush() throws IOException {
106 this.out.flush();
107 }
108
109 @Override
110 public void write(byte[] b, int off, int len) throws IOException {
111 if (this.closed) {
112 throw new IOException("Attempted write to closed stream.");
113 }
114 if (this.total < this.contentLength) {
115 long max = this.contentLength - this.total;
116 if (len > max) {
117 len = (int) max;
118 }
119 this.out.write(b, off, len);
120 this.total += len;
121 }
122 }
123
124 @Override
125 public void write(byte[] b) throws IOException {
126 write(b, 0, b.length);
127 }
128
129 @Override
130 public void write(int b) throws IOException {
131 if (this.closed) {
132 throw new IOException("Attempted write to closed stream.");
133 }
134 if (this.total < this.contentLength) {
135 this.out.write(b);
136 this.total++;
137 }
138 }
139
140 }