1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/ContentLengthOutputStream.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.impl.io; 1.32 + 1.33 +import java.io.IOException; 1.34 +import java.io.OutputStream; 1.35 + 1.36 +import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; 1.37 + 1.38 +/** 1.39 + * Output stream that cuts off after a defined number of bytes. This class 1.40 + * is used to send content of HTTP messages where the end of the content entity 1.41 + * is determined by the value of the <code>Content-Length header</code>. 1.42 + * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE} 1.43 + * long. 1.44 + * <p> 1.45 + * Note that this class NEVER closes the underlying stream, even when close 1.46 + * gets called. Instead, the stream will be marked as closed and no further 1.47 + * output will be permitted. 1.48 + * 1.49 + * @since 4.0 1.50 + */ 1.51 +public class ContentLengthOutputStream extends OutputStream { 1.52 + 1.53 + /** 1.54 + * Wrapped session output buffer. 1.55 + */ 1.56 + private final SessionOutputBuffer out; 1.57 + 1.58 + /** 1.59 + * The maximum number of bytes that can be written the stream. Subsequent 1.60 + * write operations will be ignored. 1.61 + */ 1.62 + private final long contentLength; 1.63 + 1.64 + /** Total bytes written */ 1.65 + private long total = 0; 1.66 + 1.67 + /** True if the stream is closed. */ 1.68 + private boolean closed = false; 1.69 + 1.70 + /** 1.71 + * Wraps a session output buffer and cuts off output after a defined number 1.72 + * of bytes. 1.73 + * 1.74 + * @param out The session output buffer 1.75 + * @param contentLength The maximum number of bytes that can be written to 1.76 + * the stream. Subsequent write operations will be ignored. 1.77 + * 1.78 + * @since 4.0 1.79 + */ 1.80 + public ContentLengthOutputStream(final SessionOutputBuffer out, long contentLength) { 1.81 + super(); 1.82 + if (out == null) { 1.83 + throw new IllegalArgumentException("Session output buffer may not be null"); 1.84 + } 1.85 + if (contentLength < 0) { 1.86 + throw new IllegalArgumentException("Content length may not be negative"); 1.87 + } 1.88 + this.out = out; 1.89 + this.contentLength = contentLength; 1.90 + } 1.91 + 1.92 + /** 1.93 + * <p>Does not close the underlying socket output.</p> 1.94 + * 1.95 + * @throws IOException If an I/O problem occurs. 1.96 + */ 1.97 + public void close() throws IOException { 1.98 + if (!this.closed) { 1.99 + this.closed = true; 1.100 + this.out.flush(); 1.101 + } 1.102 + } 1.103 + 1.104 + public void flush() throws IOException { 1.105 + this.out.flush(); 1.106 + } 1.107 + 1.108 + public void write(byte[] b, int off, int len) throws IOException { 1.109 + if (this.closed) { 1.110 + throw new IOException("Attempted write to closed stream."); 1.111 + } 1.112 + if (this.total < this.contentLength) { 1.113 + long max = this.contentLength - this.total; 1.114 + if (len > max) { 1.115 + len = (int) max; 1.116 + } 1.117 + this.out.write(b, off, len); 1.118 + this.total += len; 1.119 + } 1.120 + } 1.121 + 1.122 + public void write(byte[] b) throws IOException { 1.123 + write(b, 0, b.length); 1.124 + } 1.125 + 1.126 + public void write(int b) throws IOException { 1.127 + if (this.closed) { 1.128 + throw new IOException("Attempted write to closed stream."); 1.129 + } 1.130 + if (this.total < this.contentLength) { 1.131 + this.out.write(b); 1.132 + this.total++; 1.133 + } 1.134 + } 1.135 + 1.136 +}