Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/impl/io/AbstractHttpDataTransmitter.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/io/AbstractHttpDataTransmitter.java $
3    * $Revision: 390877 $
4    * $Date: 2006-04-02 20:12:59 +0200 (Sun, 02 Apr 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2002-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.http.impl.io;
31  
32  import java.io.IOException;
33  import java.io.OutputStream;
34  
35  import org.apache.http.io.ByteArrayBuffer;
36  import org.apache.http.io.CharArrayBuffer;
37  import org.apache.http.io.HttpDataTransmitter;
38  import org.apache.http.params.HttpParams;
39  import org.apache.http.params.HttpProtocolParams;
40  import org.apache.http.protocol.HTTP;
41  
42  /**
43   * Abstract base class for data transmitters using traditional IO.
44   *
45   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
46   *
47   */
48  public abstract class AbstractHttpDataTransmitter implements HttpDataTransmitter {
49  
50      private static final byte[] CRLF = new byte[] {HTTP.CR, HTTP.LF};
51      
52      private static int MAX_CHUNK = 256;
53      
54      private OutputStream outstream;
55      private ByteArrayBuffer buffer;
56          
57      private String charset = HTTP.US_ASCII;
58      private boolean ascii = true;
59      
60      protected void init(final OutputStream outstream, int buffersize) {
61          if (outstream == null) {
62              throw new IllegalArgumentException("Input stream may not be null");
63          }
64          if (buffersize <= 0) {
65              throw new IllegalArgumentException("Buffer size may not be negative or zero");
66          }
67          this.outstream = outstream;
68          this.buffer = new ByteArrayBuffer(buffersize);
69      }
70      
71      protected void flushBuffer() throws IOException {
72          if (this.buffer.length() > 0) {
73              this.outstream.write(this.buffer.buffer(), 0, this.buffer.length());
74              this.buffer.clear();
75          }
76      }
77      
78      public void flush() throws IOException {
79          flushBuffer();
80          this.outstream.flush();
81      }
82      
83      public void write(final byte[] b, int off, int len) throws IOException {
84          if (b == null) {
85              return;
86          }
87          // Do not want to buffer largish chunks
88          // if the byte array is larger then MAX_CHUNK
89          // write it directly to the output stream
90          if (len > MAX_CHUNK || len > this.buffer.capacity()) {
91              // flush the buffer
92              flushBuffer();
93              // write directly to the out stream
94              this.outstream.write(b, off, len);
95          } else {
96              // Do not let the buffer grow unnecessarily
97              int freecapacity = this.buffer.capacity() - this.buffer.length();
98              if (len > freecapacity) {
99                  // flush the buffer
100                 flushBuffer();
101             }
102             // buffer
103             this.buffer.append(b, off, len);
104         }
105     }
106     
107     public void write(final byte[] b) throws IOException {
108         if (b == null) {
109             return;
110         }
111         write(b, 0, b.length);
112     }
113     
114     public void write(int b) throws IOException {
115         if (this.buffer.isFull()) {
116             flushBuffer();
117         }
118         this.buffer.append(b);
119     }
120     
121     public void writeLine(final String s) throws IOException {
122         if (s == null) {
123             return;
124         }
125         if (s.length() > 0) {
126             write(s.getBytes(this.charset));
127         }
128         write(CRLF);
129     }
130     
131     public void writeLine(final CharArrayBuffer s) throws IOException {
132         if (s == null) {
133             return;
134         }
135         if (this.ascii) {
136             int off = 0;
137             int remaining = s.length();
138             while (remaining > 0) {
139                 int chunk = this.buffer.capacity() - this.buffer.length();
140                 chunk = Math.min(chunk, remaining);
141                 if (chunk > 0) {
142                     this.buffer.append(s, off, chunk);
143                 }
144                 if (this.buffer.isFull()) {
145                     flushBuffer();
146                 }
147                 off += chunk;
148                 remaining -= chunk;
149             }
150         } else {
151             // This is VERY memory inefficient, BUT since non-ASCII charsets are 
152             // NOT meant to be used anyway, there's no point optimizing it
153             byte[] tmp = s.toString().getBytes(this.charset);
154             write(tmp);
155         }
156         write(CRLF);
157     }
158     
159     public void reset(final HttpParams params) {
160         this.charset = HttpProtocolParams.getHttpElementCharset(params); 
161         this.ascii = this.charset.equalsIgnoreCase(HTTP.US_ASCII)
162                      || this.charset.equalsIgnoreCase(HTTP.ASCII);
163     }
164     
165 }