/* * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; class BaosTest { void aBad() throws IOException { ByteArrayOutputStream x = new ByteArrayOutputStream(); ObjectOutputStream y = new ObjectOutputStream(x); y.writeObject(1337); byte[] bytes = x.toByteArray(); // This may return partial results. } /** Bugfix for aBad. */ void a1Ok() throws IOException { ByteArrayOutputStream x = new ByteArrayOutputStream(); ObjectOutputStream y = new ObjectOutputStream(x); y.writeObject(1337); y.close(); byte[] bytes = x.toByteArray(); } /** Another bugfix for aBad. */ void a2Ok() throws IOException { ByteArrayOutputStream x = new ByteArrayOutputStream(); ObjectOutputStream y = new ObjectOutputStream(x); y.writeObject(1337); y.flush(); byte[] bytes = x.toByteArray(); } void bBad() throws IOException { ByteArrayOutputStream x = new ByteArrayOutputStream(); ObjectOutputStream y = new ObjectOutputStream(x); y.writeObject(1337); byte[] bytes = x.toByteArray(); y.close(); } void cBad() throws IOException { ByteArrayOutputStream x = new ByteArrayOutputStream(); DataOutputStream y = new DataOutputStream(x); y.writeLong(1337); byte[] bytes = x.toByteArray(); } }