繁体   English   中英

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

[英]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 旨在是线程安全的,因此可以自由使用同步。 我使用了volatileAtomicLong位置变量,但同步可能是最好的,因为它允许一个线程对流进行操作并查询其位置而无需放弃锁定。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM