]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/FileIO.java
aa71732855bab8a6108d9fa2d837ebb3798a2839
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / FileIO.java
1 package org.simantics.acorn;
2
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;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.simantics.databoard.file.RuntimeIOException;
22
23 public class FileIO {
24         
25     private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];
26     
27     private static final Set<OpenOption> CREATE_OPTIONS = new HashSet<>(2);
28     private static final Set<OpenOption> APPEND_OPTIONS = new HashSet<>(1);
29     
30     static {
31         CREATE_OPTIONS.add(StandardOpenOption.WRITE);
32         CREATE_OPTIONS.add(StandardOpenOption.CREATE);
33         
34         APPEND_OPTIONS.add(StandardOpenOption.APPEND);
35     }
36     
37         private Path path;
38         private int writePosition = 0;
39
40         private FileIO(Path path) {
41                 this.path = path;
42         }
43         
44         private static Map<Path, FileIO> map = new HashMap<Path, FileIO>();
45         
46         public static FileIO get(Path path) {
47                 synchronized(map) {
48                         FileIO existing = map.get(path);
49                         if(existing == null) {
50                                 existing = new FileIO(path);
51                                 map.put(path, existing);
52                         }
53                         return existing;
54                 }
55         }
56         
57         //private static final boolean TRACE_SWAP = false;
58         private static final boolean TRACE_PERF = false;
59
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;
65                 
66                 ByteBuffer bb = ByteBuffer.wrap(bytes, 0, length);
67                 try (FileChannel fc = FileChannel.open(path, options, NO_ATTRIBUTES)) {
68             fc.write(bb);
69                 }
70                 
71         writePosition += length;
72                 if(TRACE_PERF) {
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");
76                 }
77                 return result;
78         }
79
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);
85             int read = 0;
86             while (read < length) {
87                 read += channel.read(buf);
88             }
89             byte[] result = buf.array();
90             if (result.length != length)
91                 System.err.println("faa");
92             if (TRACE_PERF) {
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");
96             }
97             return result;
98         }
99     }
100
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")) {
104                         raf.getFD().sync();
105                 }
106         }
107
108         static void uncheckedSyncPath(Path f) {
109                 try {
110                         syncPath(f);
111                 } catch (IOException e) {
112                         throw new RuntimeIOException(e);
113                 }
114         }
115
116         public static void main(String[] args) throws Exception {
117
118                 byte[] buf = new byte[1024*1024];
119                 
120                 long s = System.nanoTime();
121                 
122                 Path test = Paths.get("e:/work/test.dat");
123                 OutputStream fs = Files.newOutputStream(test);
124                 OutputStream os = new BufferedOutputStream(fs, 128*1024);
125                 
126                 for(int i=0;i<40;i++) {
127                         os.write(buf);
128                 }
129                 
130                 os.flush();
131                 //fs.getFD().sync();
132                 os.close();
133                 
134                 syncPath(test);
135                 
136                 long duration = System.nanoTime()-s;
137                 System.err.println("Took " + 1e-6*duration + "ms.");
138                 
139                 
140         }
141         
142 }