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

(bf) CharBuffers created by wrap(Str) and wrap(char[]) behave differently

XMLWordPrintable

    • sparc
    • solaris_7

      CharBuffers created by calling wrap(String) and wrap(char[]) behaves differently when get(int index) is called , for index > limit.

      CharBuffer created by wrapping string returns the character at location index. Which it should not return.

      And CharBuffer created by wrapping char array throws expected IndexOutOfBoundsException.

      public static void main(String[] args) {
         String source = "This is a test string";
         CharBuffer c1 = CharBuffer.wrap(source);
         CharBuffer c2 = CharBuffer.wrap(source.toCharArray());
         int limit = source.length()/2;
         c1.limit(limit);
         c2.limit(limit);
         test(c1, "CharBuffer created by wrapping String.");
         test(c2, "CharBuffer created by wrapping Char Array.");
      }
      private static void test(CharBuffer c,String msg) {
         System.out.println(msg);
         try {
           int index = c.limit() + 1;
           char ch = c.get(index);
           System.out.println("Char at index is: "+ch);
           System.out.println("Error: IndexOutOfBoundsException not thrown for index > limit");
           System.out.println("Test Failed");
         }
         catch(IndexOutOfBoundsException e){
           System.out.println("Expected IndexOutOfBoundsException thrown for index > limit");
           System.out.println("Test Passed");
         }
      }

            iris Iris Clark
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: