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
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
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
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
}\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
\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
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
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
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
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
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