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 * 1565892 2006-11-28 lupusalex Make SBLIM client JSR48 compliant
19 * 2003590 2008-06-30 blaschke-oss Change licensing from CPL to EPL
20 * 2524131 2009-01-21 raman_arora Upgrade client to JDK 1.5 (Phase 1)
21 */
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.*;
46
47 /**
48 * Class BoundedInputStream implements an input stream with a maximum byte
49 * count.
50 *
51 */
52 public class BoundedInputStream extends FilterInputStream {
53 private long maxLen, used;
54
55 private boolean closed = false;
56
57 /**
58 * Ctor. Creates the stream with unlimited length.
59 *
60 * @param pStream
61 * The stream this one is build upon
62 */
63 public BoundedInputStream(InputStream pStream) {
64 this(pStream, -1);
65 }
66
67 /**
68 * Ctor.
69 *
70 * @param pStream
71 * The stream this one is build upon
72 * @param pMaximumLength
73 * The maximum number of bytes that can be read from this stream.
74 * A value of -1 represents unlimited mode.
75 */
76 public BoundedInputStream(InputStream pStream, long pMaximumLength) {
77 super(pStream);
78 this.maxLen = pMaximumLength;
79 this.used = 0;
80 this.in = pStream;
81 }
82
83 @Override
84 public int read() throws IOException {
85 if (this.maxLen > -1) {
86 if (this.used >= this.maxLen) return -1;
87
88 int value = this.in.read();
89 if (value > -1) this.used++;
90 return value;
91 }
92 return this.in.read();
93 }
94
95 @Override
96 public int read(byte buf[]) throws IOException {
97 return read(buf, 0, buf.length);
98 }
99
100 @Override
101 public int read(byte buf[], int off, int len) throws IOException {
102 if (this.closed) throw new IOException("I/O error - the stream is closed");
103
104 if (this.maxLen > -1) {
105 if (this.used >= this.maxLen) return -1;
106
107 long min = ((this.used + len) > this.maxLen) ? this.maxLen - this.used : len;
108 int total = this.in.read(buf, off, (int) min);
109 if (total > -1) this.used += total;
110 return total;
111 }
112 return this.in.read(buf, off, len);
113 }
114
115 @Override
116 public long skip(long len) throws IOException {
117 if (this.maxLen > -1) {
118 if (len >= 0) {
119 long min = ((this.used + len) > this.maxLen) ? this.maxLen - this.used : len;
120 long total = this.in.skip(min);
121 if (total > -1) {
122 this.used += total;
123 }
124 return total;
125 }
126 return -1;
127 }
128 return this.in.skip(len);
129 }
130
131 @Override
132 public int available() throws IOException {
133 if (this.maxLen > -1) {
134 return (int) (this.maxLen - this.used);
135 }
136 return this.in.available();
137 }
138
139 @Override
140 public synchronized void close() throws IOException {
141 if (this.maxLen > -1) {
142 if (!this.closed) {
143 byte[] buf = new byte[512];
144 while (read(buf, 0, buf.length) > -1) {
145 // empty
146 }
147 this.closed = true;
148 }
149 // else
150 // throw new IOException();
151 } else {
152 this.in.close();
153 }
154 }
155 }