|
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.InputStream; |
|
32 |
|
33 import ch.boye.httpclientandroidlib.io.BufferInfo; |
|
34 import ch.boye.httpclientandroidlib.io.SessionInputBuffer; |
|
35 |
|
36 /** |
|
37 * Input stream that reads data without any transformation. The end of the |
|
38 * content entity is demarcated by closing the underlying connection |
|
39 * (EOF condition). Entities transferred using this input stream can be of |
|
40 * unlimited length. |
|
41 * <p> |
|
42 * Note that this class NEVER closes the underlying stream, even when close |
|
43 * gets called. Instead, it will read until the end of the stream (until |
|
44 * <code>-1</code> is returned). |
|
45 * |
|
46 * @since 4.0 |
|
47 */ |
|
48 public class IdentityInputStream extends InputStream { |
|
49 |
|
50 private final SessionInputBuffer in; |
|
51 |
|
52 private boolean closed = false; |
|
53 |
|
54 /** |
|
55 * Wraps session input stream and reads input until the the end of stream. |
|
56 * |
|
57 * @param in The session input buffer |
|
58 */ |
|
59 public IdentityInputStream(final SessionInputBuffer in) { |
|
60 super(); |
|
61 if (in == null) { |
|
62 throw new IllegalArgumentException("Session input buffer may not be null"); |
|
63 } |
|
64 this.in = in; |
|
65 } |
|
66 |
|
67 public int available() throws IOException { |
|
68 if (this.in instanceof BufferInfo) { |
|
69 return ((BufferInfo) this.in).length(); |
|
70 } else { |
|
71 return 0; |
|
72 } |
|
73 } |
|
74 |
|
75 public void close() throws IOException { |
|
76 this.closed = true; |
|
77 } |
|
78 |
|
79 public int read() throws IOException { |
|
80 if (this.closed) { |
|
81 return -1; |
|
82 } else { |
|
83 return this.in.read(); |
|
84 } |
|
85 } |
|
86 |
|
87 public int read(final byte[] b, int off, int len) throws IOException { |
|
88 if (this.closed) { |
|
89 return -1; |
|
90 } else { |
|
91 return this.in.read(b, off, len); |
|
92 } |
|
93 } |
|
94 |
|
95 } |