X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.acorn%2Fsrc%2Forg%2Fsimantics%2Facorn%2FFileIO.java;h=a65d19b5b7c82770d6ce58b07b9831dcbdb51b21;hb=refs%2Fchanges%2F90%2F1690%2F3;hp=aa71732855bab8a6108d9fa2d837ebb3798a2839;hpb=463c2a8dd4b9ca623872e6520906c19c03a1c4cb;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/FileIO.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/FileIO.java index aa7173285..a65d19b5b 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/FileIO.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/FileIO.java @@ -13,47 +13,32 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.nio.file.attribute.FileAttribute; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; import org.simantics.databoard.file.RuntimeIOException; public class FileIO { - - private static final FileAttribute[] NO_ATTRIBUTES = new FileAttribute[0]; - + + private static final FileAttribute[] NO_ATTRIBUTES = {}; + private static final Set CREATE_OPTIONS = new HashSet<>(2); private static final Set APPEND_OPTIONS = new HashSet<>(1); - + static { CREATE_OPTIONS.add(StandardOpenOption.WRITE); CREATE_OPTIONS.add(StandardOpenOption.CREATE); - + APPEND_OPTIONS.add(StandardOpenOption.APPEND); } - + private Path path; private int writePosition = 0; - private FileIO(Path path) { + public FileIO(Path path) { this.path = path; } - - private static Map map = new HashMap(); - - public static FileIO get(Path path) { - synchronized(map) { - FileIO existing = map.get(path); - if(existing == null) { - existing = new FileIO(path); - map.put(path, existing); - } - return existing; - } - } - + //private static final boolean TRACE_SWAP = false; private static final boolean TRACE_PERF = false; @@ -66,15 +51,17 @@ public class FileIO { ByteBuffer bb = ByteBuffer.wrap(bytes, 0, length); try (FileChannel fc = FileChannel.open(path, options, NO_ATTRIBUTES)) { fc.write(bb); + + writePosition += length; + if(TRACE_PERF) { + long duration = System.nanoTime()-start; + double ds = 1e-9*duration; + System.err.println("Wrote " + bytes.length + " bytes @ " + 1e-6*bytes.length / ds + "MB/s"); + } + return result; + } catch (Throwable t) { + throw new IOException("An error occured file saving bytes for file " + path.toAbsolutePath().toString(), t); } - - writePosition += length; - if(TRACE_PERF) { - long duration = System.nanoTime()-start; - double ds = 1e-9*duration; - System.err.println("Wrote " + bytes.length + " bytes @ " + 1e-6*bytes.length / ds + "MB/s"); - } - return result; } public synchronized byte[] readBytes(int offset, int length) throws IOException {