1212import android .content .DialogInterface .OnCancelListener ;
1313import android .content .Intent ;
1414import android .content .pm .PackageManager ;
15- import android .content .res .AssetFileDescriptor ;
1615import android .content .res .Configuration ;
1716import android .content .res .Resources ;
1817import android .graphics .Rect ;
@@ -99,6 +98,7 @@ public class MainActivity extends NativeActivity {
9998 private static final int REQUEST_STORAGE_PERMISSION = 1 ;
10099 private static final int REQUEST_LOCATION_PERMISSION = 2 ;
101100 private static final String FOLDER_NAME = "SmallBASIC" ;
101+ private static final int COPY_BUFFER_SIZE = 1024 ;
102102 private String _startupBas = null ;
103103 private boolean _untrusted = false ;
104104 private final ExecutorService _audioExecutor = Executors .newSingleThreadExecutor ();
@@ -694,24 +694,23 @@ public void run() {
694694 });
695695 }
696696
697- private void copy (File src , File dst ) throws IOException {
698- InputStream in = new FileInputStream (src );
697+ private void copy (InputStream in , OutputStream out ) throws IOException {
699698 try {
700- OutputStream out = new FileOutputStream (dst );
701- try {
702- byte [] buf = new byte [1024 ];
703- int len ;
704- while ((len = in .read (buf )) > 0 ) {
705- out .write (buf , 0 , len );
706- }
707- } finally {
708- out .close ();
699+ byte [] buf = new byte [COPY_BUFFER_SIZE ];
700+ int len ;
701+ while ((len = in .read (buf )) > 0 ) {
702+ out .write (buf , 0 , len );
709703 }
710704 } finally {
705+ out .close ();
711706 in .close ();
712707 }
713708 }
714709
710+ private void copy (File src , File dst ) throws IOException {
711+ copy (new FileInputStream (src ), new FileOutputStream (dst ));
712+ }
713+
715714 private String execBuffer (final String buffer , final String name , boolean run ) throws IOException {
716715 File outputFile = new File (getInternalStorage (), name );
717716 BufferedWriter output = new BufferedWriter (new FileWriter (outputFile ));
@@ -952,7 +951,15 @@ protected Response getFile(String path, boolean asset) throws IOException {
952951 log ("Opened " + name + " " + length + " bytes" );
953952 result = new Response (getAssets ().open (name ), length );
954953 } else {
955- result = null ;
954+ File file = getFile (getExternalStorage (), path );
955+ if (file == null ) {
956+ file = getFile (getInternalStorage (), path );
957+ }
958+ if (file != null ) {
959+ result = new Response (new FileInputStream (file ), file .length ());
960+ } else {
961+ throw new IOException ("file not found" );
962+ }
956963 }
957964 return result ;
958965 }
@@ -976,13 +983,46 @@ protected void log(String message) {
976983 }
977984
978985 @ Override
979- protected boolean renameFile (String from , String to ) {
980- return false ;
986+ protected void renameFile (String from , String to ) throws IOException {
987+ if (to == null || !to .endsWith (".bas" )) {
988+ throw new IOException ("Invalid New File Name: " + to );
989+ }
990+ File toFile = getFile (getInternalStorage (), to );
991+ if (toFile == null ) {
992+ toFile = getFile (getExternalStorage (), to );
993+ }
994+ if (toFile != null ) {
995+ throw new IOException ("New File Name already exists" );
996+ }
997+ File fromFile = getFile (getInternalStorage (), from );
998+ if (fromFile == null ) {
999+ fromFile = getFile (getExternalStorage (), from );
1000+ }
1001+ if (fromFile == null ) {
1002+ throw new IOException ("Old File Name does not exist" );
1003+ }
1004+ if (!fromFile .renameTo (new File (getExternalStorage (), to ))) {
1005+ throw new IOException ("File rename failed" );
1006+ }
9811007 }
9821008
9831009 @ Override
984- protected boolean saveFile (String fileName , String content ) {
985- return false ;
1010+ protected void saveFile (String fileName , byte [] content ) throws IOException {
1011+ File file = new File (getExternalStorage (), fileName );
1012+ if (file .exists ()) {
1013+ throw new IOException ("File already exists" );
1014+ } else if (file .isDirectory ()) {
1015+ throw new IOException ("Invalid File Name: " + fileName );
1016+ }
1017+ copy (new ByteArrayInputStream (content ), new FileOutputStream (file ));
1018+ }
1019+
1020+ private File getFile (String parent , String path ) {
1021+ File result = new File (parent , path );
1022+ if (!result .exists () || !result .canRead () || result .isDirectory ()) {
1023+ result = null ;
1024+ }
1025+ return result ;
9861026 }
9871027
9881028 private long getFileLength (String name ) throws IOException {
0 commit comments