Debian Codegraph @

src/main/org/h2/store/fs/mem/FileMem.java

raw
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.mem;
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 FileMem extends FileBaseDefault {
20
21 /**
22 * The file data.
23 */
24 final FileMemData data;
25
26 private final boolean readOnly;
27 private volatile boolean closed;
28
29 FileMem(FileMemData 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 if (readOnly) {
60 throw new NonWritableChannelException();
61 }
62 int len = src.remaining();
63 if (len == 0) {
64 return 0;
65 }
66 data.touch(readOnly);
67 data.readWrite(position, src.array(),
68 src.arrayOffset() + src.position(), len, true);
69 src.position(src.position() + len);
70 return len;
71 }
72
73 @Override
74 public int read(ByteBuffer dst, long position) throws IOException {
75 if (closed) {
76 throw new ClosedChannelException();
77 }
78 int len = dst.remaining();
79 if (len == 0) {
80 return 0;
81 }
82 long newPos = data.readWrite(position, dst.array(),
83 dst.arrayOffset() + dst.position(), len, false);
84 len = (int) (newPos - position);
85 if (len <= 0) {
86 return -1;
87 }
88 dst.position(dst.position() + len);
89 return len;
90 }
91
92 @Override
93 public void implCloseChannel() throws IOException {
94 closed = true;
95 }
96
97 @Override
98 public void force(boolean metaData) throws IOException {
99 // do nothing
100 }
101
102 @Override
103 public FileLock tryLock(long position, long size,
104 boolean shared) throws IOException {
105 if (closed) {
106 throw new ClosedChannelException();
107 }
108 if (shared) {
109 if (!data.lockShared()) {
110 return null;
111 }
112 } else {
113 if (!data.lockExclusive()) {
114 return null;
115 }
116 }
117
118 return new FileLock(FakeFileChannel.INSTANCE, position, size, shared) {
119
120 @Override
121 public boolean isValid() {
122 return true;
123 }
124
125 @Override
126 public void release() throws IOException {
127 data.unlock();
128 }
129 };
130 }
131
132 @Override
133 public String toString() {
134 return closed ? "<closed>" : data.getName();
135 }
136
137 }