}
private String buildDoc(Base64Variant b64variant, Random r, byte[] data, boolean addNoise)
{
// Let's use base64 codec from RI here:
AsciiValueEncoder enc = new ValueEncoderFactory().getEncoder(b64variant, data, 0, data.length);
StringBuffer sb = new StringBuffer(data.length * 2);
sb.append("<root>");
// Without noise it's quite easy, just need enough space:
if (!addNoise) {
// Base64 adds 33% overhead, but let's be generous
char[] buffer = new char[4 + (data.length * 3 / 2)];
int len = enc.encodeMore(buffer, 0, buffer.length);
sb.append(buffer, 0, len);
} else {
// but with noise, need bit different approach
char[] buffer = new char[300];
while (!enc.isCompleted()) {
int offset = r.nextInt() & 0xF;
int len;
int rn = r.nextInt() & 15;
switch (rn) {
case 1:
case 2:
case 3:
case 4:
len = rn;
break;
case 5:
case 6:
case 7:
len = 3 + (r.nextInt() & 15);
break;
default:
len = 20 + (r.nextInt() & 127);
break;
}
int end = enc.encodeMore(buffer, offset, offset+len);
// regular or CDATA?
boolean cdata = r.nextBoolean() && r.nextBoolean();
if (cdata) {