/** * Test setting negative position. */ @Test public void testSetNegativePosition() throws Exception { stream.write(new byte[BUFFER_SIZE]); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Position out of bounds"); stream.setPosition(-1); }
/** * Test that toString returns a substring of the buffer with range(0, position). */ @Test public void testToString() throws IOException { byte[] data = "1234567890".getBytes(ConfigConstants.DEFAULT_CHARSET); ByteArrayOutputStreamWithPos stream = new ByteArrayOutputStreamWithPos(data.length); stream.write(data); Assert.assertArrayEquals(data, stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); for (int i = 0; i < data.length; i++) { stream.setPosition(i); Assert.assertArrayEquals(Arrays.copyOf(data, i), stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); } // validate that the stored bytes are still tracked properly even when expanding array stream.setPosition(data.length + 1); Assert.assertArrayEquals(Arrays.copyOf(data, data.length + 1), stream.toString().getBytes(ConfigConstants.DEFAULT_CHARSET)); } }
/** * Test setting position which is exactly the same with the buffer size. */ @Test public void testSetPositionWhenBufferIsFull() throws Exception { stream.write(new byte[BUFFER_SIZE]); // check whether the buffer is filled fully Assert.assertEquals(BUFFER_SIZE, stream.getBuf().length); // check current position is the end of the buffer Assert.assertEquals(BUFFER_SIZE, stream.getPosition()); stream.setPosition(BUFFER_SIZE); // confirm current position is at where we expect. Assert.assertEquals(BUFFER_SIZE, stream.getPosition()); }
/** * Test setting position larger than buffer size. */ @Test public void testSetPositionLargerThanBufferSize() throws Exception { // fully fill the buffer stream.write(new byte[BUFFER_SIZE]); Assert.assertEquals(BUFFER_SIZE, stream.getBuf().length); // expand the buffer by setting position beyond the buffer length stream.setPosition(BUFFER_SIZE + 1); Assert.assertEquals(BUFFER_SIZE * 2, stream.getBuf().length); Assert.assertEquals(BUFFER_SIZE + 1, stream.getPosition()); }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); isEmpty = false; }
@Override public void write(int b) throws IOException { os.write(b); isEmpty = false; }
@Override public void write(int b) throws IOException { os.write(b); isEmpty = false; }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); isEmpty = false; }
@Override public void write(int b) throws IOException { os.write(b); isEmpty = false; }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); isEmpty = false; }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); }
@Override public void write(int b) throws IOException { os.write(b); isEmpty = false; }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); isEmpty = false; }
@Override public void write(MemorySegment segment, int off, int len) throws IOException { os.write(segment, off, len); isEmpty = false; }
@Override public void write(int b) throws IOException { os.write(b); }
@Override public void write(byte[] b, int off, int len) throws IOException { os.write(b, off, len); }
@Override public void write(int b) throws IOException { os.write(b); }
public static void serializeGroupPrefix( ByteArrayOutputStreamWithPos outputStream, int group, @Nullable byte[] stateNameBytes) throws IOException { writeGroup(outputStream, group); if (stateNameBytes != null) { outputStream.write(stateNameBytes); } }
@Override public byte[] deserialize(DataInputView source) throws IOException { ByteArrayOutputStreamWithPos output = new ByteArrayOutputStreamWithPos(); byte[] buffer = new byte[1024]; int numBytes; source.skipBytesToRead(1); while ((numBytes = source.read(buffer)) != -1) { output.write(buffer, 0, numBytes); } return output.toByteArray(); }