UnsyncBufferedWriter extends Writer { @Override public void write(char[] chars, int offset, int length) throws IOException { if (_writer == null) { throw new IOException("Writer is null"); } BoundaryCheckerUtil.check(chars.length, offset, length); if (length >= _size) { if (_count > 0) { _writer.write(_buffer, 0, _count); _count = 0; } _writer.write(chars, offset, length); return; } if ((_count > 0) && (length > (_size - _count))) { _writer.write(_buffer, 0, _count); _count = 0; } System.arraycopy(chars, offset, _buffer, _count, length); _count += length; } UnsyncBufferedWriter(Writer writer);  UnsyncBufferedWriter(Writer writer, int size); @Override void close(); @Override void flush(); void newLine(); @Override void write(char[] chars, int offset, int length); @Override void write(int c); @Override void write(String string, int offset, int length);  }
@Test public void testBlockWrite() throws Exception { StringWriter stringWriter = new StringWriter(); UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter( stringWriter, 3); unsyncBufferedWriter.write("ab".toCharArray()); char[] buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(2, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('a', buffer[0]); Assert.assertEquals('b', buffer[1]); StringBuffer stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write("c".toCharArray()); buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(3, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('a', buffer[0]); Assert.assertEquals('b', buffer[1]); Assert.assertEquals('c', buffer[2]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write("de".toCharArray()); buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(2, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('d', buffer[0]); Assert.assertEquals('e', buffer[1]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(3, stringBuffer.length()); Assert.assertEquals("abc", stringBuffer.toString()); unsyncBufferedWriter.write("fgh".toCharArray()); _bufferField.get(unsyncBufferedWriter); Assert.assertEquals(0, _countField.getInt(unsyncBufferedWriter)); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(8, stringBuffer.length()); Assert.assertEquals("abcdefgh", stringBuffer.toString()); unsyncBufferedWriter.write("ijk".toCharArray()); Assert.assertEquals(0, _countField.getInt(unsyncBufferedWriter)); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(11, stringBuffer.length()); Assert.assertEquals("abcdefghijk", stringBuffer.toString()); }
@Test public void testStringWrite() throws Exception { StringWriter stringWriter = new StringWriter(); UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter( stringWriter, 3); unsyncBufferedWriter.write("ab"); char[] buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(2, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('a', buffer[0]); Assert.assertEquals('b', buffer[1]); StringBuffer stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write("cd"); buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(1, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('d', buffer[0]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(3, stringBuffer.length()); Assert.assertEquals("abc", stringBuffer.toString()); unsyncBufferedWriter.write("efghi".toCharArray()); Assert.assertEquals(0, _countField.getInt(unsyncBufferedWriter)); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(9, stringBuffer.length()); Assert.assertEquals("abcdefghi", stringBuffer.toString()); }
@Test public void testWrite() throws Exception { StringWriter stringWriter = new StringWriter(); UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter( stringWriter, 3); unsyncBufferedWriter.write('a'); char[] buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(1, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('a', buffer[0]); StringBuffer stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write('b'); Assert.assertEquals(2, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('b', buffer[1]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write('c'); Assert.assertEquals(3, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('c', buffer[2]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(0, stringBuffer.length()); unsyncBufferedWriter.write('d'); buffer = (char[])_bufferField.get(unsyncBufferedWriter); Assert.assertEquals(1, _countField.getInt(unsyncBufferedWriter)); Assert.assertEquals('d', buffer[0]); stringBuffer = stringWriter.getBuffer(); Assert.assertEquals(3, stringBuffer.length()); Assert.assertEquals("abc", stringBuffer.toString()); }