| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, and the |
| 3 |
* EPL 1.0 (https://h2database.com/html/license.html). Initial Developer: H2 |
| 4 |
* Group |
| 5 |
*/ |
| 6 |
package org.h2.store; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import org.h2.engine.SessionRemote; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.mvstore.DataUtils; |
| 13 |
|
| 14 |
/** |
| 15 |
* An input stream used by the client side of a tcp connection to fetch LOB data |
| 16 |
* on demand from the server. |
| 17 |
*/ |
| 18 |
public class LobStorageRemoteInputStream extends InputStream { |
| 19 |
|
| 20 |
private final SessionRemote sessionRemote; |
| 21 |
|
| 22 |
/** |
| 23 |
* The lob id. |
| 24 |
*/ |
| 25 |
private final long lobId; |
| 26 |
|
| 27 |
private final byte[] hmac; |
| 28 |
|
| 29 |
/** |
| 30 |
* The position. |
| 31 |
*/ |
| 32 |
private long pos; |
| 33 |
|
| 34 |
public LobStorageRemoteInputStream(SessionRemote handler, long lobId, byte[] hmac) { |
| 35 |
this.sessionRemote = handler; |
| 36 |
this.lobId = lobId; |
| 37 |
this.hmac = hmac; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public int read() throws IOException { |
| 42 |
byte[] buff = new byte[1]; |
| 43 |
int len = read(buff, 0, 1); |
| 44 |
return len < 0 ? len : (buff[0] & 255); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public int read(byte[] buff) throws IOException { |
| 49 |
return read(buff, 0, buff.length); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public int read(byte[] buff, int off, int length) throws IOException { |
| 54 |
assert(length >= 0); |
| 55 |
if (length == 0) { |
| 56 |
return 0; |
| 57 |
} |
| 58 |
try { |
| 59 |
length = sessionRemote.readLob(lobId, hmac, pos, buff, off, length); |
| 60 |
} catch (DbException e) { |
| 61 |
throw DataUtils.convertToIOException(e); |
| 62 |
} |
| 63 |
if (length == 0) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
pos += length; |
| 67 |
return length; |
| 68 |
} |
| 69 |
|
| 70 |
} |