This commit is contained in:
redkale
2024-09-25 23:27:33 +08:00
parent 1d4a1d4b66
commit 4028c9a465
6 changed files with 529 additions and 129 deletions

View File

@@ -0,0 +1,309 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.convert.pb;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.redkale.convert.SimpledCoder;
import org.redkale.util.Utility;
/**
*
* @author zhangjx
*/
public class ProtobufCoders {
private ProtobufCoders() {
// do nothing
}
public static class ProtobufBoolArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, boolean[]>
implements ProtobufPrimitivable {
public static final ProtobufBoolArraySimpledCoder instance = new ProtobufBoolArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, boolean[] values) {
out.writeBools(values);
}
@Override
public boolean[] convertFrom(ProtobufReader in) {
return in.readBools();
}
}
public static class ProtobufByteArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, byte[]>
implements ProtobufPrimitivable {
public static final ProtobufByteArraySimpledCoder instance = new ProtobufByteArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, byte[] values) {
out.writeBytes(values);
}
@Override
public byte[] convertFrom(ProtobufReader in) {
return in.readBytes();
}
}
public static class ProtobufCharArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, char[]>
implements ProtobufPrimitivable {
public static final ProtobufCharArraySimpledCoder instance = new ProtobufCharArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, char[] values) {
out.writeChars(values);
}
@Override
public char[] convertFrom(ProtobufReader in) {
return in.readChars();
}
}
public static class ProtobufShortArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, short[]>
implements ProtobufPrimitivable {
public static final ProtobufShortArraySimpledCoder instance = new ProtobufShortArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, short[] values) {
out.writeShorts(values);
}
@Override
public short[] convertFrom(ProtobufReader in) {
return in.readShorts();
}
}
public static class ProtobufIntArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, int[]>
implements ProtobufPrimitivable {
public static final ProtobufIntArraySimpledCoder instance = new ProtobufIntArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, int[] values) {
out.writeInts(values);
}
@Override
public int[] convertFrom(ProtobufReader in) {
return in.readInts();
}
}
public static class ProtobufFloatArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, float[]>
implements ProtobufPrimitivable {
public static final ProtobufFloatArraySimpledCoder instance = new ProtobufFloatArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, float[] values) {
out.writeFloats(values);
}
@Override
public float[] convertFrom(ProtobufReader in) {
return in.readFloats();
}
}
public static class ProtobufLongArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, long[]>
implements ProtobufPrimitivable {
public static final ProtobufLongArraySimpledCoder instance = new ProtobufLongArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, long[] values) {
out.writeLongs(values);
}
@Override
public long[] convertFrom(ProtobufReader in) {
return in.readLongs();
}
}
public static class ProtobufDoubleArraySimpledCoder extends SimpledCoder<ProtobufReader, ProtobufWriter, double[]>
implements ProtobufPrimitivable {
public static final ProtobufDoubleArraySimpledCoder instance = new ProtobufDoubleArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, double[] values) {
out.writeDoubles(values);
}
@Override
public double[] convertFrom(ProtobufReader in) {
return in.readDoubles();
}
}
public static class ProtobufBoolArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Boolean[]>
implements ProtobufPrimitivable {
public static final ProtobufBoolArraySimpledCoder2 instance = new ProtobufBoolArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Boolean[] values) {
out.writeBools(values);
}
@Override
public Boolean[] convertFrom(ProtobufReader in) {
return Utility.box(in.readBools());
}
}
public static class ProtobufByteArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Byte[]>
implements ProtobufPrimitivable {
public static final ProtobufByteArraySimpledCoder2 instance = new ProtobufByteArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Byte[] values) {
out.writeBytes(values);
}
@Override
public Byte[] convertFrom(ProtobufReader in) {
return Utility.box(in.readBytes());
}
}
public static class ProtobufCharArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Character[]>
implements ProtobufPrimitivable {
public static final ProtobufCharArraySimpledCoder2 instance = new ProtobufCharArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Character[] values) {
out.writeChars(values);
}
@Override
public Character[] convertFrom(ProtobufReader in) {
return Utility.box(in.readChars());
}
}
public static class ProtobufShortArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Short[]>
implements ProtobufPrimitivable {
public static final ProtobufShortArraySimpledCoder2 instance = new ProtobufShortArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Short[] values) {
out.writeShorts(values);
}
@Override
public Short[] convertFrom(ProtobufReader in) {
return Utility.box(in.readShorts());
}
}
public static class ProtobufIntArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Integer[]>
implements ProtobufPrimitivable {
public static final ProtobufIntArraySimpledCoder2 instance = new ProtobufIntArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Integer[] values) {
out.writeInts(values);
}
@Override
public Integer[] convertFrom(ProtobufReader in) {
return Utility.box(in.readInts());
}
}
public static class ProtobufFloatArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Float[]>
implements ProtobufPrimitivable {
public static final ProtobufFloatArraySimpledCoder2 instance = new ProtobufFloatArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Float[] values) {
out.writeFloats(values);
}
@Override
public Float[] convertFrom(ProtobufReader in) {
return Utility.box(in.readFloats());
}
}
public static class ProtobufLongArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Long[]>
implements ProtobufPrimitivable {
public static final ProtobufLongArraySimpledCoder2 instance = new ProtobufLongArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Long[] values) {
out.writeLongs(values);
}
@Override
public Long[] convertFrom(ProtobufReader in) {
return Utility.box(in.readLongs());
}
}
public static class ProtobufDoubleArraySimpledCoder2 extends SimpledCoder<ProtobufReader, ProtobufWriter, Double[]>
implements ProtobufPrimitivable {
public static final ProtobufDoubleArraySimpledCoder2 instance = new ProtobufDoubleArraySimpledCoder2();
@Override
public void convertTo(ProtobufWriter out, Double[] values) {
out.writeDoubles(values);
}
@Override
public Double[] convertFrom(ProtobufReader in) {
return Utility.box(in.readDoubles());
}
}
public static class ProtobufAtomicIntegerArraySimpledCoder
extends SimpledCoder<ProtobufReader, ProtobufWriter, AtomicInteger[]> implements ProtobufPrimitivable {
public static final ProtobufAtomicIntegerArraySimpledCoder instance =
new ProtobufAtomicIntegerArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, AtomicInteger[] values) {
out.writeAtomicIntegers(values);
}
@Override
public AtomicInteger[] convertFrom(ProtobufReader in) {
return in.readAtomicIntegers();
}
}
public static class ProtobufAtomicLongArraySimpledCoder
extends SimpledCoder<ProtobufReader, ProtobufWriter, AtomicLong[]> implements ProtobufPrimitivable {
public static final ProtobufAtomicLongArraySimpledCoder instance = new ProtobufAtomicLongArraySimpledCoder();
@Override
public void convertTo(ProtobufWriter out, AtomicLong[] values) {
out.writeAtomicLongs(values);
}
@Override
public AtomicLong[] convertFrom(ProtobufReader in) {
return in.readAtomicLongs();
}
}
}

