Java Random String randomFixedByteLengthUnicodeString(Random r, int length)

Here you can find the source of randomFixedByteLengthUnicodeString(Random r, int length)

Description

Returns random string, with a given UTF-8 byte length

License

Apache License

Declaration

public static String randomFixedByteLengthUnicodeString(Random r,
        int length) 

Method Source Code

//package com.java2s;
/**//from w  w w  .  j  a v a2 s .c  o  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /** Returns random string, with a given UTF-8 byte length*/
    public static String randomFixedByteLengthUnicodeString(Random r,
            int length) {

        final char[] buffer = new char[length * 3];
        int bytes = length;
        int i = 0;
        for (; i < buffer.length && bytes != 0; i++) {
            int t;
            if (bytes >= 4) {
                t = r.nextInt(5);
            } else if (bytes >= 3) {
                t = r.nextInt(4);
            } else if (bytes >= 2) {
                t = r.nextInt(2);
            } else {
                t = 0;
            }
            if (t == 0) {
                buffer[i] = (char) r.nextInt(0x80);
                bytes--;
            } else if (1 == t) {
                buffer[i] = (char) nextInt(r, 0x80, 0x7ff);
                bytes -= 2;
            } else if (2 == t) {
                buffer[i] = (char) nextInt(r, 0x800, 0xd7ff);
                bytes -= 3;
            } else if (3 == t) {
                buffer[i] = (char) nextInt(r, 0xe000, 0xfffe);
                bytes -= 3;
            } else if (4 == t) {
                // Make a surrogate pair
                // High surrogate
                buffer[i++] = (char) nextInt(r, 0xd800, 0xdbff);
                // Low surrogate
                buffer[i] = (char) nextInt(r, 0xdc00, 0xdfff);
                bytes -= 4;
            }

        }
        return new String(buffer, 0, i);
    }

    /** start and end are BOTH inclusive */
    public static int nextInt(Random r, int start, int end) {
        return start + r.nextInt(end - start + 1);
    }
}

Related

  1. getString(int length)
  2. getString(int length)
  3. getString(int n, int arg[])
  4. randomCharacter(String availableValues)
  5. randomFileString()
  6. randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
  7. randomize(String[] orderedList)
  8. randomizeMap(Map map)
  9. randomlyRecaseCodePoints(Random random, String str)