Skip to content

Commit

Permalink
Make ArrayProcessor and AsyncDeserializationContext visible to AutoCo…
Browse files Browse the repository at this point in the history
…dec.

PiperOrigin-RevId: 587147252
Change-Id: I10fcd0308a7b1c062e5d04b4bed53230cb5c9a5f
  • Loading branch information
aoeui authored and copybara-github committed Dec 1, 2023
1 parent b582d2e commit 0226327
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
*
* <p>Clients should obtain instances using {@link #forType}.
*/
abstract class ArrayProcessor {
public abstract class ArrayProcessor {
/**
* Serializes an array.
*
* @param type the type of the array. Can be a multidimensional array type.
* @param arr the array instance to serialize.
*/
abstract void serialize(
public abstract void serialize(
SerializationContext context, CodedOutputStream codedOut, Class<?> type, Object arr)
throws IOException, SerializationException;

Expand All @@ -52,7 +52,7 @@ abstract void serialize(
* @param obj the object to contain the array, that could be an array itself.
* @param offset offset within obj to write the deserialized value.
*/
abstract void deserialize(
public abstract void deserialize(
AsyncDeserializationContext context,
CodedInputStream codedIn,
Class<?> type,
Expand Down Expand Up @@ -105,9 +105,9 @@ private static Class<?> resolveBaseArrayType(Class<?> arrayType) {
* <p>Subclasses handle the base arrays by implementing {@link #serializeArrayData} and {@link
* #deserializeArrayData}.
*/
private abstract static class PrimitiveArrayProcessor extends ArrayProcessor {
public abstract static class PrimitiveArrayProcessor extends ArrayProcessor {
@Override
final void serialize(
public final void serialize(
SerializationContext context, CodedOutputStream codedOut, Class<?> type, Object arr)
throws IOException {
// The first field is a tag indicating either null or the size of the array to come.
Expand Down Expand Up @@ -138,11 +138,11 @@ final void serialize(
serializeArrayData(codedOut, arr);
}

abstract void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
public abstract void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException;

@Override
final void deserialize(
public final void deserialize(
AsyncDeserializationContext context,
CodedInputStream codedIn,
Class<?> type,
Expand All @@ -169,13 +169,15 @@ final void deserialize(
unsafe().putObject(obj, offset, deserializeArrayData(codedIn, length));
}

abstract Object deserializeArrayData(CodedInputStream codedIn, int length) throws IOException;
public abstract Object deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException;
}

private static final PrimitiveArrayProcessor BOOLEAN_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor BOOLEAN_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
boolean[] values = (boolean[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (boolean value : values) {
Expand All @@ -184,7 +186,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
boolean[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public boolean[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
boolean[] values = new boolean[length];
for (int i = 0; i < length; ++i) {
values[i] = codedIn.readBool();
Expand All @@ -193,10 +196,11 @@ boolean[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOEx
}
};

private static final PrimitiveArrayProcessor BYTE_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor BYTE_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
byte[] values = (byte[]) untypedArr;
int length = values.length;
codedOut.writeInt32NoTag(length + 1);
Expand All @@ -206,15 +210,17 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
byte[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public byte[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
return codedIn.readRawBytes(length);
}
};

private static final PrimitiveArrayProcessor SHORT_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor SHORT_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
short[] values = (short[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (short value : values) {
Expand All @@ -223,7 +229,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
short[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public short[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
short[] values = new short[length];
for (int i = 0; i < length; ++i) {
values[i] = readShort(codedIn);
Expand All @@ -232,10 +239,11 @@ short[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOExce
}
};

private static final PrimitiveArrayProcessor CHAR_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor CHAR_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
char[] values = (char[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (char value : values) {
Expand All @@ -244,7 +252,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
char[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public char[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
char[] values = new char[length];
for (int i = 0; i < length; ++i) {
values[i] = readChar(codedIn);
Expand All @@ -253,10 +262,11 @@ char[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOExcep
}
};

private static final PrimitiveArrayProcessor INT_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor INT_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
int[] values = (int[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (int value : values) {
Expand All @@ -265,7 +275,7 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
int[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public int[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
int[] values = new int[length];
for (int i = 0; i < length; ++i) {
values[i] = codedIn.readInt32();
Expand All @@ -274,10 +284,11 @@ int[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOExcept
}
};

private static final PrimitiveArrayProcessor LONG_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor LONG_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
long[] values = (long[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (long value : values) {
Expand All @@ -286,7 +297,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
long[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public long[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
long[] values = new long[length];
for (int i = 0; i < length; ++i) {
values[i] = codedIn.readInt64();
Expand All @@ -295,10 +307,11 @@ long[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOExcep
}
};

private static final PrimitiveArrayProcessor FLOAT_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor FLOAT_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
float[] values = (float[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (float value : values) {
Expand All @@ -307,7 +320,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
float[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public float[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
float[] values = new float[length];
for (int i = 0; i < length; ++i) {
values[i] = codedIn.readFloat();
Expand All @@ -316,10 +330,11 @@ float[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOExce
}
};

private static final PrimitiveArrayProcessor DOUBLE_ARRAY_PROCESSOR =
public static final PrimitiveArrayProcessor DOUBLE_ARRAY_PROCESSOR =
new PrimitiveArrayProcessor() {
@Override
void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IOException {
public void serializeArrayData(CodedOutputStream codedOut, Object untypedArr)
throws IOException {
double[] values = (double[]) untypedArr;
codedOut.writeInt32NoTag(values.length + 1);
for (double value : values) {
Expand All @@ -328,7 +343,8 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}

@Override
double[] deserializeArrayData(CodedInputStream codedIn, int length) throws IOException {
public double[] deserializeArrayData(CodedInputStream codedIn, int length)
throws IOException {
double[] values = new double[length];
for (int i = 0; i < length; ++i) {
values[i] = codedIn.readDouble();
Expand All @@ -337,13 +353,13 @@ void serializeArrayData(CodedOutputStream codedOut, Object untypedArr) throws IO
}
};

private static final ArrayProcessor OBJECT_ARRAY_PROCESSOR =
public static final ArrayProcessor OBJECT_ARRAY_PROCESSOR =
new ArrayProcessor() {
// Special case uses `Array.newInstance` to also create leaf-level arrays. Additionally,
// unlike the `PrimitiveArrayProcessor`, the unnested arrays rely on serialization contexts.

@Override
void serialize(
public void serialize(
SerializationContext context, CodedOutputStream codedOut, Class<?> type, Object arr)
throws IOException, SerializationException {
// Tagging works exactly the same as PrimitiveArrayProcessor.serialize: 0 for null;
Expand All @@ -367,7 +383,7 @@ void serialize(
}

@Override
void deserialize(
public void deserialize(
AsyncDeserializationContext context,
CodedInputStream codedIn,
Class<?> type,
Expand Down Expand Up @@ -401,7 +417,7 @@ void deserialize(
}
};

private static void serializeObjectArray(
public static void serializeObjectArray(
SerializationContext context, CodedOutputStream codedOut, Object untypedArr)
throws IOException, SerializationException {
Object[] values = (Object[]) untypedArr;
Expand All @@ -411,7 +427,7 @@ private static void serializeObjectArray(
}
}

private static void deserializeObjectArray(
public static void deserializeObjectArray(
AsyncDeserializationContext context, CodedInputStream codedIn, Object arr, int length)
throws IOException, SerializationException {
for (int i = 0; i < length; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//
// Once all codecs are migrated, this should replace the existing DeserializationContext as the
// interface to codecs.
interface AsyncDeserializationContext extends SerializationDependencyProvider {
public interface AsyncDeserializationContext extends SerializationDependencyProvider {

/** Defines a way to set a field in a given object. */
interface FieldSetter {
Expand Down

0 comments on commit 0226327

Please sign in to comment.