OSDN Git Service

[fixed] Removed input synchronization.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 18 Nov 2010 07:36:52 +0000 (07:36 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Thu, 18 Nov 2010 07:36:52 +0000 (07:36 +0000)
[fixed] Files bugs, added more tests.

backends/gdx-backend-android/src/com/badlogic/gdx/backends/android/AndroidFileHandle.java
gdx/src/com/badlogic/gdx/files/FileHandle.java
tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java

index be7e096..a87e66f 100644 (file)
@@ -43,11 +43,7 @@ public class AndroidFileHandle extends FileHandle {
                        file = new File("/" + fileName);\r
                        break;\r
                case Internal:\r
-                       try {\r
-                               assets.open(fileName).close();\r
-                       } catch (Exception ex) {\r
-                               throw new GdxRuntimeException("File not found: " + file + " (" + type + ")", ex);\r
-                       }\r
+                       ensureInternalFileExists(fileName);\r
                        file = new File(fileName);\r
                        break;\r
                case External:\r
@@ -72,11 +68,7 @@ public class AndroidFileHandle extends FileHandle {
                                throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
                        break;\r
                case Internal:\r
-                       try {\r
-                               assets.open(file.getPath()).close();\r
-                       } catch (Exception ex) {\r
-                               throw new GdxRuntimeException("File not found: " + file + " (" + type + ")", ex);\r
-                       }\r
+                       ensureInternalFileExists(file.getPath());\r
                        break;\r
                }\r
        }\r
@@ -133,12 +125,24 @@ public class AndroidFileHandle extends FileHandle {
        public boolean isDirectory () {\r
                if (type == FileType.Internal) {\r
                        try {\r
-                               assets.list(file.getPath());\r
-                               return true;\r
-                       } catch (Exception ex) {\r
+                               return assets.list(file.getPath()).length > 0;\r
+                       } catch (IOException ex) {\r
                                return false;\r
                        }\r
                }\r
                return super.isDirectory();\r
        }\r
+\r
+       private void ensureInternalFileExists (String fileName) {\r
+               try {\r
+                       assets.open(fileName).close(); // Check if file exists.\r
+               } catch (Exception ex) {\r
+                       try {\r
+                               if (assets.list(fileName).length == 0) // Try as directory.\r
+                                       throw new GdxRuntimeException("File not found: " + fileName + " (" + type + ")", ex);\r
+                       } catch (Exception ex2) {\r
+                               throw new GdxRuntimeException("Error locating file: " + fileName + " (" + type + ")", ex2);\r
+                       }\r
+               }\r
+       }\r
 }\r
index 851b25c..df6087c 100644 (file)
@@ -67,13 +67,13 @@ public abstract class FileHandle {
                this.type = type;\r
 \r
                switch (type) {\r
+               case Internal:\r
+                       if (file.exists()) break;\r
+                       // Fall through.\r
                case Classpath:\r
                        if (FileHandle.class.getResourceAsStream(file.getPath().replace('\\', '/')) == null)\r
                                throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
                        break;\r
-               case Internal:\r
-                       if (!file.exists()) throw new GdxRuntimeException("File not found: " + file + " (" + type + ")");\r
-                       break;\r
                }\r
        }\r
 \r
@@ -153,7 +153,8 @@ public abstract class FileHandle {
        }\r
 \r
        /**\r
-        * Returns true if this file is a directory. Always returns false for classpath files.\r
+        * Returns true if this file is a directory. Always returns false for classpath files. On Android, an internal handle to an\r
+        * empty directory will return false.\r
         */\r
        public boolean isDirectory () {\r
                if (type == FileType.Classpath) return false;\r
@@ -238,9 +239,12 @@ public abstract class FileHandle {
 \r
        /**\r
         * Moves this file to the specified file, overwriting the file if it already exists.\r
-        * @throw GdxRuntimeException if the destination file handle is a {@link FileType#Classpath} or {@link FileType#Internal} file.\r
+        * @throw GdxRuntimeException if the source or destination file handle is a {@link FileType#Classpath} or\r
+        *        {@link FileType#Internal} file.\r
         */\r
        public void moveTo (FileHandle dest) {\r
+               if (type == FileType.Classpath) throw new GdxRuntimeException("Cannot move a classpath file: " + file);\r
+               if (type == FileType.Internal) throw new GdxRuntimeException("Cannot move an internal file: " + file);\r
                copyTo(dest);\r
                delete();\r
        }\r
@@ -272,11 +276,13 @@ public abstract class FileHandle {
        static private boolean deleteDirectory (File file) {\r
                if (file.exists()) {\r
                        File[] files = file.listFiles();\r
-                       for (int i = 0, n = files.length; i < n; i++) {\r
-                               if (files[i].isDirectory())\r
-                                       deleteDirectory(files[i]);\r
-                               else\r
-                                       files[i].delete();\r
+                       if (files != null) {\r
+                               for (int i = 0, n = files.length; i < n; i++) {\r
+                                       if (files[i].isDirectory())\r
+                                               deleteDirectory(files[i]);\r
+                                       else\r
+                                               files[i].delete();\r
+                               }\r
                        }\r
                }\r
                return file.delete();\r
index 1556675..19fe355 100644 (file)
@@ -7,6 +7,7 @@ import java.io.File;
 import java.io.IOException;\r
 import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
+import java.io.OutputStream;\r
 import java.io.OutputStreamWriter;\r
 \r
 import com.badlogic.gdx.Files.FileType;\r
@@ -98,13 +99,97 @@ public class FilesTest extends GdxTest {
                        message += "External storage not available";\r
                }\r
 \r
-               // Can only really test external with this, since internal you can't create files, and absolute on Android you don't have\r
-               // permissions to create files.\r
-               testWriteRead("meow", FileType.External);\r
+               try {\r
+                       testInternal();\r
+                       testExternal();\r
+               } catch (IOException ex) {\r
+                       throw new RuntimeException(ex);\r
+               }\r
+       }\r
+\r
+       private void testClasspath () throws IOException {\r
+               FileHandle handle = Gdx.files.internal("com/badlogic/gdx/tests/FilesTest.class");\r
+               if (!handle.exists()) fail();\r
+               if (handle.isDirectory()) fail();\r
+               try {\r
+                       handle.delete();\r
+                       fail();\r
+               } catch (Exception expected) {\r
+               }\r
+               if (handle.list().length != 0) fail();\r
+               if (!handle.parent().exists()) fail();\r
+               try {\r
+                       handle.read().close();\r
+                       fail();\r
+               } catch (Exception ignored) {\r
+               }\r
+               FileHandle dir = Gdx.files.internal("data");\r
+               if (!dir.path().equals("data")) fail();\r
+               if (!dir.exists()) fail();\r
+               if (!dir.isDirectory()) fail();\r
+               if (dir.list().length == 0) fail();\r
+               FileHandle child = dir.child("badlogic.jpg");\r
+               if (!child.name().equals("badlogic.jpg")) fail();\r
+               if (!child.nameWithoutExtension().equals("badlogic")) fail();\r
+               if (!child.extension().equals("jpg")) fail();\r
+               if (!child.parent().exists()) fail();\r
+               FileHandle copy = Gdx.files.external("badlogic.jpg-copy");\r
+               copy.delete();\r
+               if (copy.exists()) fail();\r
+               handle.copyTo(copy);\r
+               if (!copy.exists()) fail();\r
+               if (copy.length() != 68465) fail();\r
+               copy.delete();\r
+               if (copy.exists()) fail();\r
+               InputStream input = handle.read();\r
+               byte[] bytes = new byte[70000];\r
+               if (input.read(bytes) != 68465) fail();\r
+               input.close();\r
+       }\r
+\r
+       private void testInternal () throws IOException {\r
+               FileHandle handle = Gdx.files.internal("data/badlogic.jpg");\r
+               if (!handle.exists()) fail();\r
+               if (handle.isDirectory()) fail();\r
+               try {\r
+                       handle.delete();\r
+                       fail();\r
+               } catch (Exception expected) {\r
+               }\r
+               if (handle.list().length != 0) fail();\r
+               if (!handle.parent().exists()) fail();\r
+               try {\r
+                       handle.read().close();\r
+                       fail();\r
+               } catch (Exception ignored) {\r
+               }\r
+               FileHandle dir = Gdx.files.internal("data");\r
+               if (!dir.path().equals("data")) fail();\r
+               if (!dir.exists()) fail();\r
+               if (!dir.isDirectory()) fail();\r
+               if (dir.list().length == 0) fail();\r
+               FileHandle child = dir.child("badlogic.jpg");\r
+               if (!child.name().equals("badlogic.jpg")) fail();\r
+               if (!child.nameWithoutExtension().equals("badlogic")) fail();\r
+               if (!child.extension().equals("jpg")) fail();\r
+               if (!child.parent().exists()) fail();\r
+               FileHandle copy = Gdx.files.external("badlogic.jpg-copy");\r
+               copy.delete();\r
+               if (copy.exists()) fail();\r
+               handle.copyTo(copy);\r
+               if (!copy.exists()) fail();\r
+               if (copy.length() != 68465) fail();\r
+               copy.delete();\r
+               if (copy.exists()) fail();\r
+               InputStream input = handle.read();\r
+               byte[] bytes = new byte[70000];\r
+               if (input.read(bytes) != 68465) fail();\r
+               input.close();\r
        }\r
 \r
-       private void testWriteRead (String path, FileType type) {\r
-               FileHandle handle = Gdx.files.getFileHandle(path, type);\r
+       private void testExternal () throws IOException {\r
+               String path = "meow";\r
+               FileHandle handle = Gdx.files.external(path);\r
                handle.delete();\r
                if (handle.exists()) fail();\r
                if (handle.isDirectory()) fail();\r
@@ -113,7 +198,7 @@ public class FilesTest extends GdxTest {
                if (handle.child("meow").exists()) fail();\r
                if (!handle.parent().exists()) fail();\r
                try {\r
-                       handle.read();\r
+                       handle.read().close();\r
                        fail();\r
                } catch (Exception ignored) {\r
                }\r
@@ -128,6 +213,48 @@ public class FilesTest extends GdxTest {
                if (!child.parent().exists()) fail();\r
                if (!handle.deleteDirectory()) fail();\r
                if (handle.exists()) fail();\r
+               OutputStream output = handle.write(false);\r
+               output.write("moo".getBytes());\r
+               output.close();\r
+               if (!handle.exists()) fail();\r
+               if (handle.length() != 3) fail();\r
+               FileHandle copy = Gdx.files.external(path + "-copy");\r
+               copy.delete();\r
+               if (copy.exists()) fail();\r
+               handle.copyTo(copy);\r
+               if (!copy.exists()) fail();\r
+               if (copy.length() != 3) fail();\r
+               FileHandle move = Gdx.files.external(path + "-move");\r
+               move.delete();\r
+               if (move.exists()) fail();\r
+               copy.moveTo(move);\r
+               if (!move.exists()) fail();\r
+               if (move.length() != 3) fail();\r
+               move.deleteDirectory();\r
+               if (move.exists()) fail();\r
+               InputStream input = handle.read();\r
+               byte[] bytes = new byte[6];\r
+               if (input.read(bytes) != 3) fail();\r
+               input.close();\r
+               if (!new String(bytes, 0, 3).equals("moo")) fail();\r
+               output = handle.write(true);\r
+               output.write("cow".getBytes());\r
+               output.close();\r
+               if (handle.length() != 6) fail();\r
+               input = handle.read();\r
+               if (input.read(bytes) != 6) fail();\r
+               input.close();\r
+               if (!new String(bytes, 0, 6).equals("moocow")) fail();\r
+               if (handle.isDirectory()) fail();\r
+               if (handle.list().length != 0) fail();\r
+               if (!handle.name().equals("meow")) fail();\r
+               if (!handle.nameWithoutExtension().equals("meow")) fail();\r
+               if (!handle.extension().equals("")) fail();\r
+               handle.deleteDirectory();\r
+               if (handle.exists()) fail();\r
+               if (handle.isDirectory()) fail();\r
+               handle.delete();\r
+               handle.deleteDirectory();\r
        }\r
 \r
        private void fail () {\r