1 package org.simantics.acorn.lru;
3 import java.nio.file.Path;
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;
10 import gnu.trove.list.array.TByteArrayList;
12 public class FileInfo extends LRUObject<String, FileInfo> {
14 private TByteArrayList bytes;
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);
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);
29 public byte[] getResourceFile() throws AcornAccessVerificationException, IllegalAcornStateException {
31 if(VERIFY) verifyAccess();
34 return bytes.toArray();
38 public ResourceSegment getResourceSegment(final byte[] clusterUID, final int resourceIndex, final long segmentOffset, short segmentSize) throws AcornAccessVerificationException, IllegalAcornStateException {
40 if(VERIFY) verifyAccess();
44 int segSize = segmentSize;
47 if (segmentSize == -1)
48 segSize = Math.min(65535, bytes.size());
50 final long valueSize = bytes.size();
51 final byte[] segment = bytes.toArray((int) segmentOffset, segSize);
53 return new ResourceSegment() {
56 public long getValueSize() {
61 public byte[] getSegment() {
66 public int getResourceIndex() {
71 public long getOffset() {
76 public byte[] getClusterId() {
80 } catch (Throwable t) {
81 throw new IllegalAcornStateException(t);
85 public void updateData(byte[] newBytes, long offset, long pos, long size) throws AcornAccessVerificationException, IllegalAcornStateException {
87 if(VERIFY) verifyAccess();
91 bytes.remove((int)offset, (int)(bytes.size()-offset));
93 bytes.fill((int) (offset + size), (int) (offset + size), (byte) 0);
94 bytes.set((int) offset, newBytes, (int) pos, (int) size);
102 public Pair<byte[], Integer> toBytes() {
103 byte[] result = bytes.toArray();
105 return Pair.make(result, result.length);
109 protected void release() {
114 public void fromFile(byte[] data) {
115 bytes = new TByteArrayList(data);
119 protected String getExtension() {
124 protected boolean overwrite() {