]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/LiteralFileUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / LiteralFileUtil.java
1 package org.simantics.db.common.utils;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileInputStream;\r
5 import java.io.FileOutputStream;\r
6 import java.io.IOException;\r
7 import java.io.InputStream;\r
8 import java.io.OutputStream;\r
9 import java.nio.ByteBuffer;\r
10 import java.nio.channels.FileChannel;\r
11 import java.util.Arrays;\r
12 \r
13 import org.simantics.databoard.Bindings;\r
14 import org.simantics.databoard.Files;\r
15 import org.simantics.databoard.binding.Binding;\r
16 import org.simantics.databoard.serialization.Serializer;\r
17 import org.simantics.databoard.type.Datatype;\r
18 import org.simantics.databoard.util.binary.RandomAccessBinary;\r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.WriteGraph;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.layer0.Layer0;\r
24 \r
25 /**\r
26  * Converts byte[] literals to files and back. These utilities are not usable\r
27  * for literals of any other type than byte[].\r
28  */\r
29 public class LiteralFileUtil {\r
30 \r
31     public static void copyByteArrayToFile(ReadGraph g, Resource source, File target) throws DatabaseException, IOException {\r
32         byte[] data = g.getValue(source, Bindings.BYTE_ARRAY);\r
33         OutputStream stream = new FileOutputStream(target);\r
34         try {\r
35             // TODO: For some peculiar reason a direct stream.write(data) OOMs with approx. 70MB array\r
36             for(int pos = 0;pos < data.length;) {\r
37                 int len = Math.min(65536, data.length-pos);\r
38                 stream.write(data,pos,len);\r
39                 pos += len;\r
40             }\r
41         } finally {\r
42             stream.close();\r
43         }\r
44     }\r
45     \r
46     public static void copyByteArrayToFile(ReadGraph g, Resource subject, Resource predicate, File target) throws DatabaseException, IOException {\r
47         Resource source = g.getSingleObject(subject, predicate);\r
48         copyByteArrayToFile(g, source, target);\r
49     }\r
50 \r
51     public static void copyRandomAccessBinaryToFile(ReadGraph g, Resource source, File target) throws DatabaseException, IOException {\r
52         OutputStream stream = new FileOutputStream(target);\r
53         try {\r
54             RandomAccessBinary rab = g.getRandomAccessBinary(source);\r
55             // Skip byte array length data according to databoard byte[] serialization rules.\r
56             rab.position(4);\r
57             ByteBuffer bb =  ByteBuffer.wrap(new byte[65536]);\r
58             byte[] bbArray = bb.array();\r
59             while (rab.position() < rab.length()) {\r
60                 rab.readFully(bb, (int) Math.min(bbArray.length, rab.length() - rab.position()));\r
61                 stream.write(bbArray, 0, bb.position());\r
62                 bb.position(0);\r
63             }\r
64         } finally {\r
65             stream.close();\r
66         }\r
67     }\r
68 \r
69     public static void copyRandomAccessBinaryFromFile(File source, RandomAccessBinary rab) throws IOException {\r
70         ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[65536]);\r
71 \r
72         FileInputStream stream = new FileInputStream(source);\r
73         FileChannel channel = stream.getChannel();\r
74 \r
75         try {\r
76             rab.position(0);\r
77             rab.writeInt((int)source.length());\r
78             while(channel.read(byteBuffer) != -1) {\r
79                 byteBuffer.limit(byteBuffer.position());\r
80                 byteBuffer.position(0);\r
81                 rab.writeFully(byteBuffer);\r
82                 byteBuffer.position(0);\r
83             }\r
84         } finally {\r
85             stream.close();\r
86         }\r
87 \r
88         // Make sure the RAB file will not be longer than the\r
89         // source file due to possible previously existing data.\r
90         rab.setLength(rab.position());\r
91         rab.flush();\r
92     }\r
93 \r
94 \r
95     public static void copyRandomAccessBinaryToFile(ReadGraph g, Resource subject, Resource predicate, File target) throws DatabaseException, IOException {\r
96         Resource source = g.getSingleObject(subject, predicate);\r
97         copyRandomAccessBinaryToFile(g, source, target);\r
98     }\r
99 \r
100     public static void copyToFileWithBinding(ReadGraph g, Resource source, File target, Binding binding) throws DatabaseException, IOException {\r
101         // Datatyyppi tieto on jo resurssissa, eik�? \r
102         // Sidonnan saisi Bindings.getBinding( type )\r
103         Object value = g.getValue(source, binding);\r
104         Serializer s = Bindings.getSerializerUnchecked(binding);\r
105         s.serialize(value, target);\r
106     }\r
107 \r
108     public static void copyToFileWithBinding(ReadGraph g, Resource subject, Resource predicate, File target, Binding binding) throws DatabaseException, IOException {\r
109         Resource source = g.getSingleObject(subject, predicate);\r
110         copyToFileWithBinding(g, source, target, binding);\r
111     }\r
112 \r
113     public static void copyToFileAsVariant(ReadGraph g, Resource source, File target, Binding binding) throws DatabaseException, IOException {\r
114         Object value = g.getValue(source, binding);\r
115         Files.createFile(target, binding, value);\r
116     }\r
117 \r
118     public static void copyToFileAsVariant(ReadGraph g, Resource source, File target) throws DatabaseException, IOException {\r
119         Datatype type = g.getDataType(source);\r
120         Binding binding = Bindings.getBinding(type);\r
121         copyToFileAsVariant(g, source, target, binding);\r
122     }\r
123 \r
124     public static void copyToFileAsVariant(ReadGraph g, Resource subject, Resource predicate, File target) throws DatabaseException, IOException {\r
125         Resource source = g.getSingleObject(subject, predicate);\r
126         copyToFileAsVariant(g, source, target);\r
127     }\r
128 \r
129     public static void copyByteArrayFromFile(WriteGraph g, File source, Resource target) throws DatabaseException, IOException {\r
130         InputStream stream = new FileInputStream(source);\r
131         byte[] data = new byte[(int)source.length()];\r
132         stream.read(data);\r
133         stream.close();\r
134         g.claimValue(target, data, Bindings.BYTE_ARRAY);\r
135     }\r
136 \r
137     public static void copyByteArrayFromFile(WriteGraph g, File source, Resource subject, Resource predicate) throws DatabaseException, IOException {\r
138         InputStream stream = new FileInputStream(source);\r
139         byte[] data = new byte[(int)source.length()];\r
140         stream.read(data);\r
141         stream.close();\r
142         g.claimLiteral(subject, predicate, data, Bindings.BYTE_ARRAY);\r
143     }\r
144 \r
145     /**\r
146      * @param g\r
147      * @param source\r
148      * @param subject\r
149      * @param predicate\r
150      * @throws DatabaseException\r
151      * @throws IOException\r
152      */\r
153     public static void streamingCopyByteArrayFromFile(WriteGraph g, File source, Resource subject, Resource predicate) throws DatabaseException, IOException {\r
154         Resource target = g.getPossibleObject(subject, predicate);\r
155         if (target == null) {\r
156             Layer0 L0 = Layer0.getInstance(g);\r
157             target = g.newResource();\r
158             g.claim(target, L0.InstanceOf, null, L0.ByteArray);\r
159             g.claim(subject, predicate, target);\r
160         }\r
161         InputStream stream = new FileInputStream(source);\r
162         try {\r
163             copyStreamToRandomAccessBinary(g, stream, target);\r
164         } finally {\r
165             stream.close();\r
166         }\r
167     }\r
168 \r
169     public static void copyByteArrayFromStream(WriteGraph g, InputStream source, Resource target) throws IOException, DatabaseException {\r
170         byte[] buffer = new byte[0x10000];\r
171         int pos = 0;\r
172         while(true) {\r
173             int count = source.read(buffer, pos, buffer.length - pos);\r
174             if(count <= 0)\r
175                 break;\r
176             pos += count;\r
177             if(pos == buffer.length)\r
178                 buffer = Arrays.copyOf(buffer, (pos * 3) / 2);\r
179         }\r
180         buffer = Arrays.copyOf(buffer, pos);\r
181         g.claimValue(target, buffer, Bindings.BYTE_ARRAY);\r
182     }\r
183 \r
184     public static void copyStreamToRandomAccessBinary(WriteGraph g, InputStream source, Resource target) throws IOException, DatabaseException {\r
185         RandomAccessBinary rab = g.getRandomAccessBinary(target);\r
186         byte[] buffer = new byte[0x10000];\r
187         if (rab.length() < 4)\r
188             rab.setLength(4);\r
189         rab.position(4);\r
190         int length = 0;\r
191         while (true) {\r
192             int count = source.read(buffer, 0, buffer.length);\r
193             if (count <= 0)\r
194                 break;\r
195             rab.write(buffer, 0, count);\r
196             length += count;\r
197         }\r
198         rab.position(0);\r
199         rab.writeInt(length);\r
200         rab.flush();\r
201     }\r
202 \r
203 }\r