下面列出了com.google.protobuf.CodedInputStream# readUInt64 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
StringBuilder vals = new StringBuilder();
while (inputStream.getBytesUntilLimit() > 0) {
if (vals.length() > 0) {
vals.append(",");
}
long valLen = inputStream.readUInt64();
// TODO: charset
vals.append(new String(inputStream.readRawBytes((int) valLen)));
}
// TODO: charset mess here
byte[] buf = vals.toString().getBytes();
return vf.createFromBytes(buf, 0, buf.length);
} catch (IOException e) {
throw new DataReadException(e);
}
}
@Override
public <T> T decodeSet(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
StringBuilder vals = new StringBuilder();
while (inputStream.getBytesUntilLimit() > 0) {
if (vals.length() > 0) {
vals.append(",");
}
long valLen = inputStream.readUInt64();
// TODO: charset
vals.append(new String(inputStream.readRawBytes((int) valLen)));
}
// TODO: charset mess here
byte[] buf = vals.toString().getBytes();
return vf.createFromBytes(buf, 0, buf.length);
} catch (IOException e) {
throw new DataReadException(e);
}
}
@Override
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
int year = (int) inputStream.readUInt64();
int month = (int) inputStream.readUInt64();
int day = (int) inputStream.readUInt64();
// do we have a time too?
if (inputStream.getBytesUntilLimit() > 0) {
int hours = 0;
int minutes = 0;
int seconds = 0;
int nanos = 0;
if (!inputStream.isAtEnd()) {
hours = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
minutes = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
seconds = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
nanos = 1000 * (int) inputStream.readInt64();
}
}
}
}
return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
return vf.createFromDate(year, month, day);
} catch (IOException e) {
throw new DataReadException(e);
}
}
@Override
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(bytes, offset, length);
int year = (int) inputStream.readUInt64();
int month = (int) inputStream.readUInt64();
int day = (int) inputStream.readUInt64();
// do we have a time too?
if (inputStream.getBytesUntilLimit() > 0) {
int hours = 0;
int minutes = 0;
int seconds = 0;
int nanos = 0;
if (!inputStream.isAtEnd()) {
hours = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
minutes = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
seconds = (int) inputStream.readInt64();
if (!inputStream.isAtEnd()) {
nanos = 1000 * (int) inputStream.readInt64();
}
}
}
}
return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
return vf.createFromDate(year, month, day);
} catch (IOException e) {
throw new DataReadException(e);
}
}
private void readPoint(Track track, CodedInputStream input) throws IOException {
int latitudeE6 = 0;
int longitudeE6 = 0;
boolean continuous = true;
float altitude = Float.NaN;
float speed = Float.NaN;
float bearing = Float.NaN;
float accuracy = Float.NaN;
long timestamp = 0L;
boolean done = false;
while (!done) {
int tag = input.readTag();
int field = WireFormat.getTagFieldNumber(tag);
switch (field) {
case 0:
done = true;
break;
default: {
throw new com.google.protobuf.InvalidProtocolBufferException("Unsupported proto field: " + tag);
}
case FIELD_POINT_LATITUDE: {
latitudeE6 = input.readInt32();
break;
}
case FIELD_POINT_LONGITUDE: {
longitudeE6 = input.readInt32();
break;
}
case FIELD_POINT_ALTITUDE: {
altitude = input.readFloat();
break;
}
case FIELD_POINT_SPEED: {
speed = input.readFloat();
break;
}
case FIELD_POINT_BEARING: {
bearing = input.readFloat();
break;
}
case FIELD_POINT_ACCURACY: {
accuracy = input.readFloat();
break;
}
case FIELD_POINT_TIMESTAMP: {
timestamp = input.readUInt64();
break;
}
case FIELD_POINT_CONTINUOUS: {
continuous = input.readBool();
break;
}
}
}
track.addPointFast(continuous, latitudeE6, longitudeE6, altitude, speed, bearing, accuracy, timestamp);
}
/**
* Read a field of any primitive type for immutable messages from a CodedInputStream. Enums, groups, and embedded
* messages are not handled by this method.
*
* @param input The stream from which to read.
* @param type Declared type of the field.
* @param checkUtf8 When true, check that the input is valid utf8.
* @return An object representing the field's value, of the exact type which would be returned by
* {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
* @throws IOException Signals that an I/O exception has occurred.
*/
public static Object readPrimitiveField(CodedInputStream input, final WireFormat.FieldType type, boolean checkUtf8)
throws IOException {
switch (type) {
case DOUBLE:
return input.readDouble();
case FLOAT:
return input.readFloat();
case INT64:
return input.readInt64();
case UINT64:
return input.readUInt64();
case INT32:
return input.readInt32();
case FIXED64:
return input.readFixed64();
case FIXED32:
return input.readFixed32();
case BOOL:
return input.readBool();
case STRING:
if (checkUtf8) {
return input.readStringRequireUtf8();
} else {
return input.readString();
}
case BYTES:
return input.readByteArray();
case UINT32:
return input.readUInt32();
case SFIXED32:
return input.readSFixed32();
case SFIXED64:
return input.readSFixed64();
case SINT32:
return input.readSInt32();
case SINT64:
return input.readSInt64();
case GROUP:
throw new IllegalArgumentException("readPrimitiveField() cannot handle nested groups.");
case MESSAGE:
throw new IllegalArgumentException("readPrimitiveField() cannot handle embedded messages.");
case ENUM:
// We don't handle enums because we don't know what to do if the
// value is not recognized.
throw new IllegalArgumentException("readPrimitiveField() cannot handle enums.");
}
throw new RuntimeException("There is no way to get here, but the compiler thinks otherwise.");
}