Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.impl.io;
30 import java.io.IOException;
31 import java.io.OutputStream;
33 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
35 /**
36 * Output stream that cuts off after a defined number of bytes. This class
37 * is used to send content of HTTP messages where the end of the content entity
38 * is determined by the value of the <code>Content-Length header</code>.
39 * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE}
40 * long.
41 * <p>
42 * Note that this class NEVER closes the underlying stream, even when close
43 * gets called. Instead, the stream will be marked as closed and no further
44 * output will be permitted.
45 *
46 * @since 4.0
47 */
48 public class ContentLengthOutputStream extends OutputStream {
50 /**
51 * Wrapped session output buffer.
52 */
53 private final SessionOutputBuffer out;
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;
61 /** Total bytes written */
62 private long total = 0;
64 /** True if the stream is closed. */
65 private boolean closed = false;
67 /**
68 * Wraps a session output buffer and cuts off output after a defined number
69 * of bytes.
70 *
71 * @param out The session output buffer
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 out, long contentLength) {
78 super();
79 if (out == null) {
80 throw new IllegalArgumentException("Session output buffer may not be null");
81 }
82 if (contentLength < 0) {
83 throw new IllegalArgumentException("Content length may not be negative");
84 }
85 this.out = out;
86 this.contentLength = contentLength;
87 }
89 /**
90 * <p>Does not close the underlying socket output.</p>
91 *
92 * @throws IOException If an I/O problem occurs.
93 */
94 public void close() throws IOException {
95 if (!this.closed) {
96 this.closed = true;
97 this.out.flush();
98 }
99 }
101 public void flush() throws IOException {
102 this.out.flush();
103 }
105 public void write(byte[] b, int off, int len) throws IOException {
106 if (this.closed) {
107 throw new IOException("Attempted write to closed stream.");
108 }
109 if (this.total < this.contentLength) {
110 long max = this.contentLength - this.total;
111 if (len > max) {
112 len = (int) max;
113 }
114 this.out.write(b, off, len);
115 this.total += len;
116 }
117 }
119 public void write(byte[] b) throws IOException {
120 write(b, 0, b.length);
121 }
123 public void write(int b) throws IOException {
124 if (this.closed) {
125 throw new IOException("Attempted write to closed stream.");
126 }
127 if (this.total < this.contentLength) {
128 this.out.write(b);
129 this.total++;
130 }
131 }
133 }