Java InputStream이 주어지면 스트림의 현재 오프셋을 어떻게 확인할 수 있습니까?
-
04-07-2019 - |
문제
나는 일반적이고 재사용이 가능한 것을 원합니다. getPosition()
스트림의 시작점에서 읽은 바이트 수를 알려주는 메서드입니다.이상적으로는 이것이 모든 InputStream과 함께 작동하는 것을 선호하므로 서로 다른 소스에서 가져올 때 각각을 래핑할 필요가 없습니다.
그런 짐승이 존재하는가?그렇지 않다면 누구든지 기존 계산 구현을 추천할 수 있습니까? InputStream
?
해결책
보세요 CountingInputStream Commons IO 패키지에서. 그들은 다른 유용한 입력 스트림 변형의 꽤 좋은 모음을 가지고 있습니다.
다른 팁
다음에 설정된 데코레이터 패턴을 따라야 합니다. 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();
}
아니. InputStream
잠재적으로 무한한 양의 데이터를 처리하기위한 것이므로 카운터가 방해가됩니다. 그것들을 모두 포장하는 것 외에도, 당신은 측면으로 무언가를 할 수 있습니다.
도 있습니다 CountingInputStream
안에 구아바.
Apidocs :https://google.github.io/guava/releases/19.0/api/docs/com/google/common/io/countinginputstream.html