View Javadoc

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 org.apache.http.impl.conn;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.nio.charset.Charset;
34  import java.nio.charset.CharsetDecoder;
35  
36  import org.apache.http.config.MessageConstraints;
37  import org.apache.http.impl.io.HttpTransportMetricsImpl;
38  import org.apache.http.impl.io.SessionInputBufferImpl;
39  
40  /**
41   * {@link org.apache.http.io.SessionInputBuffer} mockup implementation.
42   */
43  public class SessionInputBufferMock extends SessionInputBufferImpl {
44  
45      public static final int BUFFER_SIZE = 16;
46  
47      public SessionInputBufferMock(
48              final InputStream instream,
49              final int buffersize,
50              final MessageConstraints constrains,
51              final CharsetDecoder decoder) {
52          super(new HttpTransportMetricsImpl(), buffersize, -1, constrains, decoder);
53          bind(instream);
54      }
55  
56      public SessionInputBufferMock(
57              final InputStream instream,
58              final int buffersize) {
59          this(instream, buffersize, null, null);
60      }
61  
62      public SessionInputBufferMock(
63              final byte[] bytes,
64              final int buffersize,
65              final MessageConstraints constrains,
66              final CharsetDecoder decoder) {
67          this(new ByteArrayInputStream(bytes), buffersize, constrains, decoder);
68      }
69  
70      public SessionInputBufferMock(
71              final byte[] bytes,
72              final int buffersize,
73              final MessageConstraints constrains) {
74          this(new ByteArrayInputStream(bytes), buffersize, constrains, null);
75      }
76  
77      public SessionInputBufferMock(
78              final byte[] bytes,
79              final int buffersize) {
80          this(new ByteArrayInputStream(bytes), buffersize);
81      }
82  
83      public SessionInputBufferMock(
84              final byte[] bytes) {
85          this(bytes, BUFFER_SIZE);
86      }
87  
88      public SessionInputBufferMock(
89              final byte[] bytes, final Charset charset) {
90          this(bytes, BUFFER_SIZE, null, charset != null ? charset.newDecoder() : null);
91      }
92  
93      public SessionInputBufferMock(
94              final byte[] bytes,
95              final CharsetDecoder decoder) {
96          this(bytes, BUFFER_SIZE, null, decoder);
97      }
98  
99      public SessionInputBufferMock(
100             final String s,
101             final Charset charset) {
102         this(s.getBytes(charset), charset);
103     }
104 
105     @Override
106     public boolean isDataAvailable(final int timeout) throws IOException {
107         return true;
108     }
109 
110 }