You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

/*
* 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();
}
}