This should be an easy question for an experienced people here. I am a Java beginner, so I have been trying to solve this for already several hours. I am studying Java by doing practical work, I have read what I could about arrays and memory allocation, but probably I still don't understand something.
I am writing this method which basically takes source text e.g. "bla bla bla search entry bla bla bla" and should highlight search entry by returning a string like "bla bla bla {font=red}search entry{/font} bla bla bla". If source text is in english, there is no problem, first half of the function (I have not included it) works well. But if the source text is double byte coded I have to use ByteArrayInputStream and ByteArrayOutputStream to do the same thing.
Here is the code:
sDebug=new String(retVal, "UTF-8");
final String sHtml1="<font color='green'><b>";
final String sHtml2="</b></font>";
ByteArrayOutputStream baOut=new ByteArrayOutputStream();
//retVal contains source text
ByteArrayInputStream baIn=new ByteArrayInputStream(retVal); );
try
{
//posB - where search entry begins in retVal
posB=Integer.valueOf(srchArray[j+2]);
//posE - where search entry ends in retVal
posE=posB+Integer.valueOf(srchArray[j+3]);
byte[] buffer=new byte[posB];
//read from beginning till posB
baIn.read(buffer, 0, posB);
sDebug=new String(buffer, "UTF-8");
baOut.write(buffer);
baOut.write(sHtml1.getBytes("UTF-8") );
sDebug=new String(baOut.toByteArray(), "UTF-8");
buffer=new byte[posE-posB];
//*************************************************
//*********THIS IS WHERE IT THROWS EXCEPTION:******
baIn.read(buffer, posB, posE-posB);
baOut.write(buffer);
sDebug=new String(baOut.toByteArray(), "UTF-8");
baOut.write(sHtml2.getBytes("UTF-8"));
sDebug=new String(baOut.toByteArray(), "UTF-8");
buffer=new byte[retVal.length-posE];
baIn.read(buffer, posE, retVal.length-posE);
baOut.write(buffer);
sDebug=new String(baOut.toByteArray(), "UTF-8");
retVal=baOut.toByteArray();
sDebug=new String(baOut.toByteArray(), "UTF-8");
//sDebug=baOut.toString("UTF-8");
}
catch(Exception e)
{
String err="Error: " + e.getMessage();
Toast.makeText(Central.context, err, Toast.LENGTH_LONG).show();
}