Re: Run-length encoding (RLE) of stream segments ...
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
/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
/** 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();
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;