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