mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/io/AbstractSessionOutputBuffer.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.io;
michael@0 29
michael@0 30 import java.io.IOException;
michael@0 31 import java.io.OutputStream;
michael@0 32
michael@0 33 import ch.boye.httpclientandroidlib.io.BufferInfo;
michael@0 34 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer;
michael@0 35 import ch.boye.httpclientandroidlib.io.HttpTransportMetrics;
michael@0 36 import ch.boye.httpclientandroidlib.params.CoreConnectionPNames;
michael@0 37 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 38 import ch.boye.httpclientandroidlib.params.HttpProtocolParams;
michael@0 39 import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0 40 import ch.boye.httpclientandroidlib.util.ByteArrayBuffer;
michael@0 41 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0 42
michael@0 43 /**
michael@0 44 * Abstract base class for session output buffers that stream data to
michael@0 45 * an arbitrary {@link OutputStream}. This class buffers small chunks of
michael@0 46 * output data in an internal byte array for optimal output performance.
michael@0 47 * <p>
michael@0 48 * {@link #writeLine(CharArrayBuffer)} and {@link #writeLine(String)} methods
michael@0 49 * of this class use CR-LF as a line delimiter.
michael@0 50 * <p>
michael@0 51 * The following parameters can be used to customize the behavior of this
michael@0 52 * class:
michael@0 53 * <ul>
michael@0 54 * <li>{@link ch.boye.httpclientandroidlib.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
michael@0 55 * <li>{@link ch.boye.httpclientandroidlib.params.CoreConnectionPNames#MIN_CHUNK_LIMIT}</li>
michael@0 56 * </ul>
michael@0 57 * <p>
michael@0 58 *
michael@0 59 * @since 4.0
michael@0 60 */
michael@0 61 public abstract class AbstractSessionOutputBuffer implements SessionOutputBuffer, BufferInfo {
michael@0 62
michael@0 63 private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF};
michael@0 64
michael@0 65 private OutputStream outstream;
michael@0 66 private ByteArrayBuffer buffer;
michael@0 67
michael@0 68 private String charset = HTTP.US_ASCII;
michael@0 69 private boolean ascii = true;
michael@0 70 private int minChunkLimit = 512;
michael@0 71
michael@0 72 private HttpTransportMetricsImpl metrics;
michael@0 73
michael@0 74 /**
michael@0 75 * Initializes this session output buffer.
michael@0 76 *
michael@0 77 * @param outstream the destination output stream.
michael@0 78 * @param buffersize the size of the internal buffer.
michael@0 79 * @param params HTTP parameters.
michael@0 80 */
michael@0 81 protected void init(final OutputStream outstream, int buffersize, final HttpParams params) {
michael@0 82 if (outstream == null) {
michael@0 83 throw new IllegalArgumentException("Input stream may not be null");
michael@0 84 }
michael@0 85 if (buffersize <= 0) {
michael@0 86 throw new IllegalArgumentException("Buffer size may not be negative or zero");
michael@0 87 }
michael@0 88 if (params == null) {
michael@0 89 throw new IllegalArgumentException("HTTP parameters may not be null");
michael@0 90 }
michael@0 91 this.outstream = outstream;
michael@0 92 this.buffer = new ByteArrayBuffer(buffersize);
michael@0 93 this.charset = HttpProtocolParams.getHttpElementCharset(params);
michael@0 94 this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII)
michael@0 95 || this.charset.equalsIgnoreCase(HTTP.ASCII);
michael@0 96 this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512);
michael@0 97 this.metrics = createTransportMetrics();
michael@0 98 }
michael@0 99
michael@0 100 /**
michael@0 101 * @since 4.1
michael@0 102 */
michael@0 103 protected HttpTransportMetricsImpl createTransportMetrics() {
michael@0 104 return new HttpTransportMetricsImpl();
michael@0 105 }
michael@0 106
michael@0 107 /**
michael@0 108 * @since 4.`1
michael@0 109 */
michael@0 110 public int capacity() {
michael@0 111 return this.buffer.capacity();
michael@0 112 }
michael@0 113
michael@0 114 /**
michael@0 115 * @since 4.1
michael@0 116 */
michael@0 117 public int length() {
michael@0 118 return this.buffer.length();
michael@0 119 }
michael@0 120
michael@0 121 /**
michael@0 122 * @since 4.1
michael@0 123 */
michael@0 124 public int available() {
michael@0 125 return capacity() - length();
michael@0 126 }
michael@0 127
michael@0 128 protected void flushBuffer() throws IOException {
michael@0 129 int len = this.buffer.length();
michael@0 130 if (len > 0) {
michael@0 131 this.outstream.write(this.buffer.buffer(), 0, len);
michael@0 132 this.buffer.clear();
michael@0 133 this.metrics.incrementBytesTransferred(len);
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 public void flush() throws IOException {
michael@0 138 flushBuffer();
michael@0 139 this.outstream.flush();
michael@0 140 }
michael@0 141
michael@0 142 public void write(final byte[] b, int off, int len) throws IOException {
michael@0 143 if (b == null) {
michael@0 144 return;
michael@0 145 }
michael@0 146 // Do not want to buffer large-ish chunks
michael@0 147 // if the byte array is larger then MIN_CHUNK_LIMIT
michael@0 148 // write it directly to the output stream
michael@0 149 if (len > this.minChunkLimit || len > this.buffer.capacity()) {
michael@0 150 // flush the buffer
michael@0 151 flushBuffer();
michael@0 152 // write directly to the out stream
michael@0 153 this.outstream.write(b, off, len);
michael@0 154 this.metrics.incrementBytesTransferred(len);
michael@0 155 } else {
michael@0 156 // Do not let the buffer grow unnecessarily
michael@0 157 int freecapacity = this.buffer.capacity() - this.buffer.length();
michael@0 158 if (len > freecapacity) {
michael@0 159 // flush the buffer
michael@0 160 flushBuffer();
michael@0 161 }
michael@0 162 // buffer
michael@0 163 this.buffer.append(b, off, len);
michael@0 164 }
michael@0 165 }
michael@0 166
michael@0 167 public void write(final byte[] b) throws IOException {
michael@0 168 if (b == null) {
michael@0 169 return;
michael@0 170 }
michael@0 171 write(b, 0, b.length);
michael@0 172 }
michael@0 173
michael@0 174 public void write(int b) throws IOException {
michael@0 175 if (this.buffer.isFull()) {
michael@0 176 flushBuffer();
michael@0 177 }
michael@0 178 this.buffer.append(b);
michael@0 179 }
michael@0 180
michael@0 181 /**
michael@0 182 * Writes characters from the specified string followed by a line delimiter
michael@0 183 * to this session buffer.
michael@0 184 * <p>
michael@0 185 * This method uses CR-LF as a line delimiter.
michael@0 186 *
michael@0 187 * @param s the line.
michael@0 188 * @exception IOException if an I/O error occurs.
michael@0 189 */
michael@0 190 public void writeLine(final String s) throws IOException {
michael@0 191 if (s == null) {
michael@0 192 return;
michael@0 193 }
michael@0 194 if (s.length() > 0) {
michael@0 195 write(s.getBytes(this.charset));
michael@0 196 }
michael@0 197 write(CRLF);
michael@0 198 }
michael@0 199
michael@0 200 /**
michael@0 201 * Writes characters from the specified char array followed by a line
michael@0 202 * delimiter to this session buffer.
michael@0 203 * <p>
michael@0 204 * This method uses CR-LF as a line delimiter.
michael@0 205 *
michael@0 206 * @param s the buffer containing chars of the line.
michael@0 207 * @exception IOException if an I/O error occurs.
michael@0 208 */
michael@0 209 public void writeLine(final CharArrayBuffer s) throws IOException {
michael@0 210 if (s == null) {
michael@0 211 return;
michael@0 212 }
michael@0 213 if (this.ascii) {
michael@0 214 int off = 0;
michael@0 215 int remaining = s.length();
michael@0 216 while (remaining > 0) {
michael@0 217 int chunk = this.buffer.capacity() - this.buffer.length();
michael@0 218 chunk = Math.min(chunk, remaining);
michael@0 219 if (chunk > 0) {
michael@0 220 this.buffer.append(s, off, chunk);
michael@0 221 }
michael@0 222 if (this.buffer.isFull()) {
michael@0 223 flushBuffer();
michael@0 224 }
michael@0 225 off += chunk;
michael@0 226 remaining -= chunk;
michael@0 227 }
michael@0 228 } else {
michael@0 229 // This is VERY memory inefficient, BUT since non-ASCII charsets are
michael@0 230 // NOT meant to be used anyway, there's no point optimizing it
michael@0 231 byte[] tmp = s.toString().getBytes(this.charset);
michael@0 232 write(tmp);
michael@0 233 }
michael@0 234 write(CRLF);
michael@0 235 }
michael@0 236
michael@0 237 public HttpTransportMetrics getMetrics() {
michael@0 238 return this.metrics;
michael@0 239 }
michael@0 240
michael@0 241 }

mercurial