View Javadoc
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.hc.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  
33  import org.apache.hc.core5.http.StreamClosedException;
34  import org.apache.hc.core5.http.io.SessionOutputBuffer;
35  import org.apache.hc.core5.util.Args;
36  
37  /**
38   * Output stream that cuts off after a defined number of bytes. This class
39   * is used to send content of HTTP messages where the end of the content entity
40   * is determined by the value of the {@code Content-Length header}.
41   * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE}
42   * long.
43   * <p>
44   * Note that this class NEVER closes the underlying stream, even when close
45   * gets called.  Instead, the stream will be marked as closed and no further
46   * output will be permitted.
47   *
48   * @since 4.0
49   */
50  public class ContentLengthOutputStream extends OutputStream {
51  
52      private final SessionOutputBuffer buffer;
53      private final OutputStream outputStream;
54  
55      /**
56       * The maximum number of bytes that can be written the stream. Subsequent
57       * write operations will be ignored.
58       */
59      private final long contentLength;
60  
61      /** Total bytes written */
62      private long total;
63  
64      /** True if the stream is closed. */
65      private boolean closed;
66  
67      /**
68       * Default constructor.
69       *
70       * @param buffer Session output buffer
71       * @param outputStream Output stream
72       * @param contentLength The maximum number of bytes that can be written to
73       * the stream. Subsequent write operations will be ignored.
74       *
75       * @since 4.0
76       */
77      public ContentLengthOutputStream(final SessionOutputBuffer buffer, final OutputStream outputStream, final long contentLength) {
78          super();
79          this.buffer = Args.notNull(buffer, "Session output buffer");
80          this.outputStream = Args.notNull(outputStream, "Output stream");
81          this.contentLength = Args.notNegative(contentLength, "Content length");
82      }
83  
84      /**
85       * <p>Does not close the underlying socket output.</p>
86       *
87       * @throws IOException If an I/O problem occurs.
88       */
89      @Override
90      public void close() throws IOException {
91          if (!this.closed) {
92              this.closed = true;
93              this.buffer.flush(this.outputStream);
94          }
95      }
96  
97      @Override
98      public void flush() throws IOException {
99          this.buffer.flush(this.outputStream);
100     }
101 
102     @Override
103     public void write(final byte[] b, final int off, final int len) throws IOException {
104         if (this.closed) {
105             throw new StreamClosedException();
106         }
107         if (this.total < this.contentLength) {
108             final long max = this.contentLength - this.total;
109             int chunk = len;
110             if (chunk > max) {
111                 chunk = (int) max;
112             }
113             this.buffer.write(b, off, chunk, this.outputStream);
114             this.total += chunk;
115         }
116     }
117 
118     @Override
119     public void write(final byte[] b) throws IOException {
120         write(b, 0, b.length);
121     }
122 
123     @Override
124     public void write(final int b) throws IOException {
125         if (this.closed) {
126             throw new StreamClosedException();
127         }
128         if (this.total < this.contentLength) {
129             this.buffer.write(b, this.outputStream);
130             this.total++;
131         }
132     }
133 
134 }