Skip to content

Commit a9af868

Browse files
authored
Merge pull request #46 from pcardon/android/mv
Change Android mv() back to using the system renameTo() method.
2 parents 3b6f51f + b2bf3a4 commit a9af868

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -648,22 +648,23 @@ static void mv(String path, String dest, Callback callback) {
648648
}
649649

650650
try {
651-
InputStream in = new FileInputStream(path);
652-
OutputStream out = new FileOutputStream(dest);
653-
654-
//read source path to byte buffer. Write from input to output stream
655-
byte[] buffer = new byte[1024];
656-
int read;
657-
while ((read = in.read(buffer)) != -1) { //read is successful
658-
out.write(buffer, 0, read);
651+
// mv should fail if the destination directory does not exist.
652+
File destFile = new File(dest);
653+
File parentDir = destFile.getParentFile();
654+
if (parentDir != null && !parentDir.exists()) {
655+
callback.invoke("mv failed because the destination directory doesn't exist");
656+
return;
657+
}
658+
// mv overwrites files, so delete any existing file.
659+
if (destFile.exists()) {
660+
destFile.delete();
661+
}
662+
// mv by renaming the file.
663+
boolean result = src.renameTo(destFile);
664+
if (!result) {
665+
callback.invoke("mv failed for unknown reasons");
666+
return;
659667
}
660-
in.close();
661-
out.flush();
662-
663-
src.delete(); //remove original file
664-
} catch (FileNotFoundException exception) {
665-
callback.invoke("Source file not found.");
666-
return;
667668
} catch (Exception e) {
668669
callback.invoke(e.toString());
669670
return;

0 commit comments

Comments
 (0)