]> source.dussan.org Git - jackcess.git/commitdiff
better handling of non-existent files in open method
authorJames Ahlborn <jtahlborn@yahoo.com>
Mon, 12 Jun 2006 19:54:39 +0000 (19:54 +0000)
committerJames Ahlborn <jtahlborn@yahoo.com>
Mon, 12 Jun 2006 19:54:39 +0000 (19:54 +0000)
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@51 f203690c-595d-4dc9-a70b-905162fa7fd2

src/java/com/healthmarketscience/jackcess/Database.java
src/java/com/healthmarketscience/jackcess/JetFormat.java

index 51bc445093b00bc18817c63ede92d10ff8655477..209173ebd41d8df488233725ba56d27f050fe531 100644 (file)
@@ -177,6 +177,9 @@ public class Database {
    * @param mdbFile File containing the database
    */
   public static Database open(File mdbFile) throws IOException, SQLException {
+    if(!JetFormat.mayBeMdbFile(mdbFile)) {
+      throw new FileNotFoundException("database file is empty or nonexistent");
+    }
     return new Database(openChannel(mdbFile));
   }
   
index d6925b63fa8a91e1f92bc329443fd4ad7366c4b9..0ecfdcdbaa724f2ecef0df0fbb4c856864aa952e 100644 (file)
@@ -27,6 +27,8 @@ King of Prussia, PA 19406
 
 package com.healthmarketscience.jackcess;
 
+import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
@@ -112,13 +114,23 @@ public abstract class JetFormat {
   public final Charset CHARSET;
   
   public static final JetFormat VERSION_4 = new Jet4Format();
+
+  /**
+   * @return <code>true</code> if the given file could possibly be a database
+   *         file.
+   */
+  public static boolean mayBeMdbFile(File file) throws IOException
+  {
+    // no chance of reading the file format if none of these is true
+    return(file.exists() && file.canRead() && (file.length() >= 1L));
+  }
   
   /**
    * @return The Jet Format represented in the passed-in file
    */
   public static JetFormat getFormat(FileChannel channel) throws IOException {
     ByteBuffer buffer = ByteBuffer.allocate(1);
-    channel.read(buffer, OFFSET_VERSION);
+    int bytesRead = channel.read(buffer, OFFSET_VERSION);
     buffer.flip();
     byte version = buffer.get();
     if (version == CODE_VERSION_4) {