RockerUtil { static public List<String> getTextAsJavaByteArrayInitializer(String text, String charsetName, int maxArraySize) throws UnsupportedEncodingException { byte[] bytes = text.getBytes(charsetName); List<String> arrays = new ArrayList<>(); for (int length = 0; length < bytes.length; ) { StringBuilder sb = new StringBuilder(); sb.append("new byte[] { "); for (int chunk = 0; length < bytes.length && chunk < maxArraySize; chunk++) { byte b = bytes[length]; if (chunk != 0) { sb.append(", "); } appendByteAsJavaByteInitializer(sb, b); chunk++; length++; } sb.append(" };"); arrays.add(sb.toString()); } return arrays; }  static boolean isJava8Plus(TemplateModel model); static boolean isJavaIdentifier(String identifier); static String pathToPackageName(Path path); static Path packageNameToPath(String packageName); static boolean isRelativePath(Path baseDir, Path file); static int countChars(String value, char ch); static boolean isWhitespaceNoLineBreak(char c); static boolean isWhitespace(char c); static boolean isWhitespace(String s); static String consoleFriendlyText(String s); static String templateNameToName(String templateName); static ContentType templateNameToContentType(String templateName); static Collection<File> listFileTree(File dir); static String qualifiedClassName(Object obj); static String qualifiedClassName(Class<?> type); static String unqualifiedClassName(Object obj); static String unqualifiedClassName(Class<?> type); static String chompClosureOpen(String expr); static String chompClosureAssignmentOpen(String expr); static List<String> stringIntoChunks(String s, int chunkSize); static List<String> getTextAsJavaByteArrayInitializer(String text, String charsetName, int maxArraySize); static void appendByteAsJavaByteInitializer(StringBuilder sb, byte b); static String md5(File f); static String byteArrayToHex(byte[] a); static final CharSequenceTranslator ESCAPE_JAVA; }
@Test public void getTextAsJavaByteArrayInitializer() throws Exception { Assert.assertEquals("new byte[] { 'h', 't', 'm', 'l' };", RockerUtil.getTextAsJavaByteArrayInitializer("html", "UTF-8", 4096).get(0)); Assert.assertEquals("new byte[] { (byte)0xe2, (byte)0x82, (byte)0xac };", RockerUtil.getTextAsJavaByteArrayInitializer("\u20AC", "UTF-8", 4096).get(0)); List<String> byteArrays = RockerUtil.getTextAsJavaByteArrayInitializer("\u20AC", "UTF-8", 1); Assert.assertEquals("new byte[] { (byte)0xe2 };", byteArrays.get(0)); Assert.assertEquals("new byte[] { (byte)0x82 };", byteArrays.get(1)); Assert.assertEquals("new byte[] { (byte)0xac };", byteArrays.get(2)); }