给定Java InputStream,如何确定流中的当前偏移量?

Chr*_*s R 15 java io inputstream

我想要一个通用的,可重用的getPosition()方法,它会告诉我从流的起始点读取的字节数.理想情况下,我更喜欢使用它来处理所有的InputStream,因此当我从不同的源获取它们时,我不必包装它们中的每一个.

这样的野兽存在吗?如果没有,任何人都可以推荐现有的计数实施InputStream吗?

eri*_*son 20

您需要遵循建立的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's not allowed here. */
    if (!markSupported())
      throw new IOException("Mark not supported.");
    super.reset();
    pos = mark;
  }

}
Run Code Online (Sandbox Code Playgroud)

InputStreams旨在是线程安全的,因此可以自由地使用同步.我玩volatileAtomicLong定位变量,但同步可能是最好的,因为它允许一个线程在流上操作并查询其位置而不放弃锁.

PositionInputStream is = …
synchronized (is) {
  is.read(buf);
  pos = is.getPosition();
}
Run Code Online (Sandbox Code Playgroud)

  • @Asterios - 流*的报告位置始终正确*; `InputStreamReader`就像读取流一样.您不能指望流跟踪它的客户端正在使用的数据(即,读者`实际上已经检查过`读者`读取了多少字节).这种功能必须构建在`Reader`中. (3认同)

Joe*_*dge 14

看一下Commons IO包中的CountingInputStream.他们也有很多其他有用的InputStream变种.