| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.value.lob; |
| 7 |
|
| 8 |
import java.io.BufferedInputStream; |
| 9 |
import java.io.InputStream; |
| 10 |
|
| 11 |
import org.h2.engine.Constants; |
| 12 |
import org.h2.engine.SysProperties; |
| 13 |
import org.h2.store.DataHandler; |
| 14 |
import org.h2.store.FileStore; |
| 15 |
import org.h2.store.FileStoreInputStream; |
| 16 |
import org.h2.store.fs.FileUtils; |
| 17 |
import org.h2.value.ValueLob; |
| 18 |
|
| 19 |
/** |
| 20 |
* LOB data stored in a temporary file. |
| 21 |
*/ |
| 22 |
public final class LobDataFile extends LobData { |
| 23 |
|
| 24 |
private DataHandler handler; |
| 25 |
|
| 26 |
/** |
| 27 |
* If the LOB is a temporary LOB being managed by a temporary ResultSet, it |
| 28 |
* is stored in a temporary file. |
| 29 |
*/ |
| 30 |
private final String fileName; |
| 31 |
|
| 32 |
private final FileStore tempFile; |
| 33 |
|
| 34 |
public LobDataFile(DataHandler handler, String fileName, FileStore tempFile) { |
| 35 |
this.handler = handler; |
| 36 |
this.fileName = fileName; |
| 37 |
this.tempFile = tempFile; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void remove(ValueLob value) { |
| 42 |
if (fileName != null) { |
| 43 |
if (tempFile != null) { |
| 44 |
tempFile.stopAutoDelete(); |
| 45 |
} |
| 46 |
// synchronize on the database, to avoid concurrent temp file |
| 47 |
// creation / deletion / backup |
| 48 |
synchronized (handler.getLobSyncObject()) { |
| 49 |
FileUtils.delete(fileName); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public InputStream getInputStream(long precision) { |
| 56 |
FileStore store = handler.openFile(fileName, "r", true); |
| 57 |
boolean alwaysClose = SysProperties.lobCloseBetweenReads; |
| 58 |
return new BufferedInputStream(new FileStoreInputStream(store, false, alwaysClose), |
| 59 |
Constants.IO_BUFFER_SIZE); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public DataHandler getDataHandler() { |
| 64 |
return handler; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public String toString() { |
| 69 |
return "lob-file: " + fileName; |
| 70 |
} |
| 71 |
|
| 72 |
} |