All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.hutool.core.io.LimitedInputStream Maven / Gradle / Ivy

Go to download

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。

There is a newer version: 5.8.34
Show newest version
package cn.hutool.core.io;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 限制读取最大长度的{@link FilterInputStream} 实现
* 来自:https://github.com/skylot/jadx/blob/master/jadx-plugins/jadx-plugins-api/src/main/java/jadx/api/plugins/utils/LimitedInputStream.java * * @author jadx */ public class LimitedInputStream extends FilterInputStream { private final long maxSize; private long currentPos; /** * 构造 * @param in {@link InputStream} * @param maxSize 限制最大读取量,单位byte */ public LimitedInputStream(InputStream in, long maxSize) { super(in); this.maxSize = maxSize; } @Override public int read() throws IOException { final int data = super.read(); if (data != -1) { currentPos++; checkPos(); } return data; } @Override public int read(byte[] b, int off, int len) throws IOException { final int count = super.read(b, off, len); if (count > 0) { currentPos += count; checkPos(); } return count; } @Override public long skip(long n) throws IOException { final long skipped = super.skip(n); if (skipped != 0) { currentPos += skipped; checkPos(); } return skipped; } private void checkPos() { if (currentPos > maxSize) { throw new IllegalStateException("Read limit exceeded"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy