forked from JOSM/josm-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix AIOOBE when reading more data from a file than indicated by file …
…header git-svn-id: https://josm.openstreetmap.de/osmsvn/applications/editors/josm/plugins@36160 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
- Loading branch information
taylor.smock
committed
Oct 5, 2023
1 parent
d963a33
commit dc2819a
Showing
2 changed files
with
129 additions
and
5 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
121 changes: 121 additions & 0 deletions
121
FIT/src/main/java/org/openstreetmap/josm/plugins/fit/lib/utils/CountingInputStream.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,121 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.fit.lib.utils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Count the bytes read from a stream | ||
*/ | ||
public class CountingInputStream extends InputStream { | ||
private final InputStream inputStream; | ||
private long bytesRead; | ||
private long mark; | ||
|
||
/** | ||
* Create a new {@link CountingInputStream} | ||
* @param inputStream The stream to count bytes read from | ||
*/ | ||
public CountingInputStream(InputStream inputStream) { | ||
this.inputStream = inputStream; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
final var read = this.inputStream.read(); | ||
if (read != -1) { | ||
bytesRead++; | ||
} | ||
return read; | ||
} | ||
|
||
/** | ||
* Get the number of bytes read | ||
* @return The bytes read | ||
*/ | ||
public long bytesRead() { | ||
return this.bytesRead; | ||
} | ||
|
||
@Override | ||
public int read(byte[] bytes) throws IOException { | ||
final int read = this.inputStream.read(bytes); | ||
if (read != -1) { | ||
this.bytesRead += read; | ||
} | ||
return read; | ||
} | ||
|
||
@Override | ||
public int read(byte[] bytes, int off, int len) throws IOException { | ||
final int read = this.inputStream.read(bytes, off, len); | ||
if (read != -1) { | ||
this.bytesRead += read; | ||
} | ||
return read; | ||
} | ||
|
||
@Override | ||
public byte[] readAllBytes() throws IOException { | ||
final var allBytes = this.inputStream.readAllBytes(); | ||
this.bytesRead += allBytes.length; | ||
return allBytes; | ||
} | ||
|
||
@Override | ||
public byte[] readNBytes(int len) throws IOException { | ||
final var nBytes = this.inputStream.readNBytes(len); | ||
this.bytesRead += nBytes.length; | ||
return nBytes; | ||
} | ||
|
||
@Override | ||
public int readNBytes(byte[] b, int off, int len) throws IOException { | ||
final var read = this.inputStream.readNBytes(b, off, len); | ||
if (read != -1) { | ||
this.bytesRead += read; | ||
} | ||
return read; | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
final var read = this.inputStream.skip(n); | ||
this.bytesRead += read; | ||
return read; | ||
} | ||
|
||
@Override | ||
public void skipNBytes(long n) throws IOException { | ||
this.inputStream.skipNBytes(n); | ||
this.bytesRead += n; // This might not be accurate... | ||
} | ||
|
||
@Override | ||
public int available() throws IOException { | ||
return this.inputStream.available(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
this.inputStream.close(); | ||
} | ||
|
||
@Override | ||
public void mark(int readlimit) { | ||
this.inputStream.mark(readlimit); | ||
this.mark = this.bytesRead; | ||
} | ||
|
||
@Override | ||
public void reset() throws IOException { | ||
this.inputStream.reset(); | ||
this.bytesRead = this.mark; | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return this.inputStream.markSupported(); | ||
} | ||
} |