001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.util; 028 029import java.io.UnsupportedEncodingException; 030 031import org.apache.http.Consts; 032 033/** 034 * The home for utility methods that handle various encoding tasks. 035 * 036 * 037 * @since 4.0 038 */ 039public final class EncodingUtils { 040 041 /** 042 * Converts the byte array of HTTP content characters to a string. If 043 * the specified charset is not supported, default system encoding 044 * is used. 045 * 046 * @param data the byte array to be encoded 047 * @param offset the index of the first byte to encode 048 * @param length the number of bytes to encode 049 * @param charset the desired character encoding 050 * @return The result of the conversion. 051 */ 052 public static String getString( 053 final byte[] data, 054 final int offset, 055 final int length, 056 final String charset) { 057 Args.notNull(data, "Input"); 058 Args.notEmpty(charset, "Charset"); 059 try { 060 return new String(data, offset, length, charset); 061 } catch (final UnsupportedEncodingException e) { 062 return new String(data, offset, length); 063 } 064 } 065 066 067 /** 068 * Converts the byte array of HTTP content characters to a string. If 069 * the specified charset is not supported, default system encoding 070 * is used. 071 * 072 * @param data the byte array to be encoded 073 * @param charset the desired character encoding 074 * @return The result of the conversion. 075 */ 076 public static String getString(final byte[] data, final String charset) { 077 Args.notNull(data, "Input"); 078 return getString(data, 0, data.length, charset); 079 } 080 081 /** 082 * Converts the specified string to a byte array. If the charset is not supported the 083 * default system charset is used. 084 * 085 * @param data the string to be encoded 086 * @param charset the desired character encoding 087 * @return The resulting byte array. 088 */ 089 public static byte[] getBytes(final String data, final String charset) { 090 Args.notNull(data, "Input"); 091 Args.notEmpty(charset, "Charset"); 092 try { 093 return data.getBytes(charset); 094 } catch (final UnsupportedEncodingException e) { 095 return data.getBytes(); 096 } 097 } 098 099 /** 100 * Converts the specified string to byte array of ASCII characters. 101 * 102 * @param data the string to be encoded 103 * @return The string as a byte array. 104 */ 105 public static byte[] getAsciiBytes(final String data) { 106 Args.notNull(data, "Input"); 107 return data.getBytes(Consts.ASCII); 108 } 109 110 /** 111 * Converts the byte array of ASCII characters to a string. This method is 112 * to be used when decoding content of HTTP elements (such as response 113 * headers) 114 * 115 * @param data the byte array to be encoded 116 * @param offset the index of the first byte to encode 117 * @param length the number of bytes to encode 118 * @return The string representation of the byte array 119 */ 120 public static String getAsciiString(final byte[] data, final int offset, final int length) { 121 Args.notNull(data, "Input"); 122 return new String(data, offset, length, Consts.ASCII); 123 } 124 125 /** 126 * Converts the byte array of ASCII characters to a string. This method is 127 * to be used when decoding content of HTTP elements (such as response 128 * headers) 129 * 130 * @param data the byte array to be encoded 131 * @return The string representation of the byte array 132 */ 133 public static String getAsciiString(final byte[] data) { 134 Args.notNull(data, "Input"); 135 return getAsciiString(data, 0, data.length); 136 } 137 138 /** 139 * This class should not be instantiated. 140 */ 141 private EncodingUtils() { 142 } 143 144}