1616
1717package io .objectbox ;
1818
19+ import org .greenrobot .essentials .io .IoUtils ;
20+
21+ import java .io .BufferedInputStream ;
22+ import java .io .BufferedOutputStream ;
1923import 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 ;
2029import java .lang .reflect .Method ;
2130import java .util .ArrayList ;
2231import java .util .List ;
2635
2736import io .objectbox .annotation .apihint .Experimental ;
2837import io .objectbox .annotation .apihint .Internal ;
38+ import io .objectbox .exception .DbException ;
2939import io .objectbox .ideasonly .ModelUpdate ;
3040
3141/**
@@ -80,6 +90,7 @@ public class BoxStoreBuilder {
8090 TxCallback failedReadTxAttemptCallback ;
8191
8292 final List <EntityInfo > entityInfoList = new ArrayList <>();
93+ private Factory <InputStream > initialDbFileFactory ;
8394
8495 /** Not for application use. */
8596 public static BoxStoreBuilder createDebugWithoutModel () {
@@ -306,6 +317,22 @@ public BoxStoreBuilder failedReadTxAttemptCallback(TxCallback failedReadTxAttemp
306317 return this ;
307318 }
308319
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+
309336 /**
310337 * Builds a {@link BoxStore} using any given configuration.
311338 */
@@ -314,9 +341,35 @@ public BoxStore build() {
314341 name = dbName (name );
315342 directory = getDbDir (baseDirectory , name );
316343 }
344+ checkProvisionInitialDbFile ();
317345 return new BoxStore (this );
318346 }
319347
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+
320373 static File getDbDir (@ Nullable File baseDirectoryOrNull , @ Nullable String nameOrNull ) {
321374 String name = dbName (nameOrNull );
322375 if (baseDirectoryOrNull != null ) {
0 commit comments