@Override
  public void write(OutputStream outputStream) throws IOException {
    if (isMultipart()) {
      for (FormData data : getEntries()) {
        // Write the boundary line
        outputStream.write(("--" + getMultipartBoundary()).getBytes());
        HeaderUtils.writeCRLF(outputStream);

        // Write the optional content type header line
        if (MediaType.TEXT_PLAIN.equals(data.getMediaType())) {
          // Write the content disposition header line
          String line = "Content-Disposition: form-data; name=\"" + data.getName() + "\"";
          outputStream.write(line.getBytes());
          HeaderUtils.writeCRLF(outputStream);
        } else {
          // Write the content disposition header line
          String line =
              "Content-Disposition: form-data; name=\""
                  + data.getName()
                  + "\"; filename=\""
                  + data.getFilename()
                  + "\"";
          outputStream.write(line.getBytes());
          HeaderUtils.writeCRLF(outputStream);

          // Write the content type header line
          line = "Content-Type: " + ContentType.writeHeader(data.getValueRepresentation());
          outputStream.write(line.getBytes());
          HeaderUtils.writeCRLF(outputStream);
        }

        // Write the data content
        HeaderUtils.writeCRLF(outputStream);
        data.getValueRepresentation().write(outputStream);
        HeaderUtils.writeCRLF(outputStream);
      }

      // Write the final boundary line
      outputStream.write(("--" + getMultipartBoundary() + "--").getBytes());
      HeaderUtils.writeCRLF(outputStream);
    } else {
      Representation formRep =
          new StringRepresentation(
              getQueryString(), MediaType.APPLICATION_WWW_FORM, null, CharacterSet.UTF_8);
      formRep.write(outputStream);
    }
  }