| 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.store.fs.niomem; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.ByteBuffer; |
| 10 |
import java.nio.channels.ClosedChannelException; |
| 11 |
import java.nio.channels.FileLock; |
| 12 |
import java.nio.channels.NonWritableChannelException; |
| 13 |
import org.h2.store.fs.FakeFileChannel; |
| 14 |
import org.h2.store.fs.FileBaseDefault; |
| 15 |
|
| 16 |
/** |
| 17 |
* This class represents an in-memory file. |
| 18 |
*/ |
| 19 |
class FileNioMem extends FileBaseDefault { |
| 20 |
|
| 21 |
/** |
| 22 |
* The file data. |
| 23 |
*/ |
| 24 |
final FileNioMemData data; |
| 25 |
|
| 26 |
private final boolean readOnly; |
| 27 |
private volatile boolean closed; |
| 28 |
|
| 29 |
FileNioMem(FileNioMemData data, boolean readOnly) { |
| 30 |
this.data = data; |
| 31 |
this.readOnly = readOnly; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public long size() { |
| 36 |
return data.length(); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
protected void implTruncate(long newLength) throws IOException { |
| 41 |
// compatibility with JDK FileChannel#truncate |
| 42 |
if (readOnly) { |
| 43 |
throw new NonWritableChannelException(); |
| 44 |
} |
| 45 |
if (closed) { |
| 46 |
throw new ClosedChannelException(); |
| 47 |
} |
| 48 |
if (newLength < size()) { |
| 49 |
data.touch(readOnly); |
| 50 |
data.truncate(newLength); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public int write(ByteBuffer src, long position) throws IOException { |
| 56 |
if (closed) { |
| 57 |
throw new ClosedChannelException(); |
| 58 |
} |
| 59 |
data.touch(readOnly); |
| 60 |
// offset is 0 because we start writing from src.position() |
| 61 |
long newPosition = data.readWrite(position, src, 0, src.remaining(), true); |
| 62 |
int len = (int)(newPosition - position); |
| 63 |
src.position(src.position() + len); |
| 64 |
return len; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public int read(ByteBuffer dst, long position) throws IOException { |
| 69 |
if (closed) { |
| 70 |
throw new ClosedChannelException(); |
| 71 |
} |
| 72 |
int len = dst.remaining(); |
| 73 |
if (len == 0) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
long newPos; |
| 77 |
newPos = data.readWrite(position, dst, dst.position(), len, false); |
| 78 |
len = (int) (newPos - position); |
| 79 |
if (len <= 0) { |
| 80 |
return -1; |
| 81 |
} |
| 82 |
dst.position(dst.position() + len); |
| 83 |
return len; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void implCloseChannel() throws IOException { |
| 88 |
closed = true; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void force(boolean metaData) throws IOException { |
| 93 |
// do nothing |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public FileLock tryLock(long position, long size, |
| 98 |
boolean shared) throws IOException { |
| 99 |
if (closed) { |
| 100 |
throw new ClosedChannelException(); |
| 101 |
} |
| 102 |
if (shared) { |
| 103 |
if (!data.lockShared()) { |
| 104 |
return null; |
| 105 |
} |
| 106 |
} else { |
| 107 |
if (!data.lockExclusive()) { |
| 108 |
return null; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return new FileLock(FakeFileChannel.INSTANCE, position, size, shared) { |
| 113 |
|
| 114 |
@Override |
| 115 |
public boolean isValid() { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void release() throws IOException { |
| 121 |
data.unlock(); |
| 122 |
} |
| 123 |
}; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public String toString() { |
| 128 |
return closed ? "<closed>" : data.getName(); |
| 129 |
} |
| 130 |
|
| 131 |
} |