src/java.base/share/classes/java/lang/String.java
changeset 49109 f2804cb922ce
parent 48262 daf3b49f4839
child 49122 fc16b5f193c7
equal deleted inserted replaced
49108:b9ee7c274ee0 49109:f2804cb922ce
  2961      *          guaranteed to be from a pool of unique strings.
  2961      *          guaranteed to be from a pool of unique strings.
  2962      * @jls 3.10.5 String Literals
  2962      * @jls 3.10.5 String Literals
  2963      */
  2963      */
  2964     public native String intern();
  2964     public native String intern();
  2965 
  2965 
       
  2966     /**
       
  2967      * Returns a string whose value is the concatenation of this
       
  2968      * string repeated {@code count} times.
       
  2969      * <p>
       
  2970      * If this string is empty or count is zero then the empty
       
  2971      * string is returned.
       
  2972      *
       
  2973      * @param   count number of times to repeat
       
  2974      *
       
  2975      * @return  A string composed of this string repeated
       
  2976      *          {@code count} times or the empty string if this
       
  2977      *          string is empty or count is zero
       
  2978      *
       
  2979      * @throws  IllegalArgumentException if the {@code count} is
       
  2980      *          negative.
       
  2981      *
       
  2982      * @since 11
       
  2983      */
       
  2984     public String repeat(int count) {
       
  2985         if (count < 0) {
       
  2986             throw new IllegalArgumentException("count is negative: " + count);
       
  2987         }
       
  2988         if (count == 1) {
       
  2989             return this;
       
  2990         }
       
  2991         final int len = value.length;
       
  2992         if (len == 0 || count == 0) {
       
  2993             return "";
       
  2994         }
       
  2995         if (len == 1) {
       
  2996             final byte[] single = new byte[count];
       
  2997             Arrays.fill(single, value[0]);
       
  2998             return new String(single, coder);
       
  2999         }
       
  3000         if (Integer.MAX_VALUE / count < len) {
       
  3001             throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
       
  3002                     " times will produce a String exceeding maximum size.");
       
  3003         }
       
  3004         final int limit = len * count;
       
  3005         final byte[] multiple = new byte[limit];
       
  3006         System.arraycopy(value, 0, multiple, 0, len);
       
  3007         int copied = len;
       
  3008         for (int next = copied << 1; next < limit && 0 < next; next = next << 1) {
       
  3009             System.arraycopy(multiple, 0, multiple, copied, copied);
       
  3010             copied = next;
       
  3011         }
       
  3012         System.arraycopy(multiple, 0, multiple, copied, limit - copied);
       
  3013         return new String(multiple, coder);
       
  3014     }
       
  3015 
  2966     ////////////////////////////////////////////////////////////////
  3016     ////////////////////////////////////////////////////////////////
  2967 
  3017 
  2968     /**
  3018     /**
  2969      * Copy character bytes from this string into dst starting at dstBegin.
  3019      * Copy character bytes from this string into dst starting at dstBegin.
  2970      * This method doesn't perform any range checking.
  3020      * This method doesn't perform any range checking.