Description
Constructs a new
String
by decoding the specified array of bytes using the given charset.
License
Apache License
Parameter
Parameter | Description |
---|
bytes | The bytes to be decoded into characters, may be <code>null</code> |
charsetName | The name of a required java.nio.charset.Charset |
Exception
Parameter | Description |
---|
IllegalStateException | Thrown when a UnsupportedEncodingException is caught, which should never happen for arequired charset name. |
Return
A new
String
decoded from the specified array of bytes using the given charset, or
null
if the input byte array was
null
.
Declaration
public static String newString(byte[] bytes, String charsetName)
Method Source Code
//package com.java2s;
/*// w w w .j a va 2 s .c o m
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/
public class Main {
/**
* Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
* <p>
* This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
* should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
* </p>
*
* @param bytes
* The bytes to be decoded into characters, may be <code>null</code>
* @param charsetName
* The name of a required {@link java.nio.charset.Charset}
* @return A new <code>String</code> decoded from the specified array of bytes using the given charset,
* or <code>null</code> if the input byte array was <code>null</code>.
* @throws IllegalStateException
* Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
* required charset name.
* @see CharEncoding
* @see String#String(byte[], String)
*/
public static String newString(byte[] bytes, String charsetName) {
if (bytes == null) {
return null;
}
try {
return new String(bytes, charsetName);
} catch (Exception e) {
// throw StringUtils.newIllegalStateException(charsetName, e);
}
return null;
}
}
Related
- getString(byte[] bytesIn, Charset cs)
- identify(byte[] bytes, CharsetDecoder decoder)
- inflate(byte[] bytes, Charset encoding)
- isCharset(byte[] b, String inCharset)
- newString(byte[] bytes, Charset charset)
- newString(final byte[] bytes, final Charset charset)
- newString(final byte[] bytes, final Charset charset)
- newStringFromSplit(CharsetDecoder decoder, CharsetDecoder utf8Decoder, String encoding, byte[] fieldBytes, int length)
- parseBytes(byte[] encoded, Charset charset)