]> source.dussan.org Git - jackcess.git/commitdiff
fix channel open options for new files
authorJames Ahlborn <jtahlborn@yahoo.com>
Mon, 31 Dec 2018 14:58:03 +0000 (14:58 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Mon, 31 Dec 2018 14:58:03 +0000 (14:58 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/jdk8@1257 f203690c-595d-4dc9-a70b-905162fa7fd2

src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java

index c005651456ed3055e0f9efd2341e78112ff3da6a..1db1c3b145c3e683621b0705aa0b3dc78d300c4d 100644 (file)
@@ -204,9 +204,13 @@ public class DatabaseImpl implements Database, DateTimeContext
   /** read-only channel access mode */
   public static final OpenOption[] RO_CHANNEL_OPTS =
     {StandardOpenOption.READ};
-  /** read/write channel access mode */
+  /** read/write channel access mode for existing files */
   public static final OpenOption[] RW_CHANNEL_OPTS =
     {StandardOpenOption.READ, StandardOpenOption.WRITE};
+  /** read/write/create channel access mode for new files */
+  public static final OpenOption[] RWC_CHANNEL_OPTS =
+  {StandardOpenOption.READ, StandardOpenOption.WRITE,
+   StandardOpenOption.CREATE};
 
   /** Name of the system object that is the parent of all tables */
   private static final String SYSTEM_OBJECT_NAME_TABLES = "Tables";
@@ -388,7 +392,7 @@ public class DatabaseImpl implements Database, DateTimeContext
       readOnly |= !Files.isWritable(mdbFile);
 
       // open file channel
-      channel = openChannel(mdbFile, readOnly);
+      channel = openChannel(mdbFile, readOnly, false);
       closeChannel = true;
     }
 
@@ -459,7 +463,7 @@ public class DatabaseImpl implements Database, DateTimeContext
 
     boolean closeChannel = false;
     if(channel == null) {
-      channel = openChannel(mdbFile, false);
+      channel = openChannel(mdbFile, false, true);
       closeChannel = true;
     }
 
@@ -494,10 +498,12 @@ public class DatabaseImpl implements Database, DateTimeContext
    *            that name cannot be created, or if some other error occurs
    *            while opening or creating the file
    */
-  static FileChannel openChannel(Path mdbFile, boolean readOnly)
+  static FileChannel openChannel(
+      Path mdbFile, boolean readOnly, boolean create)
     throws IOException
   {
-    OpenOption[] opts = (readOnly ? RO_CHANNEL_OPTS : RW_CHANNEL_OPTS);
+    OpenOption[] opts = (readOnly ? RO_CHANNEL_OPTS :
+                         (create ? RWC_CHANNEL_OPTS : RW_CHANNEL_OPTS));
     return FileChannel.open(mdbFile, opts);
   }
 
index 36ab9bded64ce9c34afed372c9dbaf6302150444..66c8a715f5b731fd088f3861a7127f92f3530c7f 100644 (file)
@@ -194,7 +194,7 @@ public class JetFormatTest extends TestCase {
     for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
 
       final FileChannel channel = DatabaseImpl.openChannel(
-          testDB.dbFile.toPath(), false);
+          testDB.dbFile.toPath(), false, false);
       try {
 
         JetFormat fmtActual = JetFormat.getFormat(channel);