]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/lru/FileInfo.java
Added a function to read the module source text
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / lru / FileInfo.java
1 package org.simantics.acorn.lru;
2
3 import java.nio.file.Path;
4
5 import org.simantics.acorn.exception.AcornAccessVerificationException;
6 import org.simantics.acorn.exception.IllegalAcornStateException;
7 import org.simantics.db.Database.Session.ResourceSegment;
8 import org.simantics.utils.datastructures.Pair;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import gnu.trove.list.array.TByteArrayList;
13
14 public class FileInfo extends LRUObject<String, FileInfo> {
15
16     private static final Logger LOGGER = LoggerFactory.getLogger(FileInfo.class);
17         private TByteArrayList bytes;
18         
19         // Stub
20         public FileInfo(LRU<String, FileInfo> LRU, Path readDir, String id, int offset, int length) throws AcornAccessVerificationException {
21                 super(LRU, id, readDir, id.toString() + ".extFile", offset, length, false, false);
22                 LRU.map(this);
23         }
24         
25         // New
26         public FileInfo(LRU<String, FileInfo> LRU, String id, int size) throws AcornAccessVerificationException {
27                 super(LRU, id, LRU.getDirectory(), id.toString() + ".extFile", true, true);
28                 this.bytes = new TByteArrayList(size);
29                 LRU.insert(this, accessTime);
30         }
31
32         public byte[] getResourceFile() throws AcornAccessVerificationException, IllegalAcornStateException {
33                 
34                 if(VERIFY) verifyAccess();
35                 
36                 makeResident();
37                 return bytes.toArray();
38         }
39         
40         
41         public ResourceSegment getResourceSegment(final byte[] clusterUID, final int resourceIndex, final long segmentOffset, short segmentSize) throws AcornAccessVerificationException, IllegalAcornStateException {
42
43                 if(VERIFY) verifyAccess();
44
45                 makeResident();
46                 try {
47                         int segSize = segmentSize;
48                         if (segSize < 0)
49                                 segSize += 65536;
50                         if (segmentSize == -1)
51                                 segSize = Math.min(65535, bytes.size());
52
53                         final long valueSize = bytes.size();
54                         final byte[] segment = bytes.toArray((int) segmentOffset, segSize);
55
56                         return new ResourceSegment() {
57
58                                 @Override
59                                 public long getValueSize() {
60                                         return valueSize;
61                                 }
62
63                                 @Override
64                                 public byte[] getSegment() {
65                                         return segment;
66                                 }
67
68                                 @Override
69                                 public int getResourceIndex() {
70                                         return resourceIndex;
71                                 }
72
73                                 @Override
74                                 public long getOffset() {
75                                         return segmentOffset;
76                                 }
77
78                                 @Override
79                                 public byte[] getClusterId() {
80                                         return clusterUID;
81                                 }
82                         };
83                 } catch (Throwable t) {
84                         throw new IllegalAcornStateException(t);
85                 }
86         }
87         
88         public void updateData(byte[] newBytes, long offset, long pos, long size) throws AcornAccessVerificationException, IllegalAcornStateException {
89
90                 if(VERIFY) verifyAccess();
91                 makeResident();
92
93                 if(size == 0) {
94                         bytes.remove((int)offset, (int)(bytes.size()-offset));
95                 } else {
96                         bytes.fill((int) (offset + size), (int) (offset + size), (byte) 0);
97                         bytes.set((int) offset, newBytes, (int) pos, (int) size);
98                 }
99                 
100                 setDirty();
101                 
102         }
103         
104         @Override
105         public Pair<byte[], Integer> toBytes() {
106                 byte[] result = bytes.toArray();
107                 release();
108                 return Pair.make(result, result.length);
109         }
110         
111         @Override
112         protected void release() {
113                 bytes = null;
114         }
115
116         @Override
117         public void fromFile(byte[] data) {
118                 bytes = new TByteArrayList(data);
119         }
120
121         @Override
122         protected String getExtension() {
123                 return "extFile";
124         }
125         
126         @Override
127         protected boolean overwrite() {
128                 return true;
129         }
130
131         @Override
132         public Logger getLogger() {
133             return LOGGER;
134         }
135 }