mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
34 lines
711 B
Java
34 lines
711 B
Java
package org.briarproject.api.serial;
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.briarproject.api.FormatException;
|
|
|
|
/**
|
|
* A consumer that counts the number of bytes consumed and throws a
|
|
* FormatException if the count exceeds a given limit.
|
|
*/
|
|
public class CountingConsumer implements Consumer {
|
|
|
|
private final long limit;
|
|
private long count = 0;
|
|
|
|
public CountingConsumer(long limit) {
|
|
this.limit = limit;
|
|
}
|
|
|
|
public long getCount() {
|
|
return count;
|
|
}
|
|
|
|
public void write(byte b) throws IOException {
|
|
count++;
|
|
if(count > limit) throw new FormatException();
|
|
}
|
|
|
|
public void write(byte[] b, int off, int len) throws IOException {
|
|
count += len;
|
|
if(count > limit) throw new FormatException();
|
|
}
|
|
}
|