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

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

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_7

      Description

      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");
         }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              iris Iris Clark
              Reporter:
              duke J. Duke (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: