michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.io; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.OutputStream; michael@0: michael@0: import ch.boye.httpclientandroidlib.io.BufferInfo; michael@0: import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; michael@0: import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; michael@0: import ch.boye.httpclientandroidlib.params.CoreConnectionPNames; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: import ch.boye.httpclientandroidlib.params.HttpProtocolParams; michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: import ch.boye.httpclientandroidlib.util.ByteArrayBuffer; michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer; michael@0: michael@0: /** michael@0: * Abstract base class for session output buffers that stream data to michael@0: * an arbitrary {@link OutputStream}. This class buffers small chunks of michael@0: * output data in an internal byte array for optimal output performance. michael@0: *

michael@0: * {@link #writeLine(CharArrayBuffer)} and {@link #writeLine(String)} methods michael@0: * of this class use CR-LF as a line delimiter. michael@0: *

michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *

michael@0: *

michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public abstract class AbstractSessionOutputBuffer implements SessionOutputBuffer, BufferInfo { michael@0: michael@0: private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF}; michael@0: michael@0: private OutputStream outstream; michael@0: private ByteArrayBuffer buffer; michael@0: michael@0: private String charset = HTTP.US_ASCII; michael@0: private boolean ascii = true; michael@0: private int minChunkLimit = 512; michael@0: michael@0: private HttpTransportMetricsImpl metrics; michael@0: michael@0: /** michael@0: * Initializes this session output buffer. michael@0: * michael@0: * @param outstream the destination output stream. michael@0: * @param buffersize the size of the internal buffer. michael@0: * @param params HTTP parameters. michael@0: */ michael@0: protected void init(final OutputStream outstream, int buffersize, final HttpParams params) { michael@0: if (outstream == null) { michael@0: throw new IllegalArgumentException("Input stream may not be null"); michael@0: } michael@0: if (buffersize <= 0) { michael@0: throw new IllegalArgumentException("Buffer size may not be negative or zero"); michael@0: } michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: this.outstream = outstream; michael@0: this.buffer = new ByteArrayBuffer(buffersize); michael@0: this.charset = HttpProtocolParams.getHttpElementCharset(params); michael@0: this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII) michael@0: || this.charset.equalsIgnoreCase(HTTP.ASCII); michael@0: this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512); michael@0: this.metrics = createTransportMetrics(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: protected HttpTransportMetricsImpl createTransportMetrics() { michael@0: return new HttpTransportMetricsImpl(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.`1 michael@0: */ michael@0: public int capacity() { michael@0: return this.buffer.capacity(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public int length() { michael@0: return this.buffer.length(); michael@0: } michael@0: michael@0: /** michael@0: * @since 4.1 michael@0: */ michael@0: public int available() { michael@0: return capacity() - length(); michael@0: } michael@0: michael@0: protected void flushBuffer() throws IOException { michael@0: int len = this.buffer.length(); michael@0: if (len > 0) { michael@0: this.outstream.write(this.buffer.buffer(), 0, len); michael@0: this.buffer.clear(); michael@0: this.metrics.incrementBytesTransferred(len); michael@0: } michael@0: } michael@0: michael@0: public void flush() throws IOException { michael@0: flushBuffer(); michael@0: this.outstream.flush(); michael@0: } michael@0: michael@0: public void write(final byte[] b, int off, int len) throws IOException { michael@0: if (b == null) { michael@0: return; michael@0: } michael@0: // Do not want to buffer large-ish chunks michael@0: // if the byte array is larger then MIN_CHUNK_LIMIT michael@0: // write it directly to the output stream michael@0: if (len > this.minChunkLimit || len > this.buffer.capacity()) { michael@0: // flush the buffer michael@0: flushBuffer(); michael@0: // write directly to the out stream michael@0: this.outstream.write(b, off, len); michael@0: this.metrics.incrementBytesTransferred(len); michael@0: } else { michael@0: // Do not let the buffer grow unnecessarily michael@0: int freecapacity = this.buffer.capacity() - this.buffer.length(); michael@0: if (len > freecapacity) { michael@0: // flush the buffer michael@0: flushBuffer(); michael@0: } michael@0: // buffer michael@0: this.buffer.append(b, off, len); michael@0: } michael@0: } michael@0: michael@0: public void write(final byte[] b) throws IOException { michael@0: if (b == null) { michael@0: return; michael@0: } michael@0: write(b, 0, b.length); michael@0: } michael@0: michael@0: public void write(int b) throws IOException { michael@0: if (this.buffer.isFull()) { michael@0: flushBuffer(); michael@0: } michael@0: this.buffer.append(b); michael@0: } michael@0: michael@0: /** michael@0: * Writes characters from the specified string followed by a line delimiter michael@0: * to this session buffer. michael@0: *

michael@0: * This method uses CR-LF as a line delimiter. michael@0: * michael@0: * @param s the line. michael@0: * @exception IOException if an I/O error occurs. michael@0: */ michael@0: public void writeLine(final String s) throws IOException { michael@0: if (s == null) { michael@0: return; michael@0: } michael@0: if (s.length() > 0) { michael@0: write(s.getBytes(this.charset)); michael@0: } michael@0: write(CRLF); michael@0: } michael@0: michael@0: /** michael@0: * Writes characters from the specified char array followed by a line michael@0: * delimiter to this session buffer. michael@0: *

michael@0: * This method uses CR-LF as a line delimiter. michael@0: * michael@0: * @param s the buffer containing chars of the line. michael@0: * @exception IOException if an I/O error occurs. michael@0: */ michael@0: public void writeLine(final CharArrayBuffer s) throws IOException { michael@0: if (s == null) { michael@0: return; michael@0: } michael@0: if (this.ascii) { michael@0: int off = 0; michael@0: int remaining = s.length(); michael@0: while (remaining > 0) { michael@0: int chunk = this.buffer.capacity() - this.buffer.length(); michael@0: chunk = Math.min(chunk, remaining); michael@0: if (chunk > 0) { michael@0: this.buffer.append(s, off, chunk); michael@0: } michael@0: if (this.buffer.isFull()) { michael@0: flushBuffer(); michael@0: } michael@0: off += chunk; michael@0: remaining -= chunk; michael@0: } michael@0: } else { michael@0: // This is VERY memory inefficient, BUT since non-ASCII charsets are michael@0: // NOT meant to be used anyway, there's no point optimizing it michael@0: byte[] tmp = s.toString().getBytes(this.charset); michael@0: write(tmp); michael@0: } michael@0: write(CRLF); michael@0: } michael@0: michael@0: public HttpTransportMetrics getMetrics() { michael@0: return this.metrics; michael@0: } michael@0: michael@0: }