View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   * 1535756    2006-08-07  lupusalex    Make code warning free
19   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
20   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
21   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   */
23  package org.metricshub.wbem.sblim.cimclient.internal.http.io;
24  
25  /*-
26   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27   * WBEM Java Client
28   * ჻჻჻჻჻჻
29   * Copyright 2023 - 2025 MetricsHub
30   * ჻჻჻჻჻჻
31   * Licensed under the Apache License, Version 2.0 (the "License");
32   * you may not use this file except in compliance with the License.
33   * You may obtain a copy of the License at
34   *
35   *      http://www.apache.org/licenses/LICENSE-2.0
36   *
37   * Unless required by applicable law or agreed to in writing, software
38   * distributed under the License is distributed on an "AS IS" BASIS,
39   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40   * See the License for the specific language governing permissions and
41   * limitations under the License.
42   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
43   */
44  
45  import java.io.FilterInputStream;
46  import java.io.IOException;
47  import java.io.InputStream;
48  import org.metricshub.wbem.sblim.cimclient.internal.http.HttpClient;
49  
50  /**
51   * Class KeepAliveInputStream implements an input stream for connections that
52   * keep iAlive after a request is completed
53   *
54   */
55  public class KeepAliveInputStream extends FilterInputStream {
56  	private HttpClient iClient;
57  
58  	/**
59  	 * Ctor.
60  	 *
61  	 * @param pStream
62  	 *            The underlying stream
63  	 * @param pClient
64  	 *            The associated client.
65  	 */
66  	public KeepAliveInputStream(InputStream pStream, HttpClient pClient) {
67  		super(pStream);
68  		this.iClient = pClient;
69  	}
70  
71  	@Override
72  	public int read() throws IOException {
73  		int i = super.read();
74  		if (i == -1 && this.iClient != null) {
75  			this.iClient.streamFinished();
76  			this.iClient = null;
77  		}
78  		return i;
79  	}
80  
81  	@Override
82  	public int read(byte buf[]) throws IOException {
83  		return read(buf, 0, buf.length);
84  	}
85  
86  	@Override
87  	public int read(byte buf[], int off, int len) throws IOException {
88  		int i = super.read(buf, off, len);
89  		if (i == -1 && this.iClient != null) {
90  			this.iClient.streamFinished();
91  			this.iClient = null;
92  		}
93  		return i;
94  	}
95  
96  	@Override
97  	public long skip(long len) throws IOException {
98  		long i = super.skip(len);
99  
100 		if (i == -1 && this.iClient != null) {
101 			this.iClient.streamFinished();
102 			this.iClient = null;
103 		}
104 		return i;
105 	}
106 
107 	@Override
108 	public void close() throws IOException {
109 		super.close();
110 		if (this.iClient != null) {
111 			this.iClient.streamFinished();
112 			this.iClient = null;
113 		}
114 	}
115 }