You're right, that method does what i suggested. Unfortunately it doesn't seem to help as i thought it would. I wanted to both eliminate the allocation and any copying done before inserting the String on the Document. I can eliminate the allocation keeping a buffer for a String bellow a certain size (a leak prone technique this where many documents are instantiated), but not the copying since the internal gapvector does not allow subclasses to access the internal char [] a string insertion and use getChars internally. It can only use a char array and then THAT means i must copy, since there is no way to put the string char [] into the gapVector, or to take the gapVector char[] and fill it with the string char[].
<br><br>WARNING : shameless modification of jdk gpl'd code<br><br>An alternative, add one method to the package protected abstract class GapVector:<br><br>protected void replace(int position, int rmSize, String addItems, int addSize) {
<br> int addOffset = 0;<br> if (addSize == 0) {<br> close(position, rmSize);<br> return;<br> } else if (rmSize > addSize) {<br> /* Shrink the end. */<br> close(position+addSize, rmSize-addSize);
<br> } else {<br> /* Grow the end, do two chunks. */<br> //int endSize = addSize - rmSize;<br> int end = open(position + rmSize, addSize - rmSize);<br> //System.arraycopy(addItems, rmSize, array, end, endSize);
<br> addItems.getChars(rmSize, addSize, array, end);<br> addSize = rmSize;<br> }<br> //System.arraycopy(addItems, addOffset, array, position, addSize);<br> addItems.getChars(addOffset, addSize, array, position);
<br>}<br><br>and change the GapContent subclass of gapVector insertString method to this:<br>public UndoableEdit insertString(int where, String str) throws BadLocationException {<br> if (where > length() || where < 0) {
<br> throw new BadLocationException("Invalid insert", length());<br> }<br> //char[] chars = str.toCharArray();<br> //replace(where, 0, chars, chars.length);<br> replace(where, 0, str, str.length
() );<br> return new InsertUndo(where, str.length());<br>}<br><br>Voila. If nothing is wrong in the first function, no copy needed in the Document Hierarchy.<br>Only this doesn't help those of us using 1.5 even if it is accepted as a contribution.
<br><br>I think package protected is evil.<br><br><br>