1 package org.simantics.db.common.utils;
\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
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
26 * Converts byte[] literals to files and back. These utilities are not usable
\r
27 * for literals of any other type than byte[].
\r
29 public class LiteralFileUtil {
\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
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
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
51 public static void copyRandomAccessBinaryToFile(ReadGraph g, Resource source, File target) throws DatabaseException, IOException {
\r
52 OutputStream stream = new FileOutputStream(target);
\r
54 RandomAccessBinary rab = g.getRandomAccessBinary(source);
\r
55 // Skip byte array length data according to databoard byte[] serialization rules.
\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
69 public static void copyRandomAccessBinaryFromFile(File source, RandomAccessBinary rab) throws IOException {
\r
70 ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[65536]);
\r
72 FileInputStream stream = new FileInputStream(source);
\r
73 FileChannel channel = stream.getChannel();
\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
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
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
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
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
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
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
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
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
134 g.claimValue(target, data, Bindings.BYTE_ARRAY);
\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
142 g.claimLiteral(subject, predicate, data, Bindings.BYTE_ARRAY);
\r
150 * @throws DatabaseException
\r
151 * @throws IOException
\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
161 InputStream stream = new FileInputStream(source);
\r
163 copyStreamToRandomAccessBinary(g, stream, target);
\r
169 public static void copyByteArrayFromStream(WriteGraph g, InputStream source, Resource target) throws IOException, DatabaseException {
\r
170 byte[] buffer = new byte[0x10000];
\r
173 int count = source.read(buffer, pos, buffer.length - pos);
\r
177 if(pos == buffer.length)
\r
178 buffer = Arrays.copyOf(buffer, (pos * 3) / 2);
\r
180 buffer = Arrays.copyOf(buffer, pos);
\r
181 g.claimValue(target, buffer, Bindings.BYTE_ARRAY);
\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
192 int count = source.read(buffer, 0, buffer.length);
\r
195 rab.write(buffer, 0, count);
\r
199 rab.writeInt(length);
\r