/** * Creates and initializes a new injector. * * @param tagToInject * the tag which this injector should try to inject. */ public StreamedHtmlScriptInjector(String tagToInject) { htmlSource = new CharacterRingBuffer(); tokenParser = new HtmlTokenParser(htmlSource, 0); status = Status.SCAN_XML_DECLARATION; this.tagToInject = tagToInject; }
/** * Creates a ringbuffer with the given initial contents. * * @param content * the intial contents of the buffer. */ public CharacterRingBuffer(CharSequence content) { int cap = DEFAULT_CAPACITY; while (cap < content.length()) { cap *= 2; } buffer = new char[cap]; beginOffset = 0; length = 0; append(content); }
/** * {@inheritDoc} */ @Override public char charAt(int index) { if ((index < 0) || (index >= length)) { throw new IllegalArgumentException("Index out of bounds (" + index + ")"); } return CharacterRingBuffer.this.charAt(index + offset); }
@Test public void testWrapped() { CharacterRingBuffer rb = new CharacterRingBuffer(5); rb.append("abcd"); rb.erase(2); rb.append("efghij"); String result = rb.toString(); assertThat(result, equalTo("cdefghij")); } }
@Test public void testWrapped() { CharacterRingBuffer rb = new CharacterRingBuffer(5); rb.append("ab012"); rb.erase(2); rb.append("3456789"); String subSequence = rb.subSequence(3, 8).toString(); assertThat(subSequence, equalTo("34567")); }
@Test public void testWithWrapping() { CharacterRingBuffer rb = new CharacterRingBuffer(10); rb.append("123456789"); rb.erase(5); rb.append("abcd"); for (int pos = 0; pos < "6789abcd".length(); pos++) { assertThat(rb.charAt(pos), equalTo("6789abcd".charAt(pos))); } }
@Test public void testSimple() { CharacterRingBuffer rb = new CharacterRingBuffer("0123456789"); rb.erase(3); String result = rb.toString(); assertThat(result, equalTo("3456789")); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void testNegativeIndex() { CharacterRingBuffer rb = new CharacterRingBuffer("test"); rb.charAt(-1); }
@Test public void testSimple() { CharacterRingBuffer rb = new CharacterRingBuffer(5); rb.append("abcd"); rb.append("efghij"); String result = rb.toString(); assertThat(result, equalTo("abcdefghij")); }
@Test public void testSimple() { CharacterRingBuffer rb = new CharacterRingBuffer("abcdefg"); String result = rb.toString(); assertThat(result, equalTo("abcdefg")); }
@Test public void testSimple() { CharacterRingBuffer rb = new CharacterRingBuffer("0123456789"); CharSequence subSequence = rb.subSequence(3, 8); assertThat(subSequence.toString(), equalTo("34567")); }
/** * {@inheritDoc} */ @Override public String toString() { return getAsString(0, length); }
/** * Appends the given character sequence to the end of this buffer, resizing the buffer if * necessary.<br> * Runtime complexity of this operation is amortised O(n) where n is the length of the String to * append. * * @param sequence * the character sequence to append to the end of the buffer */ public final void append(CharSequence sequence) { int len = sequence.length(); ensureCapacity(length + len); for (int pos = 0; pos < len; pos++) { buffer[(beginOffset + length + pos) % buffer.length] = sequence.charAt(pos); } length += len; }
@Test public void testWithWrapping() { CharacterRingBuffer rb = new CharacterRingBuffer(10); rb.append("123456789"); rb.erase(5); rb.append("abcd"); String result = rb.toString(); assertThat(result, equalTo("6789abcd")); } }
@Test public void testSubSequence() { CharacterRingBuffer rb = new CharacterRingBuffer(5); rb.append("ab012"); rb.erase(2); rb.append("3456789"); CharSequence subSubSequence = rb.subSequence(3, 8).subSequence(1, 4); assertThat(subSubSequence.toString(), equalTo("456")); } }
@Test public void testClear() { CharacterRingBuffer rb = new CharacterRingBuffer("0123456789"); rb.erase(10); String result = rb.toString(); assertThat(result, equalTo("")); } }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void testOutOfBoundsIndex() { CharacterRingBuffer rb = new CharacterRingBuffer("test"); rb.charAt(4); } }
@Test public void testSimple() { CharacterRingBuffer rb = new CharacterRingBuffer("abcdefg"); for (int pos = 0; pos < "abcdefg".length(); pos++) { assertThat(rb.charAt(pos), equalTo("abcdefg".charAt(pos))); } }