1 package org.simantics.acorn;
3 import java.io.BufferedOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.io.RandomAccessFile;
7 import java.nio.ByteBuffer;
8 import java.nio.channels.FileChannel;
9 import java.nio.channels.SeekableByteChannel;
10 import java.nio.file.Files;
11 import java.nio.file.OpenOption;
12 import java.nio.file.Path;
13 import java.nio.file.Paths;
14 import java.nio.file.StandardOpenOption;
15 import java.nio.file.attribute.FileAttribute;
16 import java.util.HashMap;
17 import java.util.HashSet;
21 import org.simantics.databoard.file.RuntimeIOException;
25 private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];
27 private static final Set<OpenOption> CREATE_OPTIONS = new HashSet<>(2);
28 private static final Set<OpenOption> APPEND_OPTIONS = new HashSet<>(1);
31 CREATE_OPTIONS.add(StandardOpenOption.WRITE);
32 CREATE_OPTIONS.add(StandardOpenOption.CREATE);
34 APPEND_OPTIONS.add(StandardOpenOption.APPEND);
38 private int writePosition = 0;
40 private FileIO(Path path) {
44 private static Map<Path, FileIO> map = new HashMap<Path, FileIO>();
46 public static FileIO get(Path path) {
48 FileIO existing = map.get(path);
49 if(existing == null) {
50 existing = new FileIO(path);
51 map.put(path, existing);
57 //private static final boolean TRACE_SWAP = false;
58 private static final boolean TRACE_PERF = false;
60 public synchronized int saveBytes(byte[] bytes, int length, boolean overwrite) throws IOException {
61 if(overwrite) writePosition = 0;
62 int result = writePosition;
63 long start = System.nanoTime();
64 Set<OpenOption> options = writePosition == 0 ? CREATE_OPTIONS : APPEND_OPTIONS;
66 ByteBuffer bb = ByteBuffer.wrap(bytes, 0, length);
67 try (FileChannel fc = FileChannel.open(path, options, NO_ATTRIBUTES)) {
71 writePosition += length;
73 long duration = System.nanoTime()-start;
74 double ds = 1e-9*duration;
75 System.err.println("Wrote " + bytes.length + " bytes @ " + 1e-6*bytes.length / ds + "MB/s");
80 public synchronized byte[] readBytes(int offset, int length) throws IOException {
81 long start = System.nanoTime();
82 try (SeekableByteChannel channel = Files.newByteChannel(path)) {
83 channel.position(offset);
84 ByteBuffer buf = ByteBuffer.allocate(length);
86 while (read < length) {
87 read += channel.read(buf);
89 byte[] result = buf.array();
90 if (result.length != length)
91 System.err.println("faa");
93 long duration = System.nanoTime() - start;
94 double ds = 1e-9 * duration;
95 System.err.println("Read " + result.length + " bytes @ " + 1e-6 * result.length / ds + "MB/s");
101 public static void syncPath(Path f) throws IOException {
102 // Does not seem to need 's' according to unit test in Windows
103 try (RandomAccessFile raf = new RandomAccessFile(f.toFile(), "rw")) {
108 static void uncheckedSyncPath(Path f) {
111 } catch (IOException e) {
112 throw new RuntimeIOException(e);
116 public static void main(String[] args) throws Exception {
118 byte[] buf = new byte[1024*1024];
120 long s = System.nanoTime();
122 Path test = Paths.get("e:/work/test.dat");
123 OutputStream fs = Files.newOutputStream(test);
124 OutputStream os = new BufferedOutputStream(fs, 128*1024);
126 for(int i=0;i<40;i++) {
136 long duration = System.nanoTime()-s;
137 System.err.println("Took " + 1e-6*duration + "ms.");