aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
Diffstat (limited to 'java')
-rw-r--r--java/org/gnu/emacs/EmacsOpenActivity.java64
1 files changed, 62 insertions, 2 deletions
diff --git a/java/org/gnu/emacs/EmacsOpenActivity.java b/java/org/gnu/emacs/EmacsOpenActivity.java
index baf31039ecd..c8501d91025 100644
--- a/java/org/gnu/emacs/EmacsOpenActivity.java
+++ b/java/org/gnu/emacs/EmacsOpenActivity.java
@@ -58,8 +58,10 @@ import android.os.Bundle;
58import android.os.ParcelFileDescriptor; 58import android.os.ParcelFileDescriptor;
59 59
60import java.io.File; 60import java.io.File;
61import java.io.FileReader; 61import java.io.FileInputStream;
62import java.io.FileNotFoundException; 62import java.io.FileNotFoundException;
63import java.io.FileOutputStream;
64import java.io.FileReader;
63import java.io.IOException; 65import java.io.IOException;
64import java.io.InputStream; 66import java.io.InputStream;
65import java.io.UnsupportedEncodingException; 67import java.io.UnsupportedEncodingException;
@@ -176,6 +178,50 @@ public class EmacsOpenActivity extends Activity
176 dialog.show (); 178 dialog.show ();
177 } 179 }
178 180
181 /* Check that the specified FILE is readable. If it is not, then
182 copy the file in FD to a location in the system cache
183 directory and return the name of that file. */
184
185 private String
186 checkReadableOrCopy (String file, ParcelFileDescriptor fd)
187 throws IOException, FileNotFoundException
188 {
189 File inFile;
190 FileOutputStream outStream;
191 InputStream stream;
192 byte buffer[];
193 int read;
194
195 inFile = new File (file);
196
197 if (inFile.setReadable (true))
198 return file;
199
200 /* inFile is now the file being written to. */
201 inFile = new File (getCacheDir (), inFile.getName ());
202 buffer = new byte[4098];
203 outStream = new FileOutputStream (inFile);
204 stream = new FileInputStream (fd.getFileDescriptor ());
205
206 try
207 {
208 while ((read = stream.read (buffer)) >= 0)
209 outStream.write (buffer, 0, read);
210 }
211 finally
212 {
213 /* Note that this does not close FD.
214
215 Keep in mind that execution is transferred to ``finally''
216 even if an exception happens inside the while loop
217 above. */
218 stream.close ();
219 outStream.close ();
220 }
221
222 return inFile.getCanonicalPath ();
223 }
224
179 /* Finish this activity in response to emacsclient having 225 /* Finish this activity in response to emacsclient having
180 successfully opened a file. 226 successfully opened a file.
181 227
@@ -340,17 +386,19 @@ public class EmacsOpenActivity extends Activity
340 opening the file and doing readlink on its file 386 opening the file and doing readlink on its file
341 descriptor in /proc/self/fd. */ 387 descriptor in /proc/self/fd. */
342 resolver = getContentResolver (); 388 resolver = getContentResolver ();
389 fd = null;
343 390
344 try 391 try
345 { 392 {
346 fd = resolver.openFileDescriptor (uri, "r"); 393 fd = resolver.openFileDescriptor (uri, "r");
347 names = EmacsNative.getProcName (fd.getFd ()); 394 names = EmacsNative.getProcName (fd.getFd ());
348 fd.close ();
349 395
350 /* What is the right encoding here? */ 396 /* What is the right encoding here? */
351 397
352 if (names != null) 398 if (names != null)
353 fileName = new String (names, "UTF-8"); 399 fileName = new String (names, "UTF-8");
400
401 fileName = checkReadableOrCopy (fileName, fd);
354 } 402 }
355 catch (FileNotFoundException exception) 403 catch (FileNotFoundException exception)
356 { 404 {
@@ -360,6 +408,18 @@ public class EmacsOpenActivity extends Activity
360 { 408 {
361 /* Do nothing. */ 409 /* Do nothing. */
362 } 410 }
411
412 if (fd != null)
413 {
414 try
415 {
416 fd.close ();
417 }
418 catch (IOException exception)
419 {
420 /* Do nothing. */
421 }
422 }
363 } 423 }
364 424
365 if (fileName == null) 425 if (fileName == null)