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