View File

@@ -19,7 +19,6 @@ import org.redkale.asm.MethodVisitor;
import org.redkale.asm.Opcodes;
import static org.redkale.asm.Opcodes.*;
import org.redkale.convert.*;
import org.redkale.convert.ext.*;
import org.redkale.util.AnyValue;
import org.redkale.util.RedkaleClassLoader;
import org.redkale.util.RedkaleException;
@@ -367,61 +366,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
if (!(type instanceof Class)) {
return null;
}
if (AnyValue.class.isAssignableFrom((Class) type)) {
return null;
}
// 发现有自定义的基础数据类型Encoder就不动态生成ProtobufDynEncoder了
if (factory.loadEncoder(boolean.class) != BoolSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(byte.class) != ByteSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(short.class) != ShortSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(char.class) != CharSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(int.class) != IntSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(float.class) != FloatSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(long.class) != LongSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(double.class) != DoubleSimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(String.class) != StringSimpledCoder.instance) {
return null;
}
// array
if (factory.loadEncoder(boolean[].class) != BoolArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(byte[].class) != ByteArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(short[].class) != ShortArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(char[].class) != CharArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(int[].class) != IntArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(float[].class) != FloatArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(long[].class) != LongArraySimpledCoder.instance) {
return null;
}
if (factory.loadEncoder(double[].class) != DoubleArraySimpledCoder.instance) {
if (AnyValue.class.isAssignableFrom((Class) type)) { // 不支持
return null;
}
return generateDyncEncoder(factory, (Class) type);

View File

@@ -41,6 +41,24 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
instance.register(AnyValue.class, instance.loadDecoder(AnyValueWriter.class));
instance.register(AnyValue.class, instance.loadEncoder(AnyValueWriter.class));
instance.register(boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder.instance);
instance.register(byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder.instance);
instance.register(char[].class, ProtobufCoders.ProtobufCharArraySimpledCoder.instance);
instance.register(short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder.instance);
instance.register(int[].class, ProtobufCoders.ProtobufIntArraySimpledCoder.instance);
instance.register(float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder.instance);
instance.register(long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder.instance);
instance.register(double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder.instance);
instance.register(Boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder2.instance);
instance.register(Byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder2.instance);
instance.register(Character[].class, ProtobufCoders.ProtobufCharArraySimpledCoder2.instance);
instance.register(Short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder2.instance);
instance.register(Integer[].class, ProtobufCoders.ProtobufIntArraySimpledCoder2.instance);
instance.register(Float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder2.instance);
instance.register(Long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder2.instance);
instance.register(Double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder2.instance);
instance.register(AtomicInteger[].class, ProtobufCoders.ProtobufAtomicIntegerArraySimpledCoder.instance);
instance.register(AtomicLong[].class, ProtobufCoders.ProtobufAtomicLongArraySimpledCoder.instance);
}
@SuppressWarnings("OverridableMethodCallInConstructor")
@@ -292,6 +310,58 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|| componentType == String.class;
}
// see com.google.protobuf.CodedOutputStream
protected static int computeInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
return computeUInt64SizeNoTag(value);
}
protected static int computeUInt64SizeNoTag(final long value) {
if (value == 0) {
return 1;
}
int clz = Long.numberOfLeadingZeros(value);
return ((Long.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
}
protected static int computeSInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
return computeUInt32SizeNoTag(encodeZigZag32(value));
}
protected static int computeSInt64SizeNoTag(final long value) {
if (value == 0) {
return 1;
}
return computeUInt64SizeNoTag(encodeZigZag64(value));
}
protected static int computeUInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
int clz = Integer.numberOfLeadingZeros(value);
return ((Integer.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
}
protected static int encodeZigZag32(final int n) {
if (n == 0) {
return 0;
}
return (n << 1) ^ (n >> 31);
}
protected static long encodeZigZag64(final long n) {
if (n == 0) {
return 0L;
}
return (n << 1) ^ (n >> 63);
}
public static Class getSimpleCollectionComponentType(Type type) {
return supportSimpleCollectionType(type)
? (Class) ((ParameterizedType) type).getActualTypeArguments()[0]

View File

@@ -0,0 +1,13 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.convert.pb;
/**
*
* @author zhangjx
*/
public interface ProtobufPrimitivable {
//
}

View File

@@ -8,6 +8,8 @@ package org.redkale.convert.pb;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.redkale.convert.*;
/** @author zhangjx */
@@ -210,7 +212,16 @@ public class ProtobufReader extends Reader {
// ------------------------------------------------------------
@Override
public final boolean readBoolean() {
return readRawVarint64() != 0;
return content[++this.position] != 0;
}
public boolean[] readBools() {
int size = readRawVarint32();
boolean[] data = new boolean[size];
for (int i = 0; i < size; i++) {
data[i] = readBoolean();
}
return data;
}
@Override
@@ -218,26 +229,80 @@ public class ProtobufReader extends Reader {
return (byte) readInt();
}
public byte[] readBytes() {
return readByteArray();
}
@Override
public final char readChar() {
return (char) readInt();
}
public char[] readChars() {
int len = readRawVarint32();
List<Integer> list = new ArrayList<>(len);
while (len > 0) {
int val = readChar();
list.add(val);
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
}
char[] rs = new char[list.size()];
for (int i = 0; i < rs.length; i++) {
rs[i] = (char) list.get(i).intValue();
}
return rs;
}
@Override
public final short readShort() {
return (short) readInt();
}
public short[] readShorts() {
int len = readRawVarint32();
List<Short> list = new ArrayList<>(len);
while (len > 0) {
short val = readShort();
list.add(val);
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
}
short[] rs = new short[list.size()];
for (int i = 0; i < rs.length; i++) {
rs[i] = list.get(i);
}
return rs;
}
@Override
public final int readInt() { // readSInt32
int n = readRawVarint32();
return (n >>> 1) ^ -(n & 1);
}
@Override
public final long readLong() { // readSInt64
long n = readRawVarint64();
return (n >>> 1) ^ -(n & 1);
public int[] readInts() {
int len = readRawVarint32();
List<Integer> list = new ArrayList<>(len);
while (len > 0) {
int val = readInt();
list.add(val);
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
}
int[] rs = new int[list.size()];
for (int i = 0; i < rs.length; i++) {
rs[i] = list.get(i);
}
return rs;
}
public AtomicInteger[] readAtomicIntegers() {
int len = readRawVarint32();
List<AtomicInteger> list = new ArrayList<>(len);
while (len > 0) {
int val = readInt();
list.add(new AtomicInteger(val));
len -= ProtobufFactory.computeSInt32SizeNoTag(val);
}
return list.toArray(new AtomicInteger[list.size()]);
}
@Override
@@ -245,11 +310,61 @@ public class ProtobufReader extends Reader {
return Float.intBitsToFloat(readRawLittleEndian32());
}
public float[] readFloats() {
int len = readRawVarint32();
float[] rs = new float[len / 4];
for (int i = 0; i < rs.length; i++) {
rs[i] = readFloat();
}
return rs;
}
@Override
public final long readLong() { // readSInt64
long n = readRawVarint64();
return (n >>> 1) ^ -(n & 1);
}
public long[] readLongs() {
int len = readRawVarint32();
List<Long> list = new ArrayList<>(len);
while (len > 0) {
long val = readLong();
list.add(val);
len -= ProtobufFactory.computeSInt64SizeNoTag(val);
}
long[] rs = new long[list.size()];
for (int i = 0; i < rs.length; i++) {
rs[i] = list.get(i);
}
return rs;
}
public AtomicLong[] readAtomicLongs() {
int len = readRawVarint32();
List<AtomicLong> list = new ArrayList<>(len);
while (len > 0) {
long val = readInt();
list.add(new AtomicLong(val));
len -= ProtobufFactory.computeSInt64SizeNoTag(val);
}
return list.toArray(new AtomicLong[list.size()]);
}
@Override
public final double readDouble() {
return Double.longBitsToDouble(readRawLittleEndian64());
}
public double[] readDoubles() {
int len = readRawVarint32();
double[] rs = new double[len / 8];
for (int i = 0; i < rs.length; i++) {
rs[i] = readDouble();
}
return rs;
}
@Override
public final String readClassName() {
return "";

View File

@@ -642,7 +642,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (char item : array) {
len += computeSInt32SizeNoTag(item);
len += ProtobufFactory.computeSInt32SizeNoTag(item);
}
writeLength(len);
for (char item : array) {
@@ -656,7 +656,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (Character item : array) {
len += computeSInt32SizeNoTag(item);
len += ProtobufFactory.computeSInt32SizeNoTag(item);
}
writeLength(len);
for (Character item : array) {
@@ -670,7 +670,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (Character item : array) {
len += computeSInt32SizeNoTag(item);
len += ProtobufFactory.computeSInt32SizeNoTag(item);
}
writeLength(len);
for (Character item : array) {
@@ -684,7 +684,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (short item : array) {
len += computeSInt32SizeNoTag(item);
len += ProtobufFactory.computeSInt32SizeNoTag(item);
}
writeLength(len);
for (short item : array) {
@@ -698,7 +698,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (Short item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Short item : array) {
@@ -712,7 +712,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (Short item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Short item : array) {
@@ -726,7 +726,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (int item : array) {
len += computeSInt32SizeNoTag(item);
len += ProtobufFactory.computeSInt32SizeNoTag(item);
}
writeLength(len);
for (int item : array) {
@@ -740,7 +740,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (Integer item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Integer item : array) {
@@ -754,7 +754,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (Integer item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Integer item : array) {
@@ -801,7 +801,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (long item : array) {
len += computeSInt64SizeNoTag(item);
len += ProtobufFactory.computeSInt64SizeNoTag(item);
}
writeLength(len);
for (long item : array) {
@@ -815,7 +815,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (Long item : array) {
len += computeSInt64SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Long item : array) {
@@ -829,7 +829,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (Long item : array) {
len += computeSInt64SizeNoTag(item == null ? 0 : item);
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item);
}
writeLength(len);
for (Long item : array) {
@@ -876,7 +876,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (AtomicInteger item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item.get());
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item.get());
}
writeLength(len);
for (AtomicInteger item : array) {
@@ -890,7 +890,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (AtomicInteger item : array) {
len += computeSInt32SizeNoTag(item == null ? 0 : item.get());
len += ProtobufFactory.computeSInt32SizeNoTag(item == null ? 0 : item.get());
}
writeLength(len);
for (AtomicInteger item : array) {
@@ -904,7 +904,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && array.length > 0) {
int len = 0;
for (AtomicLong item : array) {
len += computeSInt64SizeNoTag(item == null ? 0 : item.get());
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item.get());
}
writeLength(len);
for (AtomicLong item : array) {
@@ -918,7 +918,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
if (array != null && !array.isEmpty()) {
int len = 0;
for (AtomicLong item : array) {
len += computeSInt64SizeNoTag(item == null ? 0 : item.get());
len += ProtobufFactory.computeSInt64SizeNoTag(item == null ? 0 : item.get());
}
writeLength(len);
for (AtomicLong item : array) {
@@ -1643,56 +1643,4 @@ public class ProtobufWriter extends Writer implements ByteTuple {
(byte) ((int) (value >> 56) & 0xFF)
};
}
// see com.google.protobuf.CodedOutputStream
protected static int computeInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
return computeUInt64SizeNoTag(value);
}
protected static int computeUInt64SizeNoTag(final long value) {
if (value == 0) {
return 1;
}
int clz = Long.numberOfLeadingZeros(value);
return ((Long.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
}
protected static int computeSInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
return computeUInt32SizeNoTag(encodeZigZag32(value));
}
protected static int computeSInt64SizeNoTag(final long value) {
if (value == 0) {
return 1;
}
return computeUInt64SizeNoTag(encodeZigZag64(value));
}
protected static int computeUInt32SizeNoTag(final int value) {
if (value == 0) {
return 1;
}
int clz = Integer.numberOfLeadingZeros(value);
return ((Integer.SIZE * 9 + (1 << 6)) - (clz * 9)) >>> 6;
}
protected static int encodeZigZag32(final int n) {
if (n == 0) {
return 0;
}
return (n << 1) ^ (n >> 31);
}
protected static long encodeZigZag64(final long n) {
if (n == 0) {
return 0L;
}
return (n << 1) ^ (n >> 63);
}
}