16
16
17
17
package io .objectbox ;
18
18
19
+ import org .greenrobot .essentials .io .IoUtils ;
20
+
21
+ import java .io .BufferedInputStream ;
22
+ import java .io .BufferedOutputStream ;
19
23
import java .io .File ;
24
+ import java .io .FileInputStream ;
25
+ import java .io .FileNotFoundException ;
26
+ import java .io .FileOutputStream ;
27
+ import java .io .InputStream ;
28
+ import java .io .OutputStream ;
20
29
import java .lang .reflect .Method ;
21
30
import java .util .ArrayList ;
22
31
import java .util .List ;
26
35
27
36
import io .objectbox .annotation .apihint .Experimental ;
28
37
import io .objectbox .annotation .apihint .Internal ;
38
+ import io .objectbox .exception .DbException ;
29
39
import io .objectbox .ideasonly .ModelUpdate ;
30
40
31
41
/**
@@ -80,6 +90,7 @@ public class BoxStoreBuilder {
80
90
TxCallback failedReadTxAttemptCallback ;
81
91
82
92
final List <EntityInfo > entityInfoList = new ArrayList <>();
93
+ private Factory <InputStream > initialDbFileFactory ;
83
94
84
95
/** Not for application use. */
85
96
public static BoxStoreBuilder createDebugWithoutModel () {
@@ -306,6 +317,22 @@ public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback failedReadTxAttemp
306
317
return this ;
307
318
}
308
319
320
+ @ Experimental
321
+ public BoxStoreBuilder initialDbFile (final File initialDbFile ) {
322
+ return initialDbFile (new Factory <InputStream >() {
323
+ @ Override
324
+ public InputStream provide () throws FileNotFoundException {
325
+ return new FileInputStream (initialDbFile );
326
+ }
327
+ });
328
+ }
329
+
330
+ @ Experimental
331
+ public BoxStoreBuilder initialDbFile (Factory <InputStream > initialDbFileFactory ) {
332
+ this .initialDbFileFactory = initialDbFileFactory ;
333
+ return this ;
334
+ }
335
+
309
336
/**
310
337
* Builds a {@link BoxStore} using any given configuration.
311
338
*/
@@ -314,9 +341,35 @@ public BoxStore build() {
314
341
name = dbName (name );
315
342
directory = getDbDir (baseDirectory , name );
316
343
}
344
+ checkProvisionInitialDbFile ();
317
345
return new BoxStore (this );
318
346
}
319
347
348
+ private void checkProvisionInitialDbFile () {
349
+ if (initialDbFileFactory != null ) {
350
+ String dataDir = BoxStore .getCanonicalPath (directory );
351
+ File file = new File (dataDir , "data.mdb" );
352
+ if (!file .exists ()) {
353
+ InputStream in = null ;
354
+ OutputStream out = null ;
355
+ try {
356
+ in = initialDbFileFactory .provide ();
357
+ if (in == null ) {
358
+ throw new DbException ("Factory did not provide a resource" );
359
+ }
360
+ in = new BufferedInputStream (in );
361
+ out = new BufferedOutputStream (new FileOutputStream (file ));
362
+ IoUtils .copyAllBytes (in , out );
363
+ } catch (Exception e ) {
364
+ throw new DbException ("Could not provision initial data file" , e );
365
+ } finally {
366
+ IoUtils .safeClose (out );
367
+ IoUtils .safeClose (in );
368
+ }
369
+ }
370
+ }
371
+ }
372
+
320
373
static File getDbDir (@ Nullable File baseDirectoryOrNull , @ Nullable String nameOrNull ) {
321
374
String name = dbName (nameOrNull );
322
375
if (baseDirectoryOrNull != null ) {
0 commit comments