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;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import gnu.trove.list.array.TByteArrayList;
14 public class FileInfo extends LRUObject<String, FileInfo> {
16 private static final Logger LOGGER = LoggerFactory.getLogger(FileInfo.class);
17 private TByteArrayList bytes;
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);
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);
32 public byte[] getResourceFile() throws AcornAccessVerificationException, IllegalAcornStateException {
34 if(VERIFY) verifyAccess();
37 return bytes.toArray();
41 public ResourceSegment getResourceSegment(final byte[] clusterUID, final int resourceIndex, final long segmentOffset, short segmentSize) throws AcornAccessVerificationException, IllegalAcornStateException {
43 if(VERIFY) verifyAccess();
47 int segSize = segmentSize;
50 if (segmentSize == -1)
51 segSize = Math.min(65535, bytes.size());
53 final long valueSize = bytes.size();
54 final byte[] segment = bytes.toArray((int) segmentOffset, segSize);
56 return new ResourceSegment() {
59 public long getValueSize() {
64 public byte[] getSegment() {
69 public int getResourceIndex() {
74 public long getOffset() {
79 public byte[] getClusterId() {
83 } catch (Throwable t) {
84 throw new IllegalAcornStateException(t);
88 public void updateData(byte[] newBytes, long offset, long pos, long size) throws AcornAccessVerificationException, IllegalAcornStateException {
90 if(VERIFY) verifyAccess();
94 bytes.remove((int)offset, (int)(bytes.size()-offset));
96 bytes.fill((int) (offset + size), (int) (offset + size), (byte) 0);
97 bytes.set((int) offset, newBytes, (int) pos, (int) size);
105 public Pair<byte[], Integer> toBytes() {
106 byte[] result = bytes.toArray();
108 return Pair.make(result, result.length);
112 protected void release() {
117 public void fromFile(byte[] data) {
118 bytes = new TByteArrayList(data);
122 protected String getExtension() {
127 protected boolean overwrite() {
132 public Logger getLogger() {