[ACCEPTED]-Given a Java InputStream, how can I determine the current offset in the stream?-inputstream

Accepted answer
Score: 21

You'll need to follow the Decorator pattern 9 established in java.io to implement this.

Let's 8 give it a try here:

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;
  }

}

The InputStreams are 7 intended to be thread safe, so that accounts 6 for the liberal use of synchronization. I 5 played around with volatile and AtomicLong position variables, but 4 synchronization is probably best because 3 it allows one thread to operate on the stream 2 and query its position without relinquishing 1 the lock.

PositionInputStream is = …
synchronized (is) {
  is.read(buf);
  pos = is.getPosition();
}
Score: 15

Take a look at CountingInputStream in the Commons IO package. They 2 have a pretty good collection of other useful 1 InputStream variants as well.

Score: 2

No. InputStream is intended to handle potentially infinite 4 amounts of data, so a counter would get 3 in the way. In addition to wrapping them 2 all, you might be able to do something with 1 aspects.

More Related questions