Results 1 to 3 of 3

Thread: Android: minimize the execution time of the decrypt() method

  1. #1
    Join Date
    Mar 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Android

    Default Android: minimize the execution time of the decrypt() method

    I am developing ebook Android application and a beginner. Below is the code that i have to return HTML content as string and i display it later in Webview in another activity. This code works but I have to minimise the execution time of this method. I am dealing with this issue for days but unable to find a fix.
    Qt Code:
    1. public class CryptoGraphy{
    2. public String decrypt(String file) {
    3. String dec = null;
    4. try {
    5. FileInputStream fstream = new FileInputStream(file);
    6. DataInputStream in = new DataInputStream(fstream);
    7. BufferedReader br = new BufferedReader(new InputStreamReader(in));
    8. int c;
    9. char xorChar = 'A';
    10. int val = xorChar;
    11. StringWriter writer = new StringWriter(1024*20);
    12. BufferedWriter bufferedWriter = new BufferedWriter(writer);
    13.  
    14. while ((c = br.read()) != -1) {
    15. bufferedWriter.write(c ^ val);
    16. }
    17.  
    18. bufferedWriter.close();
    19. writer.flush();
    20. dec = new String(writer.toString().toCharArray());
    21. in.close();
    22. } catch (Exception e) {
    23. Log.e("error decrypt", "out of memory at "+ file);
    24. e.printStackTrace();
    25. }
    26. return dec;
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    And I get the string from another activity like this..
    Qt Code:
    1. CryptoGraphy cryptograph = new CryptoGraphy();
    2. map.put(PageFields.page_content,cryptograph.decrypt(f.toString()));
    To copy to clipboard, switch view to plain text mode 
    The problem is when i tried to run this method in a thread as below.
    I get index string out of bound exception.
    Qt Code:
    1. public class CryptoGraphy implements Runnable{
    2. private volatile String value;
    3. public CryptoGraphy(String file) {
    4. // store parameter for later user
    5. value = file;
    6. System.out.println("Cryptography()");
    7. }
    8. @Override
    9. public void run() {
    10. // TODO Auto-generated method stub
    11. String dec = null;
    12. System.out.println("Cryptography run()");
    13. try {
    14. long startnow;
    15. long endnow;
    16. startnow = android.os.SystemClock.uptimeMillis();
    17. FileInputStream fstream = new FileInputStream(value);
    18. DataInputStream in = new DataInputStream(fstream);
    19. BufferedReader br = new BufferedReader(new InputStreamReader(in));
    20. int c;
    21. char xorChar = 'A';
    22. int val = xorChar;
    23. StringWriter writer = new StringWriter(1024*20);
    24. BufferedWriter bufferedWriter = new BufferedWriter(writer);
    25. while ((c = br.read()) != -1) {
    26. bufferedWriter.write(c ^ val);
    27. }
    28. bufferedWriter.close();
    29. writer.flush();
    30. dec = new String(writer.toString().toCharArray());
    31. value = dec;
    32. System.out.println(dec);
    33. in.close();
    34. endnow = android.os.SystemClock.uptimeMillis();
    35. Log.d("cryptography", "Excution time: "+(endnow-startnow)+" ms");
    36. } catch (Exception e) {
    37.  
    38. Log.e("error decrypt", "out of memory at "+ value);
    39. e.printStackTrace();
    40. }
    41. }
    42. public String getString() {
    43. System.out.println("Cryptography getString()");
    44. return value;
    45. }
    46. }
    47.  
    48. CryptoGraphy myRunnable = new CryptoGraphy(f.toString());
    49. new Thread(myRunnable).start();
    50. String page_content_from_crypt = myRunnable.getString();
    51. map.put(PageFields.page_content,page_content_from_crypt );
    To copy to clipboard, switch view to plain text mode 

    Logcat output

    Qt Code:
    1. 03-19 10:05:59.602: E/AndroidRuntime(15913): Caused by: java.lang.StringIndexOutOfBoundsException: length=952; regionStart=93; regionLength=-94
    2. 03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.startEndAndLength(String.java:593)
    3. 03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.substring(String.java:1474)
    4. 03-19 10:05:59.602: E/AndroidRuntime(15913): at com.ssparkl.reader.MainActivity.getdata(MainActivity.java:3227)
    To copy to clipboard, switch view to plain text mode 

    is there any other way to minimize the execution time of this decrypt("filepath") method? now it takes more than 40 seconds to decrypt a 75 page book. I am trying to bring the time down to 5 seconds or less. Please help. Thanks in Advance.
    Last edited by Hari Vikram; 19th March 2015 at 06:34.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Android: minimize the execution time of the decrypt() method

    I don't see where you are waiting for the thread to complete the operation.
    You call getString() immediately after starting the thread, which might be before the thread has even begun working the runnable.

    Also, why don't you use the result of writer.toString()? Seems wasteful to create another copy of the data.

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Windows

    Default Re: Android: minimize the execution time of the decrypt() method

    Before optimising, measure. Find out where this spends most of it's time, and concentrate on that code.

    On a hunch, I'd try reading and writing in larger chunks than one character at a time.

Similar Threads

  1. Replies: 0
    Last Post: 5th October 2012, 11:17
  2. Replies: 4
    Last Post: 9th April 2012, 17:31
  3. Replies: 6
    Last Post: 18th August 2010, 12:52
  4. Replies: 1
    Last Post: 21st August 2008, 08:29
  5. Replies: 5
    Last Post: 7th December 2006, 11:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.