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 45 * {@link #close()} gets called. Instead, the stream will be marked as closed 46 * and no further 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 * Finishes writing to the underlying stream, but does NOT close the underlying stream. 86 * @throws IOException If an I/O problem occurs. 87 */ 88 @Override 89 public void close() throws IOException { 90 if (!this.closed) { 91 this.closed = true; 92 this.buffer.flush(this.outputStream); 93 } 94 } 95 96 @Override 97 public void flush() throws IOException { 98 this.buffer.flush(this.outputStream); 99 } 100 101 @Override 102 public void write(final byte[] b, final int off, final int len) throws IOException { 103 if (this.closed) { 104 throw new StreamClosedException(); 105 } 106 if (this.total < this.contentLength) { 107 final long max = this.contentLength - this.total; 108 int chunk = len; 109 if (chunk > max) { 110 chunk = (int) max; 111 } 112 this.buffer.write(b, off, chunk, this.outputStream); 113 this.total += chunk; 114 } 115 } 116 117 @Override 118 public void write(final byte[] b) throws IOException { 119 write(b, 0, b.length); 120 } 121 122 @Override 123 public void write(final int b) throws IOException { 124 if (this.closed) { 125 throw new StreamClosedException(); 126 } 127 if (this.total < this.contentLength) { 128 this.buffer.write(b, this.outputStream); 129 this.total++; 130 } 131 } 132 133 }