Re: Run-length encoding (RLE) of stream segments ...

Discussion in 'Java' started by Arne Vajhøj, Dec 27, 2010.

  1. Arne Vajhøj

    Arne Vajhøj Guest

    On 26-12-2010 21:32, lbrt chx _ gemale kom wrote:
    > Actually, I just coded an implementation myself, but as I was testing my code, I got an Exception that to me looks like a bug more than an error from myself
    > ~
    > Isn't java.lang.StringBuffer supposed to do its internal buffering by itself?


    It is supposed to increase the buffer size by itself when you append.


    > Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16
    > at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:979)
    > at java.lang.StringBuffer.insert(StringBuffer.java:446)
    > at AUtils00Test.main(AUtils00Test.java:104)
    > ~
    > /java/lang/StringBuffer.java's insert should "ensureCapacity_unsynchronized" before doing its business as it does for all other methods; which you can see from:


    It should expand the buffer if necessary to hold the extra
    characters being pushed out by the insert.

    If you try and insert beyond the end it is supposed to throw
    an exception.

    > http://www.opensource.apple.com/source/gccfast/gccfast-1622/libjava/java/lang/StringBuffer.java
    > ~
    > /** Insert the<code>String</code> argument into this<code>StringBuffer</code>.
    > * @param offset the place to insert.
    > * @param str the<code>String</code> to insert.
    > * @return this<code>StringBuffer</code>.
    > * @exception IndexOutOfBoundsException if<code>offset</code> is out
    > * of range for this<code>StringBuffer</code>.


    which is documented here!

    > */
    > public synchronized StringBuffer insert (int offset, String str)
    > {
    > if (offset< 0 || offset> count)
    > throw new StringIndexOutOfBoundsException (offset);


    and done here!

    > // Note that using `null' is from JDK 1.2.
    > if (str == null)
    > str = "null";
    > int len = str.length();
    > ensureCapacity_unsynchronized (count+len);


    And making this call *after* the test means that the
    implementation follow what it is documented to do (and
    not your expectations).

    > System.arraycopy(value, offset, value, offset+len, count-offset);
    > str.getChars(0, len, value, offset);
    > count += len;
    > return this;
    > }


    Arne
     
    Arne Vajhøj, Dec 27, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC
    Replies:
    2
    Views:
    3,960
    Marc Guardiani
    Nov 12, 2003
  2. nullptr

    RLE code review

    nullptr, Nov 8, 2003, in forum: C Programming
    Replies:
    14
    Views:
    646
    Jonathan2s6
    Nov 12, 2003
  3. Gurjant

    4 BITS RLE IN BMP

    Gurjant, Feb 6, 2008, in forum: C Programming
    Replies:
    1
    Views:
    444
    Jack Klein
    Feb 6, 2008
  4. Arne Vajhøj
    Replies:
    8
    Views:
    890
    Arne Vajhøj
    Jan 9, 2011
  5. Lew
    Replies:
    2
    Views:
    382
Loading...

Share This Page