Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * ====================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * 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, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 *
20 * This software consists of voluntary contributions made by many
21 * individuals on behalf of the Apache Software Foundation. For more
22 * information on the Apache Software Foundation, please see
23 * <http://www.apache.org/>.
24 *
25 */
27 package ch.boye.httpclientandroidlib.impl.conn;
29 import java.io.IOException;
31 import ch.boye.httpclientandroidlib.annotation.Immutable;
33 import ch.boye.httpclientandroidlib.io.EofSensor;
34 import ch.boye.httpclientandroidlib.io.HttpTransportMetrics;
35 import ch.boye.httpclientandroidlib.io.SessionInputBuffer;
36 import ch.boye.httpclientandroidlib.protocol.HTTP;
37 import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
39 /**
40 * Logs all data read to the wire LOG.
41 *
42 *
43 * @since 4.0
44 */
45 @Immutable
46 public class LoggingSessionInputBuffer implements SessionInputBuffer, EofSensor {
48 /** Original session input buffer. */
49 private final SessionInputBuffer in;
51 private final EofSensor eofSensor;
53 /** The wire log to use for writing. */
54 private final Wire wire;
56 private final String charset;
58 /**
59 * Create an instance that wraps the specified session input buffer.
60 * @param in The session input buffer.
61 * @param wire The wire log to use.
62 * @param charset protocol charset, <code>ASCII</code> if <code>null</code>
63 */
64 public LoggingSessionInputBuffer(
65 final SessionInputBuffer in, final Wire wire, final String charset) {
66 super();
67 this.in = in;
68 this.eofSensor = in instanceof EofSensor ? (EofSensor) in : null;
69 this.wire = wire;
70 this.charset = charset != null ? charset : HTTP.ASCII;
71 }
73 public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
74 this(in, wire, null);
75 }
77 public boolean isDataAvailable(int timeout) throws IOException {
78 return this.in.isDataAvailable(timeout);
79 }
81 public int read(byte[] b, int off, int len) throws IOException {
82 int l = this.in.read(b, off, len);
83 if (this.wire.enabled() && l > 0) {
84 this.wire.input(b, off, l);
85 }
86 return l;
87 }
89 public int read() throws IOException {
90 int l = this.in.read();
91 if (this.wire.enabled() && l != -1) {
92 this.wire.input(l);
93 }
94 return l;
95 }
97 public int read(byte[] b) throws IOException {
98 int l = this.in.read(b);
99 if (this.wire.enabled() && l > 0) {
100 this.wire.input(b, 0, l);
101 }
102 return l;
103 }
105 public String readLine() throws IOException {
106 String s = this.in.readLine();
107 if (this.wire.enabled() && s != null) {
108 String tmp = s + "\r\n";
109 this.wire.input(tmp.getBytes(this.charset));
110 }
111 return s;
112 }
114 public int readLine(final CharArrayBuffer buffer) throws IOException {
115 int l = this.in.readLine(buffer);
116 if (this.wire.enabled() && l >= 0) {
117 int pos = buffer.length() - l;
118 String s = new String(buffer.buffer(), pos, l);
119 String tmp = s + "\r\n";
120 this.wire.input(tmp.getBytes(this.charset));
121 }
122 return l;
123 }
125 public HttpTransportMetrics getMetrics() {
126 return this.in.getMetrics();
127 }
129 public boolean isEof() {
130 if (this.eofSensor != null) {
131 return this.eofSensor.isEof();
132 } else {
133 return false;
134 }
135 }
137 }