classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: StringBuffer question


From: Eric Blake
Subject: Re: StringBuffer question
Date: Wed, 06 Mar 2002 18:12:40 -0700

Brian Jones wrote:
> 
> I cannot get at the javadoc at the moment for 1.4, it seems to only
> partially load.  The implementation should match what is specified
> explicitly, and where such language is missing match behaviour
> instead.  Generally, software written to specification should not fail
> with Classpath if it works in Sun's VM is what I think we strive toward.

You're not the only one having problems reading the javadoc.  The web
page ends on a '\0' character - I think their server is truncating the
file when it hits the embedded null.  What I ended up doing, however,
was to download the documentation package (30 MB!) from Sun, and that
zip file contains a clean version of the StringBuffer.html file.

For example, setLength, the method that got truncated by Sun's servers,
is documented thus:

"public void setLength(int newLength)
...
"Parameters: newLength - the new length of the buffer. 
"Throws: IndexOutOfBoundsException - if the newLength argument is
negative.
"See Also: length()"

Yet my experiments show that Sun's implementation throws
StringIndexOutOfBoundsException.  Consider this code snippet:
void m(StringBuffer s, int i)
{
  try
    {
      s.setLength(i);
    }
  catch (StringIndexOutOfBoundsException e)
    {
    }
}

If you call m(new StringBuffer(), -1), the exception will be trapped by
Sun's implementation, but that behavior was not strictly documented.  So
back to my original question: Is the bounds check in the current
Classpath implementation of setLength necessary, or am I justified in
breaking the above snippet by removing the bounds check and throwing
IndexOutOfBoundsException instead?

  public synchronized void setLength(int newLength)
  {
// If this bounds check is skipped...
    if (newLength < 0)
      throw new StringIndexOutOfBoundsException(newLength);

    ensureCapacity_unsynchronized(newLength);
    for (int i = count; i < newLength; ++i)
// ... this line will throw IndexOutOfBoundsException
      value[i] = '\0';
    count = newLength;
  }

-- 
This signature intentionally left boring.

Eric Blake             address@hidden
  BYU student, free software programmer



reply via email to

[Prev in Thread] Current Thread [Next in Thread]