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.
public class CryptoGraphy{
public String decrypt(String file) {
String dec = null;
try {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int c;
char xorChar = 'A';
int val = xorChar;
StringWriter writer = new StringWriter(1024*20);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
while ((c = br.read()) != -1) {
bufferedWriter.write(c ^ val);
}
bufferedWriter.close();
writer.flush();
dec = new String(writer.toString().toCharArray());
in.close();
} catch (Exception e) {
Log.e("error decrypt", "out of memory at "+ file);
e.printStackTrace();
}
return dec;
}
}
public class CryptoGraphy{
public String decrypt(String file) {
String dec = null;
try {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int c;
char xorChar = 'A';
int val = xorChar;
StringWriter writer = new StringWriter(1024*20);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
while ((c = br.read()) != -1) {
bufferedWriter.write(c ^ val);
}
bufferedWriter.close();
writer.flush();
dec = new String(writer.toString().toCharArray());
in.close();
} catch (Exception e) {
Log.e("error decrypt", "out of memory at "+ file);
e.printStackTrace();
}
return dec;
}
}
To copy to clipboard, switch view to plain text mode
And I get the string from another activity like this..
CryptoGraphy cryptograph = new CryptoGraphy();
map.put(PageFields.page_content,cryptograph.decrypt(f.toString()));
CryptoGraphy cryptograph = new CryptoGraphy();
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.
public class CryptoGraphy implements Runnable{
private volatile String value;
public CryptoGraphy(String file) {
// store parameter for later user
value = file;
System.out.println("Cryptography()");
}
@Override
public void run() {
// TODO Auto-generated method stub
String dec = null;
System.out.println("Cryptography run()");
try {
long startnow;
long endnow;
startnow = android.os.SystemClock.uptimeMillis();
FileInputStream fstream = new FileInputStream(value);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int c;
char xorChar = 'A';
int val = xorChar;
StringWriter writer = new StringWriter(1024*20);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
while ((c = br.read()) != -1) {
bufferedWriter.write(c ^ val);
}
bufferedWriter.close();
writer.flush();
dec = new String(writer.toString().toCharArray());
value = dec;
System.out.println(dec);
in.close();
endnow = android.os.SystemClock.uptimeMillis();
Log.d("cryptography", "Excution time: "+(endnow-startnow)+" ms");
} catch (Exception e) {
Log.e("error decrypt", "out of memory at "+ value);
e.printStackTrace();
}
}
public String getString() {
System.out.println("Cryptography getString()");
return value;
}
}
CryptoGraphy myRunnable = new CryptoGraphy(f.toString());
new Thread(myRunnable).start();
String page_content_from_crypt = myRunnable.getString();
map.put(PageFields.page_content,page_content_from_crypt );
public class CryptoGraphy implements Runnable{
private volatile String value;
public CryptoGraphy(String file) {
// store parameter for later user
value = file;
System.out.println("Cryptography()");
}
@Override
public void run() {
// TODO Auto-generated method stub
String dec = null;
System.out.println("Cryptography run()");
try {
long startnow;
long endnow;
startnow = android.os.SystemClock.uptimeMillis();
FileInputStream fstream = new FileInputStream(value);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
int c;
char xorChar = 'A';
int val = xorChar;
StringWriter writer = new StringWriter(1024*20);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
while ((c = br.read()) != -1) {
bufferedWriter.write(c ^ val);
}
bufferedWriter.close();
writer.flush();
dec = new String(writer.toString().toCharArray());
value = dec;
System.out.println(dec);
in.close();
endnow = android.os.SystemClock.uptimeMillis();
Log.d("cryptography", "Excution time: "+(endnow-startnow)+" ms");
} catch (Exception e) {
Log.e("error decrypt", "out of memory at "+ value);
e.printStackTrace();
}
}
public String getString() {
System.out.println("Cryptography getString()");
return value;
}
}
CryptoGraphy myRunnable = new CryptoGraphy(f.toString());
new Thread(myRunnable).start();
String page_content_from_crypt = myRunnable.getString();
map.put(PageFields.page_content,page_content_from_crypt );
To copy to clipboard, switch view to plain text mode
Logcat output
03-19 10:05:59.602: E/AndroidRuntime(15913): Caused by: java.lang.StringIndexOutOfBoundsException: length=952; regionStart=93; regionLength=-94
03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.startEndAndLength(String.java:593)
03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.substring(String.java:1474)
03-19 10:05:59.602: E/AndroidRuntime(15913): at com.ssparkl.reader.MainActivity.getdata(MainActivity.java:3227)
03-19 10:05:59.602: E/AndroidRuntime(15913): Caused by: java.lang.StringIndexOutOfBoundsException: length=952; regionStart=93; regionLength=-94
03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.startEndAndLength(String.java:593)
03-19 10:05:59.602: E/AndroidRuntime(15913): at java.lang.String.substring(String.java:1474)
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.
Bookmarks