XMLTreeUtil { public static byte[] insertBetween(byte[] src, int left, int right, String content) { final byte[] contentSrc = content.getBytes(UTF_8); final byte[] newSrc = new byte[left + src.length - right + contentSrc.length - 1]; arraycopy(src, 0, newSrc, 0, left); arraycopy(contentSrc, 0, newSrc, left, contentSrc.length); arraycopy(src, right + 1, newSrc, left + contentSrc.length, src.length - right - 1); return newSrc; } private  XMLTreeUtil(); static byte[] insertBetween(byte[] src, int left, int right, String content); static byte[] insertInto(byte[] src, int pos, String content); static T single(List<T> target); static int lastIndexOf(byte[] src, char c, int fromIdx); static int level(Element element); static int openTagLength(NewElement element); static int closeTagLength(NewElement element); static int attributeLength(NewAttribute attribute); static String tabulate(String src, int tabsCount); static Element asElement(Node node); static List<Element> asElements(NodeList list); static List<R> asElements(NodeList list, ElementMapper<? extends R> mapper); static int indexOf(byte[] src, byte[] target, int fromIdx); static int indexOfAttributeName(byte[] src, byte[] target, int fromIdx); static byte[] replaceAll(byte[] src, byte[] target, byte[] replacement); static int rootStart(byte[] xml); static final Charset UTF_8; static final int SPACES_IN_TAB; }
@Test public void shouldInsertContentBetweenTwoAnchors() { final byte[] src = "<name>content</name>".getBytes(); final byte[] newSrc = insertBetween(src, 6, 12, "new content"); assertEquals(newSrc, "<name>new content</name>".getBytes()); }