storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/mint/build/aws-sdk-java/src/LimitedInputStream.java (about) 1 /* 2 * Mint, (C) 2018 Minio, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package io.minio.awssdk.tests; 19 20 import java.io.*; 21 22 // LimitedInputStream wraps a regular InputStream, calling 23 // read() will skip some bytes as configured and will also 24 // return only data with configured length 25 26 class LimitedInputStream extends InputStream { 27 28 private int skip; 29 private int length; 30 private InputStream is; 31 32 LimitedInputStream(InputStream is, int skip, int length) { 33 this.is = is; 34 this.skip = skip; 35 this.length = length; 36 } 37 38 @Override 39 public int read() throws IOException { 40 int r; 41 while (skip > 0) { 42 r = is.read(); 43 if (r < 0) { 44 throw new IOException("stream ended before being able to skip all bytes"); 45 } 46 skip--; 47 } 48 if (length == 0) { 49 return -1; 50 } 51 r = is.read(); 52 if (r < 0) { 53 throw new IOException("stream ended before being able to read all bytes"); 54 } 55 length--; 56 return r; 57 } 58 } 59 60