-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
hutool-core/src/main/java/cn/hutool/core/io/LimitedInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cn.hutool.core.io; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* 限制读取最大长度的{@link FilterInputStream} 实现<br> | ||
* 来自: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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters