Java InputStreamが与えられた場合、ストリーム内の現在のオフセットをどのように判断できますか?

StackOverflow https://stackoverflow.com/questions/240294

  •  04-07-2019
  •  | 
  •  

質問

ストリームの開始点から読み取られたバイト数を通知する汎用の再利用可能な getPosition()メソッドのようなものが欲しい。理想的には、これをすべてのInputStreamsで処理することを好むので、異なるソースから取得するときにそれらを1つずつラップする必要はありません。

そのような獣は存在しますか?そうでない場合、誰でもカウント InputStream の既存の実装を推奨できますか?

他のヒント

これを実装するには、 java.io で確立されたDecoratorパターンに従う必要があります。

ここで試してみましょう:

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 の位置変数をいじりましたが、1つのスレッドがストリームを操作し、ロックを放棄せずにその位置を照会できるため、おそらく同期が最適です。

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

いいえ。 InputStream は、潜在的に無限の量のデータを処理することを目的としているため、カウンターが邪魔になります。それらをすべてラップすることに加えて、アスペクトを使って何かをすることができるかもしれません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top