mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-26 01:05:38 +00:00
https://github.com/itplr-kosit/validator/issues/57 fix read stream
This commit is contained in:
parent
3cb1037771
commit
65f6914c3c
3 changed files with 64 additions and 5 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package de.kosit.validationtool.impl.input;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
|
@ -45,6 +46,35 @@ public class StreamHelper {
|
|||
|
||||
}
|
||||
|
||||
private static class PeekableInputStream extends BufferedInputStream {
|
||||
|
||||
public PeekableInputStream(final InputStream in) {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int available() throws IOException {
|
||||
int count = super.available();
|
||||
if (count == 0) {
|
||||
count = peek();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private int peek() throws IOException {
|
||||
try {
|
||||
mark(2);
|
||||
read();
|
||||
read();
|
||||
reset();
|
||||
} catch (final IOException e) {
|
||||
return 0;
|
||||
}
|
||||
return super.available();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("squid:S4929") // efficient read is done by internally used stream
|
||||
private static class CountInputStream extends FilterInputStream {
|
||||
|
||||
|
|
@ -103,6 +133,10 @@ public class StreamHelper {
|
|||
return new DigestingInputStream(input, stream, createDigest(digestAlgorithm));
|
||||
}
|
||||
|
||||
public static BufferedInputStream wrapPeekable(final InputStream stream) {
|
||||
return new PeekableInputStream(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drains the {@link Input} without further processing. This is useful to computing hashcode etc.
|
||||
*
|
||||
|
|
@ -129,8 +163,10 @@ public class StreamHelper {
|
|||
public static void drain(final InputStream input) throws IOException {
|
||||
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||
|
||||
// noinspection unused
|
||||
int n;
|
||||
|
||||
// noinspection StatementWithEmptyBody,UnusedAssignment
|
||||
while (EOF != (n = input.read(buffer))) {
|
||||
// nothing
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue