aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2016-11-12 03:07:30 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2016-11-12 03:07:30 +0000
commit7b8f4c32d54f63a584461f1911614d960b3af874 (patch)
tree623208627273b970bfc1d29bcf4a0e5b13c1a610 /src
parent96b9c8c5dbf5ec0092919897818ce91df2cd2419 (diff)
downloadjackcess-7b8f4c32d54f63a584461f1911614d960b3af874.tar.gz
jackcess-7b8f4c32d54f63a584461f1911614d960b3af874.zip
automatically switch to read-only mode when opening a file format which does not support writing, fixes feature #34
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1055 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
-rw-r--r--src/changes/changes.xml8
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java15
-rw-r--r--src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java16
3 files changed, 32 insertions, 7 deletions
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 73cf393..3e58c16 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -4,6 +4,14 @@
<author email="javajedi@users.sf.net">Tim McCune</author>
</properties>
<body>
+ <release version="2.1.6" date="TBD">
+ <action dev="jahlborn" type="update" system="SourceForge2Features"
+ issue="34">
+ When opening a database for a format which is read-only, automatically
+ open the channel as read-only (instead of throwing an exception if
+ readOnly is false).
+ </action>
+ </release>
<release version="2.1.5" date="2016-10-03">
<action dev="jahlborn" type="update">
Change multi-value complex columns so that they return all relevant
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
index dc5ecbf..ab01d0b 100644
--- a/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/DatabaseImpl.java
@@ -389,9 +389,18 @@ public class DatabaseImpl implements Database
JetFormat jetFormat = JetFormat.getFormat(channel);
if(jetFormat.READ_ONLY) {
- throw new IOException("file format " +
- jetFormat.getPossibleFileFormats().values() +
- " does not support writing for " + mdbFile);
+
+ if(closeChannel) {
+ // we own the channel, close and re-open read only
+ ByteUtil.closeQuietly(channel);
+ channel = null;
+ readOnly = true;
+ channel = openChannel(mdbFile, readOnly);
+ } else {
+ throw new IOException("file format " +
+ jetFormat.getPossibleFileFormats().values() +
+ " does not support writing for " + mdbFile);
+ }
}
}
diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java
index 93c5ca9..4efbd60 100644
--- a/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java
+++ b/src/test/java/com/healthmarketscience/jackcess/impl/JetFormatTest.java
@@ -1,8 +1,8 @@
package com.healthmarketscience.jackcess.impl;
import java.io.File;
-import java.io.IOException;
import java.nio.channels.FileChannel;
+import java.nio.channels.NonWritableChannelException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@@ -11,6 +11,7 @@ import java.util.Set;
import com.healthmarketscience.jackcess.Database;
import static com.healthmarketscience.jackcess.Database.*;
import com.healthmarketscience.jackcess.DatabaseBuilder;
+import com.healthmarketscience.jackcess.PropertyMap;
import junit.framework.TestCase;
import static com.healthmarketscience.jackcess.TestUtil.*;
@@ -208,10 +209,17 @@ public class JetFormatTest extends TestCase {
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
Database db = null;
- IOException failure = null;
+ Exception failure = null;
try {
db = openCopy(testDB);
- } catch(IOException e) {
+
+ if(testDB.getExpectedFormat().READ_ONLY) {
+ PropertyMap props = db.getUserDefinedProperties();
+ props.put("foo", "bar");
+ props.save();
+ }
+
+ } catch(Exception e) {
failure = e;
} finally {
if(db != null) {
@@ -222,7 +230,7 @@ public class JetFormatTest extends TestCase {
if(!testDB.getExpectedFormat().READ_ONLY) {
assertNull(failure);
} else {
- assertTrue(failure.getMessage().contains("does not support writing"));
+ assertTrue(failure instanceof NonWritableChannelException);
}
}