mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 12:05:29 -04:00
#489 Fix for Unexpected End of PackBits Stream for padded streams.
This commit is contained in:
parent
e8d1b999a2
commit
f6aa810f8b
@ -71,8 +71,6 @@ public final class PackBitsDecoder implements Decoder {
|
|||||||
private final boolean disableNoOp;
|
private final boolean disableNoOp;
|
||||||
private final byte[] sample;
|
private final byte[] sample;
|
||||||
|
|
||||||
private int leftOfRun;
|
|
||||||
private boolean splitRun;
|
|
||||||
private boolean reachedEOF;
|
private boolean reachedEOF;
|
||||||
|
|
||||||
/** Creates a {@code PackBitsDecoder}. */
|
/** Creates a {@code PackBitsDecoder}. */
|
||||||
@ -114,43 +112,22 @@ public final class PackBitsDecoder implements Decoder {
|
|||||||
* @param buffer a byte array, minimum 128 (or 129 if no-op is disabled) bytes long
|
* @param buffer a byte array, minimum 128 (or 129 if no-op is disabled) bytes long
|
||||||
* @return The number of bytes decoded
|
* @return The number of bytes decoded
|
||||||
*
|
*
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException if a problem occurs during decoding.
|
||||||
*/
|
*/
|
||||||
public int decode(final InputStream stream, final ByteBuffer buffer) throws IOException {
|
public int decode(final InputStream stream, final ByteBuffer buffer) throws IOException {
|
||||||
if (reachedEOF) {
|
if (reachedEOF) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Don't decode more than single runs, because some writers add pad bytes inside the stream...
|
// NOTE: We don't decode more than single runs, because some writers add pad bytes inside the stream...
|
||||||
while (buffer.hasRemaining()) {
|
|
||||||
int n;
|
|
||||||
|
|
||||||
if (splitRun) {
|
|
||||||
// Continue run
|
|
||||||
n = leftOfRun;
|
|
||||||
splitRun = false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Start new run
|
// Start new run
|
||||||
int b = stream.read();
|
int b = stream.read();
|
||||||
if (b < 0) {
|
if (b < 0) {
|
||||||
reachedEOF = true;
|
reachedEOF = true;
|
||||||
break;
|
return 0;
|
||||||
}
|
|
||||||
n = (byte) b;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split run at or before max
|
int n = (byte) b;
|
||||||
if (n >= 0 && n + 1 > buffer.remaining()) {
|
|
||||||
leftOfRun = n;
|
|
||||||
splitRun = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (n < 0 && -n + 1 > buffer.remaining()) {
|
|
||||||
leftOfRun = n;
|
|
||||||
splitRun = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (n >= 0) {
|
if (n >= 0) {
|
||||||
@ -173,7 +150,6 @@ public final class PackBitsDecoder implements Decoder {
|
|||||||
catch (IndexOutOfBoundsException e) {
|
catch (IndexOutOfBoundsException e) {
|
||||||
throw new DecodeException("Error in PackBits decompression, data seems corrupt", e);
|
throw new DecodeException("Error in PackBits decompression, data seems corrupt", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return buffer.position();
|
return buffer.position();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user