package org.h2.store;
import java.io.IOException;
import java.io.InputStream;
import org.h2.message.DbException;
import org.h2.value.ValueLobDb;
class LobStorageRemoteInputStream extends InputStream {
private final DataHandler handler;
private final long lob;
private final byte[] hmac;
private long pos;
private long remainingBytes;
public LobStorageRemoteInputStream(DataHandler handler, ValueLobDb lob,
byte[] hmac, long byteCount) {
this.handler = handler;
this.lob = lob.getLobId();
this.hmac = hmac;
remainingBytes = byteCount;
}
@Override
public int read() throws IOException {
byte[] buff = new byte[1];
int len = read(buff, 0, 1);
return len < 0 ? len : (buff[0] & 255);
}
@Override
public int read(byte[] buff) throws IOException {
return read(buff, 0, buff.length);
}
@Override
public int read(byte[] buff, int off, int length) throws IOException {
if (length == 0) {
return 0;
}
length = (int) Math.min(length, remainingBytes);
if (length == 0) {
return -1;
}
try {
length = handler.readLob(lob, hmac, pos, buff, off, length);
} catch (DbException e) {
throw DbException.convertToIOException(e);
}
if (length == 0) {
return -1;
}
remainingBytes -= length;
pos += length;
return length;
}
@Override
public long skip(long n) {
remainingBytes -= n;
pos += n;
return n;
}
}