Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4299899

javax.swing.text.DefaultStyledDocument.insert() throws incorrect exception

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.5.1, solaris_2.6



      Name: aaC67449 Date: 12/16/99



      The insert() method throws an ArrayIndexOutOfBoundsException instead of a
      BadLocationException.
      See example

      JavaDoc say:

      insert

      protected void insert(int offset,
                            DefaultStyledDocument.ElementSpec[] data)
                     throws BadLocationException

           Inserts new elements in bulk. This is useful to allow parsing with the
      document in an
           unlocked state and prepare an element structure modification. This method
      takes an array
           of tokens that describe how to update an element structure so the time
      within a write
           lock can be greatly reduced in an asynchronous update situation.

           This method is thread safe, although most Swing methods are not. Please see

      Threads and
           Swing for more information.
           Parameters:
                offset - the starting offset >= 0
                data - the element data
           Throws:
                BadLocationException - for an invalid starting offset
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


      The example:
      --------------------------------------------------------------------------------------------------------------------------

      import javax.swing.text.*;

      public class Test {
          public static void main(String argv[]) {

              StubDSD doc = new StubDSD();
              short type = DefaultStyledDocument.ElementSpec.ContentType;
              SimpleAttributeSet attr = new SimpleAttributeSet();
              SimpleAttributeSet attr_before = new SimpleAttributeSet();
              String key = "MAGIC12345";
              String value = "MAGIC54321";
              char arr[] = { 'A', 'B', 'C' };

              attr.addAttribute(key, value);
              attr_before.addAttribute(key, value);
              DefaultStyledDocument.ElementSpec es[] = { doc.new ElementSpec(attr,
                                                         type, arr, 0, 3)
                                                       };
              String setcontent = "The previous content.";

              try {
                  doc.setAsynchronousLoadPriority(-1);
                  doc.insertString(0, setcontent, attr_before);
              } catch (BadLocationException e) {
                  e.printStackTrace(System.out);
                  System.out.println("Unexpected " + e);
              }

              try {
                  doc.insert(Integer.MIN_VALUE, es);
              } catch (BadLocationException e) {
                  e.printStackTrace(System.out);
                  System.out.println(e);
              }

          }
      }

      class StubDSD extends DefaultStyledDocument {
          public StubDSD() {
              super();
          }

          public void insert(int offset, ElementSpec[] data)
                  throws BadLocationException {
              super.insert(offset, data);
          }
      }


      --------------------------------------------------------------------------------------------------------------------------

      Output :
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
              at java.lang.System.arraycopy(Native Method)
              at javax.swing.text.GapVector.shiftGap(Compiled Code)
              at javax.swing.text.GapContent.shiftGap(Compiled Code)
              at javax.swing.text.GapVector.open(Compiled Code)
              at javax.swing.text.GapVector.replace(Compiled Code)
              at javax.swing.text.GapContent.insertString(Compiled Code)
              at javax.swing.text.DefaultStyledDocument.insert(Compiled Code)
              at StubDSD.insert(Compiled Code)
              at Test.main(Compiled Code)

      ======================================================================

            svioletsunw Scott Violet (Inactive)
            alisunw Ali Ali (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: