/*
 * Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.multipart;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

public class StringPart extends PartBase {

    /**
     * Default content encoding of string parameters.
     */
    public static final String DEFAULT_CONTENT_TYPE = "text/plain";

    /**
     * Default charset of string parameters
     */
    public static final String DEFAULT_CHARSET = "US-ASCII";

    /**
     * Default transfer encoding of string parameters
     */
    public static final String DEFAULT_TRANSFER_ENCODING = "8bit";

    /**
     * Contents of this StringPart.
     */
    private final byte[] content;

    public StringPart(String name, String value, String charset) {
        this(name, value, charset, null);
    }

    /**
     * Constructor.
     * 
     * @param name
     *            The name of the part
     * @param value
     *            the string to post
     * @param charset
     *            the charset to be used to encode the string, if <code>null</code> the {@link #DEFAULT_CHARSET default} is used
     * @param contentId
     *            the content id
     */
    public StringPart(String name, String value, String charset, String contentId) {

        super(name, DEFAULT_CONTENT_TYPE, charset == null ? DEFAULT_CHARSET : charset, DEFAULT_TRANSFER_ENCODING, contentId);
        if (value == null) {
            throw new NullPointerException("value");
        }
        if (value.indexOf(0) != -1) {
            // See RFC 2048, 2.8. "8bit Data"
            throw new IllegalArgumentException("NULs may not be present in string parts");
        }
        content = value.getBytes(Charset.forName(charset));
    }

    /**
     * Writes the data to the given OutputStream.
     * 
     * @param out
     *            the OutputStream to write to
     * @throws java.io.IOException
     *             if there is a write error
     */
    protected void sendData(OutputStream out) throws IOException {
        out.write(content);
    }

    /**
     * Return the length of the data.
     * 
     * @return The length of the data.
     */
    protected long getDataLength() {
        return content.length;
    }

    public byte[] getBytes(byte[] boundary) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        write(outputStream, boundary);
        return outputStream.toByteArray();
    }

    @Override
    public long write(WritableByteChannel target, byte[] boundary) throws IOException {
        return MultipartUtils.writeBytesToChannel(target, getBytes(boundary));
    }
}

@@ -909,16 +909,67 @@
 arset;%0A%0A
+import org.asynchttpclient.util.StandardCharsets;%0A%0A
 public c
@@ -1219,38 +1219,39 @@
 ic static final 
-String
+Charset
  DEFAULT_CHARSET
@@ -1257,18 +1257,33 @@
 T = 
-%22US-
+StandardCharsets.US_
 ASCII
-%22
 ;%0A%0A 
@@ -1506,16 +1506,162 @@
 ntent;%0A%0A
+    private static Charset charsetOrDefault(String charset) %7B%0A        return charset == null ? DEFAULT_CHARSET : Charset.forName(charset);%0A    %7D%0A%0A
     publ
@@ -2274,44 +2274,33 @@
 rset
- == null ? DEFAULT_CHARSET : charset
+OrDefault(charset).name()
 , DE
@@ -2362,18 +2362,16 @@
 == null)
- %7B
 %0A       
@@ -2412,34 +2412,24 @@
 n(%22value%22);%0A
-        %7D%0A
         if (
@@ -2451,18 +2451,16 @@
 ) != -1)
- %7B
 %0A       
@@ -2593,18 +2593,8 @@
 %22);%0A
-        %7D%0A
     
@@ -2622,31 +2622,32 @@
 etBytes(
-C
+c
 harset
-.forName
+OrDefault
 (charset

Fix NPE when passing a null charset, close #651