Ответ 1
Посмотрите CountingInputStream в пакете Commons IO. Они также имеют неплохую коллекцию других полезных вариантов InputStream.
Мне бы хотелось что-то вроде универсального, повторно используемого метода getPosition()
, который скажет мне количество байтов, считанных с начальной точки потока. В идеале я бы предпочел, чтобы это работало со всеми InputStreams, так что мне не нужно обертывать каждый из них, поскольку я получаю их из разных источников.
Существует ли такой зверь? Если нет, может ли кто-нибудь порекомендовать существующую реализацию подсчета InputStream
?
Посмотрите CountingInputStream в пакете Commons IO. Они также имеют неплохую коллекцию других полезных вариантов InputStream.
Вам нужно будет следовать шаблону Decorator, установленному в java.io
, чтобы реализовать это.
Попробуем здесь:
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public final class PositionInputStream
extends FilterInputStream
{
private long pos = 0;
private long mark = 0;
public PositionInputStream(InputStream in)
{
super(in);
}
/**
* <p>Get the stream position.</p>
*
* <p>Eventually, the position will roll over to a negative number.
* Reading 1 Tb per second, this would occur after approximately three
* months. Applications should account for this possibility in their
* design.</p>
*
* @return the current stream position.
*/
public synchronized long getPosition()
{
return pos;
}
@Override
public synchronized int read()
throws IOException
{
int b = super.read();
if (b >= 0)
pos += 1;
return b;
}
@Override
public synchronized int read(byte[] b, int off, int len)
throws IOException
{
int n = super.read(b, off, len);
if (n > 0)
pos += n;
return n;
}
@Override
public synchronized long skip(long skip)
throws IOException
{
long n = super.skip(skip);
if (n > 0)
pos += n;
return n;
}
@Override
public synchronized void mark(int readlimit)
{
super.mark(readlimit);
mark = pos;
}
@Override
public synchronized void reset()
throws IOException
{
/* A call to reset can still succeed if mark is not supported, but the
* resulting stream position is undefined, so it not allowed here. */
if (!markSupported())
throw new IOException("Mark not supported.");
super.reset();
pos = mark;
}
}
InputStreams предназначены для обеспечения потоковой безопасности, что объясняет либеральное использование синхронизации. Я играл с переменными позиции volatile
и AtomicLong
, но синхронизация, вероятно, лучше всего потому, что позволяет одному потоку работать в потоке и запрашивать его позицию, не отказываясь от блокировки.
PositionInputStream is = …
synchronized (is) {
is.read(buf);
pos = is.getPosition();
}
Нет. InputStream
предназначен для обработки потенциально бесконечного количества данных, поэтому счетчик будет мешать. В дополнение к их обертыванию вы можете сделать что-то с аспектами.
Существует также CountingInputStream
в Guava.
Apidocs: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/io/CountingInputStream.html
Источник: https://github.com/google/guava/blob/master/guava/src/com/google/common/io/CountingInputStream.java