/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16BE charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16Be(final byte[] bytes) { return newString(bytes, Charsets.UTF_16BE); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the US-ASCII charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUsAscii(final byte[] bytes) { return newString(bytes, Charsets.US_ASCII); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16 charset * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16(final byte[] bytes) { return newString(bytes, Charsets.UTF_16); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset. * * @param bytes * The bytes to be decoded into characters, may be <code>null</code> * @return A new <code>String</code> decoded from the specified array of bytes using the ISO-8859-1 charset, or * <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringIso8859_1(final byte[] bytes) { return newString(bytes, Charsets.ISO_8859_1); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16LE charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16Le(final byte[] bytes) { return newString(bytes, Charsets.UTF_16LE); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
private void testNewString(final String charsetName) throws UnsupportedEncodingException { final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newString(BYTES_FIXTURE, charsetName); Assert.assertEquals(expected, actual); }
@Test public void testNewStringBadEnc() { try { StringUtils.newString(BYTES_FIXTURE, "UNKNOWN"); Assert.fail("Expected " + IllegalStateException.class.getName()); } catch (final IllegalStateException e) { // Expected } }
@Test public void testNewStringNullInput() { Assert.assertNull(StringUtils.newString(null, "UNKNOWN")); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the US-ASCII charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#US_ASCII} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUsAscii(final byte[] bytes) { return newString(bytes, Charsets.US_ASCII); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16 charset * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16(final byte[] bytes) { return newString(bytes, Charsets.UTF_16); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset. * * @param bytes * The bytes to be decoded into characters, may be <code>null</code> * @return A new <code>String</code> decoded from the specified array of bytes using the ISO-8859-1 charset, or * <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#ISO_8859_1} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringIso8859_1(final byte[] bytes) { return newString(bytes, Charsets.ISO_8859_1); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16BE charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16Be(final byte[] bytes) { return newString(bytes, Charsets.UTF_16BE); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-16LE charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_16LE} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf16Le(final byte[] bytes) { return newString(bytes, Charsets.UTF_16LE); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or <code>null</code> if the input byte array was <code>null</code>. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
/** * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset. * * @param bytes * The bytes to be decoded into characters * @return A new <code>String</code> decoded from the specified array of bytes using the UTF-8 charset, * or {@code null} if the input byte array was {@code null}. * @throws NullPointerException * Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is * required by the Java platform specification. * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException */ public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); }
/** * this is to mimic the Xml.decodeAttribute * @param el * @param tag * @return */ protected String xmlDecodeAttribute(Attributes attributes, String tag) { String charset = StringUtils.trimToNull(attributes.getValue("charset")); if (charset == null) charset = "UTF-8"; String body = StringUtils.trimToNull(attributes.getValue(tag)); if (body != null) { try { byte[] decoded = Base64.decodeBase64(body); // UTF-8 by default body = org.apache.commons.codec.binary.StringUtils.newString(decoded, charset); } catch (IllegalStateException e) { M_log.warn(" XmlDecodeAttribute: " + e.getMessage() + " tag=" + tag); } } if (body == null) body = ""; return body; }