code: public static byte[] copyByteBufToByteArray(ByteBuf input) {
          byte[] copy;
          int length = input.readableBytes();
          if (input.hasArray()) {
              byte[] inputBytes = input.array();
              int offset = input.arrayOffset() + input.readerIndex();
              copy = Arrays.copyOfRange(inputBytes, offset, offset + length);
              return copy;
          } else {
              copy = new byte[length];
              input.getBytes(input.readerIndex(), copy);
          }
          return copy;
      }
docs: Converts a {@link ByteBuf} to a byte[] in the most straightforward manner available.
  the byte[] returned is a copy of the actual data
  @param input the ByteBuf to convert.
  @return a byte[] array