List of usage examples for org.apache.commons.codec.binary StringUtils getBytesUnchecked
public static byte[] getBytesUnchecked(final String string, final String charsetName)
From source file:com.mirth.connect.donkey.util.StringUtil.java
/** * A memory efficient method for getting bytes from a string. It first calculates the required * byte array size to avoid any buffers growing out of control. * The main purpose of the method is to alleviate the use case when charset = "UTF-8". For some * reason, using new String(string, "UTF-8") requires * memory many times the size of the string itself. There is a performance decrease over the * Java method of about 30-100%, so this method would ideally only * be used when memory is an issue.//from www .j av a 2 s .c om */ public static byte[] getBytesUncheckedChunked(String string, String charset) { int offset = 0; int length = string.length(); // Calculate the size of the string after byte encoding ByteCounterOutputStream outputStream = new ByteCounterOutputStream(); while ((length - offset) > 0) { int segmentSize = Math.min(CHUNK_SIZE, length - offset); outputStream .write(StringUtils.getBytesUnchecked(string.substring(offset, offset + segmentSize), charset)); offset += segmentSize; } // Create a byte array the size of the exact size required byte[] data = new byte[outputStream.size()]; int position = 0; offset = 0; // Perform the conversion again and write the data to the byte array while ((length - offset) > 0) { int segmentSize = Math.min(CHUNK_SIZE, length - offset); byte[] segment = StringUtils.getBytesUnchecked(string.substring(offset, offset + segmentSize), charset); System.arraycopy(segment, 0, data, position, segment.length); offset += segmentSize; position += segment.length; } return data; }
From source file:yoyo.framework.standard.shared.commons.codec.StringUtilsTest.java
@Test public void test() { String testee = org.apache.commons.lang3.StringUtils.EMPTY; assertThat(StringUtils.getBytesUnchecked(testee, "UTF-8"), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesIso8859_1(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesUsAscii(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesUtf8(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesUtf16(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesUtf16Le(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.getBytesUtf16Be(testee), is(ArrayUtils.EMPTY_BYTE_ARRAY)); assertThat(StringUtils.newString(ArrayUtils.EMPTY_BYTE_ARRAY, "UTF-8"), is(testee)); assertThat(StringUtils.newStringIso8859_1(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); assertThat(StringUtils.newStringUsAscii(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); assertThat(StringUtils.newStringUtf8(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); assertThat(StringUtils.newStringUtf16(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); assertThat(StringUtils.newStringUtf16Le(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); assertThat(StringUtils.newStringUtf16Be(ArrayUtils.EMPTY_BYTE_ARRAY), is(testee)); }