-2

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();
            }
Ivan
  • 6,388
  • 3
  • 24
  • 30
  • 1
    Problem seems to be on these lines posB=Integer.valueOf(srchArray[j+2]); and posE=posB+Integer.valueOf(srchArray[j+3]); . It seems you are accessing srchArray elements which don't exist. – Shashank Kadne Feb 10 '12 at 07:39
  • On what line does the exception occur? – Mike Samuel Feb 10 '12 at 07:40
  • 1
    Please re-visit your older questions and check whether you can accept some answers. That would encourage people to offer more help. – Andreas Dolk Feb 10 '12 at 07:43
  • I revisited my old questions. Thanks for that. I didnt notice the big grey tick next to each answer to do that, now I should be fine. – Ivan Feb 11 '12 at 01:48
  • 2 Mike. Those lines are fine, exception occurs on the next line after this //*********THIS IS WHERE IT THROWS EXCEPTION:****** – Ivan Feb 11 '12 at 01:49

2 Answers2

0

If this line:

   baIn.read(buffer, posB, posE-posB);

is throwing an bounds exception, it is telling you that you've made a mistake with your index calculations. In other words, the indexes posB through posE - 1 aren't all within the buffer.


This should be an easy question for an experienced people here.

You'd be surprised. It is hard because:

  • your code is messy and hard to read,
  • your code's logic is ugly and full of inexplicable hard-wired constants,
  • your code is incomplete and contains obvious compilation errors, and
  • your description of what you are trying to do is hard to read / understand

In short it is horrible.

So as an experienced programmer, I'm going to suggest that you THROW IT AWAY and rewrite it from scratch. (If I had to code review it, I'd reject it outright as manifestly unmaintainable, whether or not it "worked".)

If that is too much to bear, you need to learn to use a debugger.

One final piece of advice is that byte-bashing text data in Java is crazy. Treat it as character based and you can use a whole host of powerful and (if used sensibly) efficient class libraries, starting with String / StringBuilder, and moving up to scanners, formatters and regular expressions.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks Stephen. I know it is awfull. I am just trying to make it do what I need. The reason why I am not treating that as a String is that I have fts sqlite search and it returns me offsets string containing positions of search entry within the text http://www.sqlite.org/fts3.html#section_4_1 . Now I need to manipulate the text so that outline search entry. I had to use ByteArray Stream Classes because offsets are measured in bytes, but text in the String is encoded using 2 bytes and 1 byte for space which makes it impossible to use these offsets with String class – Ivan Feb 11 '12 at 03:22
0

the problem was that instead of

baIn.read(buffer, posB, posE-posB);

I should have used

baIn.read(buffer, 0, posE-posB); 

it looks like second parameter must always be 0 despite of what is written in the doc.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Ivan
  • 6,388
  • 3
  • 24
  • 30