Hari Vikram
19th March 2015, 06:29
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;
}
}
And I get the string from another activity like this..
CryptoGraphy cryptograph = new CryptoGraphy();
map.put(PageFields.page_content,cryptograph.decryp t(f.toString()));
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 );
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(MainActivi ty.java:3227)
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.
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;
}
}
And I get the string from another activity like this..
CryptoGraphy cryptograph = new CryptoGraphy();
map.put(PageFields.page_content,cryptograph.decryp t(f.toString()));
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 );
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(MainActivi ty.java:3227)
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.