< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 49004 : 8197594: String#repeat


2946      * When the intern method is invoked, if the pool already contains a
2947      * string equal to this {@code String} object as determined by
2948      * the {@link #equals(Object)} method, then the string from the pool is
2949      * returned. Otherwise, this {@code String} object is added to the
2950      * pool and a reference to this {@code String} object is returned.
2951      * <p>
2952      * It follows that for any two strings {@code s} and {@code t},
2953      * {@code s.intern() == t.intern()} is {@code true}
2954      * if and only if {@code s.equals(t)} is {@code true}.
2955      * <p>
2956      * All literal strings and string-valued constant expressions are
2957      * interned. String literals are defined in section 3.10.5 of the
2958      * <cite>The Java&trade; Language Specification</cite>.
2959      *
2960      * @return  a string that has the same contents as this string, but is
2961      *          guaranteed to be from a pool of unique strings.
2962      * @jls 3.10.5 String Literals
2963      */
2964     public native String intern();
2965 














































2966     ////////////////////////////////////////////////////////////////
2967 
2968     /**
2969      * Copy character bytes from this string into dst starting at dstBegin.
2970      * This method doesn't perform any range checking.
2971      *
2972      * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
2973      * coders are different, and dst is big enough (range check)
2974      *
2975      * @param dstBegin  the char index, not offset of byte[]
2976      * @param coder     the coder of dst[]
2977      */
2978     void getBytes(byte dst[], int dstBegin, byte coder) {
2979         if (coder() == coder) {
2980             System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
2981         } else {    // this.coder == LATIN && coder == UTF16
2982             StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
2983         }
2984     }
2985 




2946      * When the intern method is invoked, if the pool already contains a
2947      * string equal to this {@code String} object as determined by
2948      * the {@link #equals(Object)} method, then the string from the pool is
2949      * returned. Otherwise, this {@code String} object is added to the
2950      * pool and a reference to this {@code String} object is returned.
2951      * <p>
2952      * It follows that for any two strings {@code s} and {@code t},
2953      * {@code s.intern() == t.intern()} is {@code true}
2954      * if and only if {@code s.equals(t)} is {@code true}.
2955      * <p>
2956      * All literal strings and string-valued constant expressions are
2957      * interned. String literals are defined in section 3.10.5 of the
2958      * <cite>The Java&trade; Language Specification</cite>.
2959      *
2960      * @return  a string that has the same contents as this string, but is
2961      *          guaranteed to be from a pool of unique strings.
2962      * @jls 3.10.5 String Literals
2963      */
2964     public native String intern();
2965 
2966     /**
2967      * Returns a string whose value is the concatenation of this
2968      * string repeated {@code count} times.
2969      * <p>
2970      * If count or length is zero then the empty string is returned.
2971      * <p>
2972      * This method may be used to create space padding for
2973      * formatting text or zero padding for formatting numbers.
2974      * @param   count number of times to repeat
2975      * @return  A string composed of this string repeated
2976      *          {@code count} times or the empty string if count
2977      *          or length is zero.
2978      * @throws  IllegalArgumentException if the {@code count} is
2979      *          negative.
2980      */
2981     public String repeat(int count) {
2982         if (count < 0) {
2983             throw new IllegalArgumentException("count is negative, " + count);
2984         }
2985         if (count == 1) {
2986             return this;
2987         }
2988         final int len = value.length;
2989         if (len == 0 || count == 0) {
2990             return "";
2991         }
2992         if (len == 1) {
2993             final byte[] single = new byte[count];
2994             Arrays.fill(single, value[0]);
2995             return new String(single, coder);
2996         }
2997         if (Integer.MAX_VALUE / count < len) {
2998             throw new OutOfMemoryError();
2999         }
3000         final int limit = len * count;
3001         final byte[] multiple = new byte[limit];
3002         System.arraycopy(value, 0, multiple, 0, len);
3003         int copied = len;
3004         for (int next = copied << 1; next < limit && 0 < next; next = next << 1) {
3005             System.arraycopy(multiple, 0, multiple, copied, copied);
3006             copied = next;
3007         }
3008         System.arraycopy(multiple, 0, multiple, copied, limit - copied);
3009         return new String(multiple, coder);
3010     }
3011 
3012     ////////////////////////////////////////////////////////////////
3013 
3014     /**
3015      * Copy character bytes from this string into dst starting at dstBegin.
3016      * This method doesn't perform any range checking.
3017      *
3018      * Invoker guarantees: dst is in UTF16 (inflate itself for asb), if two
3019      * coders are different, and dst is big enough (range check)
3020      *
3021      * @param dstBegin  the char index, not offset of byte[]
3022      * @param coder     the coder of dst[]
3023      */
3024     void getBytes(byte dst[], int dstBegin, byte coder) {
3025         if (coder() == coder) {
3026             System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
3027         } else {    // this.coder == LATIN && coder == UTF16
3028             StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
3029         }
3030     }
3031 


< prev index next >