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

import java.io.UnsupportedEncodingException;

/**
 * This class is an adaptation of the Apache HttpClient implementation
 *
 * @link http://hc.apache.org/httpclient-3.x/
 */
public class MultipartEncodingUtil {

    public static byte[] getAsciiBytes(String data) {
        try {
            return data.getBytes("US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getAsciiString(final byte[] data) {
        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }

        try {
            return new String(data, "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] getBytes(final String data, String charset) {

        if (data == null) {
            throw new IllegalArgumentException("data may not be null");
        }

        if (charset == null || charset.length() == 0) {
            throw new IllegalArgumentException("charset may not be null or empty");
        }

        try {
            return data.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(String.format("Unsupported encoding: %s", charset));
        }
    }
}

@@ -667,39 +667,27 @@
 ava.
+n
 io.
-UnsupportedEncodingException
+charset.Charset
 ;%0A%0A/
@@ -905,34 +905,16 @@
 data) %7B%0A
-        try %7B%0A    
         
@@ -938,124 +938,37 @@
 tes(
-%22US-ASCII%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+Charset.forName(%22US-ASCII%22));
 %0A   
@@ -1086,59 +1086,34 @@
 new 
-IllegalArgumentException(%22Parameter may not be null
+NullPointerException(%22data
 %22);%0A
@@ -1123,34 +1123,16 @@
      %7D%0A%0A
-        try %7B%0A    
         
@@ -1159,124 +1159,37 @@
 ta, 
-%22US-ASCII%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+Charset.forName(%22US-ASCII%22));
 %0A   
@@ -1318,54 +1318,34 @@
 new 
-IllegalArgumentException(%22data may not be null
+NullPointerException(%22data
 %22);%0A
@@ -1429,39 +1429,35 @@
   throw new 
-IllegalArgument
+NullPointer
 Exception(%22c
@@ -1466,33 +1466,8 @@
 rset
- may not be null or empty
 %22);%0A
@@ -1481,26 +1481,8 @@
  %7D%0A%0A
-        try %7B%0A    
     
@@ -1510,178 +1510,34 @@
 tes(
-c
+C
 harset
-);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new IllegalArgumentException(String.format(%22Unsupported encoding: %25s%22, charset));%0A        %7D
+.forName(charset));
 %0A   

Use Charset.forName in order to get unchecked UnsupportedCharsetException