Create A Java Variable (String) of a specific size (MB's)
<p>I am trying to benchmark some code. I am sending a String msg over sockets. I want to send 100KB, 2MB, and 10MB String variables. Is there an easy way to create a variable of these sizes?</p>

<p>Currently I am doing this.</p>

<pre><code>private static String createDataSize(int msgSize) {
    String data = "a";
    while(data.length() &lt; (msgSize*1024)-6) {
        data += "a";
    }
    return data;
}
</code></pre>

<p>But this takes a very long time. Is there a better way?</p>

<p>UPDATE:
Thanks, I am doing this now.</p>

<pre><code>/**
 * Creates a message of size @msgSize in KB.
 */
private static String createDataSize(int msgSize) {
    // Java chars are 2 bytes
    msgSize = msgSize/2;
    msgSize = msgSize * 1024;
    StringBuilder sb = new StringBuilder(msgSize);
    for (int i=0; i&lt;msgSize; i++) {
        sb.append('a');
    }
    return sb.toString();
  }
</code></pre>
