package at.ac.univie.isc.asio.io;

import at.ac.univie.isc.asio.Unchecked;
import com.google.common.io.ByteSource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * Aid in generating and using binary data.
 */
public final class Payload {
  private static final Random RNG = new Random();

  private Payload() { /* no instances */ }

  /**
   * Generate a fixed size {@code byte[]} with random contents.
   * @param size of generated array
   * @return randomly filled byte array
   */
  public static byte[] randomWithLength(final int size) {
    assert size >= 0 : "payload cannot have negative size";
    final byte[] payload = new byte[size];
    RNG.nextBytes(payload);
    return payload;
  }

  /**
   * Convert raw utf-8 encoded data back to {@code String}. It is not checked whether the data is actually
   * utf-8 encoded.
   * @param data raw binary data of an utf-8 encoded string
   * @return decoded data as String
   */
  public static String decodeUtf8(final byte[] data) {
    return new String(data, StandardCharsets.UTF_8);
  }

  /**
   * Convert a {@code String} to utf-8 encoded data.
   * @param text to be converted
   * @return utf-8 encoded binary data
   */
  public static byte[] encodeUtf8(final String text) {
    return text.getBytes(StandardCharsets.UTF_8);
  }

  /**
   * Copy the content from given {@code ByteSource} to an array.
   * @param source source of binary data
   * @return byte array containing the contents of the {@code source}
   * @throw UncheckedIOException if reading the source fails
   */
  public static byte[] asArray(final ByteSource source) {
    try {
      return source.read();
    } catch (IOException e) {
      throw new Unchecked.UncheckedIOException(e);
    }
  }
}

@@ -451,16 +451,21 @@
 ntents.%0A
+   *%0A
    * @pa
@@ -488,24 +488,24 @@
 rated array%0A
-
    * @return
@@ -748,24 +748,622 @@
 yload;%0A  %7D%0A%0A
+  private static final char%5B%5D ALPHA_NUM = %22ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890%22.toCharArray();%0A%0A  /**%0A   * Generate a fixed size %7B@code String%7D with random, alpha-numeric content.%0A   *%0A   * @param length length of generated text%0A   * @return random, alpha-numeric string%0A   */%0A  public static String randomText(final int length) %7B%0A    assert length %3E= 0 : %22payload cannot have negative size%22;%0A    final char%5B%5D content = new char%5Blength%5D;%0A    for (int i = 0; i %3C content.length; i++) %7B%0A      content%5Bi%5D = ALPHA_NUM%5BRNG.nextInt(ALPHA_NUM.length)%5D;%0A    %7D%0A    return String.copyValueOf(content);%0A  %7D%0A%0A
   /**%0A   * C
@@ -1483,16 +1483,21 @@
 ncoded.%0A
+   *%0A
    * @pa
@@ -1762,16 +1762,21 @@
 d data.%0A
+   *%0A
    * @pa
@@ -1957,24 +1957,24 @@
 %0A  %7D%0A%0A  /**%0A
-
    * Copy th
@@ -2026,16 +2026,21 @@
  array.%0A
+   *%0A
    * @pa

Add Paylod#randomText