ByteArrayCoder extends AtomicCoder<byte[]> { public void encodeAndOwn(byte[] value, OutputStream outStream, Context context) throws IOException, CoderException { if (!context.isWholeStream) { VarInt.encode(value.length, outStream); outStream.write(value); } else { if (outStream instanceof ExposedByteArrayOutputStream) { ((ExposedByteArrayOutputStream) outStream).writeAndOwn(value); } else { outStream.write(value); } } } private  ByteArrayCoder(); static ByteArrayCoder of(); @Override void encode(byte[] value, OutputStream outStream); @Override void encode(byte[] value, OutputStream outStream, Context context); void encodeAndOwn(byte[] value, OutputStream outStream, Context context); @Override byte[] decode(InputStream inStream); @Override byte[] decode(InputStream inStream, Context context); @Override void verifyDeterministic(); @Override Object structuralValue(byte[] value); @Override boolean isRegisterByteSizeObserverCheap(byte[] value); @Override TypeDescriptor<byte[]> getEncodedTypeDescriptor();  }
@Test public void testEncodeAndOwn() throws Exception { for (byte[] value : TEST_VALUES) { byte[] encodedSlow = CoderUtils.encodeToByteArray(TEST_CODER, value); byte[] encodedFast = encodeToByteArrayAndOwn(TEST_CODER, value); assertThat(encodedSlow, equalTo(encodedFast)); } }