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;
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,
90              final MessageConstraints constrains,
91              final Charset charset) {
92          this(bytes, BUFFER_SIZE, constrains, charset != null ? charset.newDecoder() : null);
93      }
94  
95      public SessionInputBufferMock(
96              final byte[] bytes,
97              final Charset charset) {
98          this(bytes, null, charset);
99      }
100 
101     public SessionInputBufferMock(
102             final byte[] bytes,
103             final CharsetDecoder decoder) {
104         this(bytes, BUFFER_SIZE, null, decoder);
105     }
106 
107     public SessionInputBufferMock(
108             final String s,
109             final MessageConstraints constrains,
110             final Charset charset) {
111         this(s.getBytes(charset), constrains, charset);
112     }
113 
114     public SessionInputBufferMock(
115             final String s,
116             final Charset charset) {
117         this(s.getBytes(charset), MessageConstraints.DEFAULT, charset);
118     }
119 
120     @Override
121     public boolean isDataAvailable(final int timeout) throws IOException {
122         return true;
123     }
124 
125 }