- private static final class Proxy implements Externalizable {
- private static final long serialVersionUID = 1L;
-
- private ShardSnapshotState snapshotState;
-
- // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
- // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
- @SuppressWarnings("checkstyle:RedundantModifier")
- public Proxy() {
- // For Externalizable
- }
-
- Proxy(final ShardSnapshotState snapshotState) {
- this.snapshotState = snapshotState;
- }
-
- @Override
- public void writeExternal(final ObjectOutput out) throws IOException {
- snapshotState.snapshot.serialize(toOutputStream(out));
- }
-
- private OutputStream toOutputStream(final ObjectOutput out) {
- if (out instanceof OutputStream) {
- return (OutputStream) out;
- }
-
- return new OutputStream() {
- @Override
- public void write(int value) throws IOException {
- out.write(value);
- }
- };
- }
-
- @Override
- public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
- snapshotState = new ShardSnapshotState(ShardDataTreeSnapshot.deserialize(toInputStream(in)));
- }
-
- private InputStream toInputStream(final ObjectInput in) {
- if (in instanceof InputStream) {
- return (InputStream) in;
- }
-
- return new InputStream() {
- @Override
- public int read() throws IOException {
- return in.read();
- }
- };
- }
-
- private Object readResolve() {
- return snapshotState;
- }
- }
-