### Question:
ByteArrayOutputStream extends OutputStream { public ByteArrayRange closeAndGetByteArray() { ByteArrayRange byteArrayRange = new ByteArrayRange(buffer, 0, count); close(); buffer = null; return byteArrayRange; } ByteArrayOutputStream();  ByteArrayOutputStream(int size); ByteArrayRange closeAndGetByteArray(); void reset(); int getLength(); ByteArrayRange getByteArray(); byte[] toByteArray(); @Override void write(byte[] buffer, int offset, int length); @Override void write(int oneByte);  }### Answer:
@Test public void testCloseAndGetByteArray(){ closedListenerTriggeredCount = 0; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] data = new byte[3]; data[0] = 1; data[1] = 2; data[2] = 3; baos.write(data); baos.setClosedListener(new ClosedListener() { @Override public void onClosed() { closedListenerTriggeredCount++; } }); ByteArrayRange byteRange = baos.closeAndGetByteArray(); byte [] bytes = byteRange.toByteArray(); baos.close(); assertEquals(bytes[0], data[0]); assertEquals(bytes[1], data[1]); assertEquals(1, closedListenerTriggeredCount); }