[英]Given a Java InputStream, how can I determine the current offset in the stream?
我想要一個通用的、可重用的getPosition()
方法,它會告訴我從流的起點讀取的字節數。 理想情況下,我希望它與所有 InputStreams 一起使用,這樣我就不必在從不同的來源獲取它們時將它們中的每一個都包裝起來。
這樣的野獸存在嗎? 如果沒有,任何人都可以推薦計數InputStream
的現有實現嗎?
您需要遵循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's 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();
}
查看 Commons IO 包中的CountingInputStream 。 他們也有很多其他有用的 InputStream 變體。
Guava 中也有CountingInputStream
。
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
InputStream
旨在處理潛在的無限量數據,因此計數器會妨礙。 除了將它們全部包裝起來之外,您還可以對方面做一些事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.