本文整理汇总了Java中it.unimi.dsi.fastutil.bytes.ByteArrayList.toByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayList.toByteArray方法的具体用法?Java ByteArrayList.toByteArray怎么用?Java ByteArrayList.toByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类it.unimi.dsi.fastutil.bytes.ByteArrayList
的用法示例。
在下文中一共展示了ByteArrayList.toByteArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deflateString
import it.unimi.dsi.fastutil.bytes.ByteArrayList; //导入方法依赖的package包/类
/**
* ZLIB compress a {@code String}.
* <p>
* This only produces ZLIB format using {@code Deflater}.
*
* @param input the string to compress, not null
* @return the compressed bytes, not null
*/
public static byte[] deflateString(final String input) {
ArgumentChecker.notNull(input, "input");
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
deflater.setInput(bytes);
ByteArrayList collector = new ByteArrayList(bytes.length + 32);
byte[] buf = new byte[1024];
deflater.finish();
while (deflater.finished() == false) {
int size = deflater.deflate(buf);
collector.addElements(collector.size(), buf, 0, size);
}
deflater.end();
return collector.toByteArray();
}
示例2: inflateString
import it.unimi.dsi.fastutil.bytes.ByteArrayList; //导入方法依赖的package包/类
/**
* ZLIB uncompress to a {@code String}.
* <p>
* This only handles ZLIB format using {@code Inflater}.
*
* @param input the bytes to compress, not null
* @return the compressed string, not null
*/
public static String inflateString(final byte[] input) {
ArgumentChecker.notNull(input, "input");
try {
Inflater inflater = new Inflater();
inflater.setInput(input);
ByteArrayList collector = new ByteArrayList(input.length * 4);
byte[] buf = new byte[1024];
while (inflater.finished() == false) {
int size = inflater.inflate(buf);
collector.addElements(collector.size(), buf, 0, size);
}
inflater.end();
byte[] bytes = collector.toByteArray();
return new String(bytes, StandardCharsets.UTF_8);
} catch (DataFormatException ex) {
throw new OpenGammaRuntimeException(ex.getMessage(), ex);
}
}