java类com.google.protobuf.WireFormat的实例源码

BlockListAsLongs.java 文件源码 项目:aliyun-oss-hadoop-fs 阅读 64 收藏 0 点赞 0 评论 0
public static BlockListAsLongs readFrom(InputStream is) throws IOException {
  CodedInputStream cis = CodedInputStream.newInstance(is);
  int numBlocks = -1;
  ByteString blocksBuf = null;
  while (!cis.isAtEnd()) {
    int tag = cis.readTag();
    int field = WireFormat.getTagFieldNumber(tag);
    switch(field) {
      case 0:
        break;
      case 1:
        numBlocks = (int)cis.readInt32();
        break;
      case 2:
        blocksBuf = cis.readBytes();
        break;
      default:
        cis.skipField(tag);
        break;
    }
  }
  if (numBlocks != -1 && blocksBuf != null) {
    return decodeBuffer(numBlocks, blocksBuf);
  }
  return null;
}
ByteBufCodedInputStream.java 文件源码 项目:incubator-pulsar 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Reads and discards a single field, given its tag value.
 *
 * @return {@code false} if the tag is an endgroup tag, in which case nothing is skipped. Otherwise, returns
 *         {@code true}.
 */
public boolean skipField(final int tag) throws IOException {
    switch (getTagWireType(tag)) {
    case WireFormat.WIRETYPE_VARINT:
        readInt32();
        return true;
    case WireFormat.WIRETYPE_FIXED64:
        readRawLittleEndian64();
        return true;
    case WireFormat.WIRETYPE_LENGTH_DELIMITED:
        skipRawBytes(readRawVarint32());
        return true;
    case WireFormat.WIRETYPE_START_GROUP:
        skipMessage();
        checkLastTagWas(makeTag(WireFormat.getTagFieldNumber(tag), WireFormat.WIRETYPE_END_GROUP));
        return true;
    case WireFormat.WIRETYPE_END_GROUP:
        return false;
    case WireFormat.WIRETYPE_FIXED32:
        readRawLittleEndian32();
        return true;
    default:
        throw new InvalidProtocolBufferException("Protocol message tag had invalid wire type.");
    }
}
ByteBufCodedInputStreamTest.java 文件源码 项目:incubator-pulsar 阅读 25 收藏 0 点赞 0 评论 0
@Test
public void testWritingDouble() throws IOException {
    ByteBuf buf = Unpooled.buffer();
    buf.clear();
    ByteBufCodedOutputStream outputStream = ByteBufCodedOutputStream.get(buf);
    outputStream.writeDouble(12, 23d);
    outputStream.writeDouble(15, 13.13d);
    outputStream.writeDouble(1, -0.003d);

    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(buf);
    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 12);
    assertEquals(inputStream.readDouble(), 23d);

    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 15);
    assertEquals(inputStream.readDouble(), 13.13d);

    assertEquals(WireFormat.getTagFieldNumber(inputStream.readTag()), 1);
    assertEquals(inputStream.readDouble(), -0.003d);
}
CodedConstant.java 文件源码 项目:jprotobuf 阅读 26 收藏 0 点赞 0 评论 0
/**
 * write list to {@link CodedOutputStream} object.
 *
 * @param out target output stream to write
 * @param order field order
 * @param type field type
 * @param list target list object to be serialized
 * @param packed the packed
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeToList(CodedOutputStream out, int order, FieldType type, List list, boolean packed)
        throws IOException {
    if (list == null || list.isEmpty()) {
        return;
    }

    if (packed) {
        out.writeUInt32NoTag(makeTag(order, WireFormat.WIRETYPE_LENGTH_DELIMITED));
        out.writeUInt32NoTag(computeListSize(order, list, type, false, null, packed, true));
    }

    for (Object object : list) {
        writeObject(out, order, type, object, true, !packed);

    }

}
MapEntryLite.java 文件源码 项目:jprotobuf 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Parses the field.
 *
 * @param <T> the generic type
 * @param input the input
 * @param extensionRegistry the extension registry
 * @param type the type
 * @param value the value
 * @return the t
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unchecked")
static <T> T parseField(CodedInputStream input, ExtensionRegistryLite extensionRegistry, WireFormat.FieldType type,
        T value) throws IOException {
    switch (type) {
        case MESSAGE:
            int length = input.readRawVarint32();
            final int oldLimit = input.pushLimit(length);
            Codec<? extends Object> codec = ProtobufProxy.create(value.getClass());
            T ret = (T) codec.decode(input.readRawBytes(length));
            input.popLimit(oldLimit);
            return ret;
        case ENUM:
            return (T) (java.lang.Integer) input.readEnum();
        case GROUP:
            throw new RuntimeException("Groups are not allowed in maps.");
        default:
            return (T) CodedConstant.readPrimitiveField(input, type, true);
    }
}
WalletProtobufSerializer.java 文件源码 项目:creacoinj 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:okwallet 阅读 22 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:cryptwallet 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
ByteBufCodedInputStream.java 文件源码 项目:incubator-pulsar 阅读 24 收藏 0 点赞 0 评论 0
public int readTag() throws IOException {
    if (isAtEnd()) {
        lastTag = 0;
        return 0;
    }

    lastTag = readRawVarint32();
    if (WireFormat.getTagFieldNumber(lastTag) == 0) {
        // If we actually read zero (or any tag number corresponding to field
        // number zero), that's not a valid tag.
        throw new InvalidProtocolBufferException("CodedInputStream encountered a malformed varint.");
    }
    return lastTag;
}
WalletProtobufSerializer.java 文件源码 项目:namecoinj 阅读 20 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:CoinJoin 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
TrackManager.java 文件源码 项目:trekarta 阅读 42 收藏 0 点赞 0 评论 0
@Override
public void saveData(OutputStream outputStream, FileDataSource source, @Nullable ProgressListener progressListener) throws Exception {
    if (source.tracks.size() != 1)
        throw new Exception("Only single track can be saved in mtrack format");
    Track track = source.tracks.get(0);
    if (progressListener != null)
        progressListener.onProgressStarted(track.points.size());
    CodedOutputStream output = CodedOutputStream.newInstance(outputStream);
    output.writeUInt32(FIELD_VERSION, VERSION);
    int progress = 0;
    for (Track.TrackPoint point : track.points) {
        output.writeTag(FIELD_POINT, WireFormat.WIRETYPE_LENGTH_DELIMITED);
        output.writeRawVarint32(getSerializedPointSize(point));
        output.writeInt32(FIELD_POINT_LATITUDE, point.latitudeE6);
        output.writeInt32(FIELD_POINT_LONGITUDE, point.longitudeE6);
        output.writeFloat(FIELD_POINT_ALTITUDE, point.elevation);
        output.writeFloat(FIELD_POINT_SPEED, point.speed);
        output.writeFloat(FIELD_POINT_BEARING, point.bearing);
        output.writeFloat(FIELD_POINT_ACCURACY, point.accuracy);
        output.writeUInt64(FIELD_POINT_TIMESTAMP, point.time);
        if (!point.continuous)
            //noinspection ConstantConditions
            output.writeBool(8, point.continuous);
        progress++;
        if (progressListener != null)
            progressListener.onProgressChanged(progress);
    }
    output.writeBytes(FIELD_NAME, ByteString.copyFromUtf8(track.name));
    output.writeUInt32(FIELD_COLOR, track.style.color);
    output.writeFloat(FIELD_WIDTH, track.style.width);
    output.flush();
    outputStream.close();
    if (progressListener != null)
        progressListener.onProgressFinished();
}
WalletProtobufSerializer.java 文件源码 项目:digibytej-alice 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:sparkbit-bitcoinj 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
CodedConstant.java 文件源码 项目:jprotobuf 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Compute the number of bytes that would be needed to encode a single tag/value pair of arbitrary type.
 *
 * @param type The field's type.
 * @param number The field's number.
 * @param value Object representing the field's value. Must be of the exact type which would be returned by
 *            {@link Message#getField(Descriptors.FieldDescriptor)} for this field.
 * @return the int
 */
public static int computeElementSize(final WireFormat.FieldType type, final int number, final Object value) {
    int tagSize = CodedOutputStream.computeTagSize(number);
    if (type == WireFormat.FieldType.GROUP) {
        // Only count the end group tag for proto2 messages as for proto1 the end
        // group tag will be counted as a part of getSerializedSize().
        tagSize *= 2;
    }
    return tagSize + computeElementSizeNoTag(type, value);
}
CodedConstant.java 文件源码 项目:jprotobuf 阅读 33 收藏 0 点赞 0 评论 0
/**
 * Compute map size.
 *
 * @param <K> the key type
 * @param <V> the value type
 * @param order the order
 * @param map the map
 * @param keyType the key type
 * @param defaultKey the default key
 * @param valueType the value type
 * @param defalutValue the defalut value
 * @return the int
 */
public static <K, V> int computeMapSize(int order, Map<K, V> map, com.google.protobuf.WireFormat.FieldType keyType,
        K defaultKey, com.google.protobuf.WireFormat.FieldType valueType, V defalutValue) {
    int size = 0;
    for (java.util.Map.Entry<K, V> entry : map.entrySet()) {
        com.baidu.bjf.remoting.protobuf.MapEntry<K, V> valuesDefaultEntry = com.baidu.bjf.remoting.protobuf.MapEntry
                .<K, V> newDefaultInstance(null, keyType, defaultKey, valueType, defalutValue);

        com.baidu.bjf.remoting.protobuf.MapEntry<K, V> values =
                valuesDefaultEntry.newBuilderForType().setKey(entry.getKey()).setValue(entry.getValue()).build();

        size += com.google.protobuf.CodedOutputStream.computeMessageSize(order, values);
    }
    return size;
}
CodedConstant.java 文件源码 项目:jprotobuf 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Write a single tag-value pair to the stream.
 *
 * @param output The output stream.
 * @param type The field's type.
 * @param number The field's number.
 * @param value Object representing the field's value. Must be 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 void writeElement(final CodedOutputStream output, final WireFormat.FieldType type, final int number,
        final Object value) throws IOException {
    // Special case for groups, which need a start and end tag; other fields
    // can just use writeTag() and writeFieldNoTag().
    if (type == WireFormat.FieldType.GROUP) {
        output.writeGroup(number, (MessageLite) value);
    } else {
        output.writeTag(number, getWireFormatForFieldType(type, false));
        writeElementNoTag(output, type, value);
    }
}
MapEntry.java 文件源码 项目:jprotobuf 阅读 26 收藏 0 点赞 0 评论 0
/**
 * Instantiates a new metadata.
 *
 * @param descriptor the descriptor
 * @param defaultInstance the default instance
 * @param keyType the key type
 * @param valueType the value type
 */
public Metadata(Descriptor descriptor, MapEntry<K, V> defaultInstance, WireFormat.FieldType keyType,
        WireFormat.FieldType valueType) {
    super(keyType, defaultInstance.key, valueType, defaultInstance.value);
    this.descriptor = descriptor;
    this.parser = new AbstractParser<MapEntry<K, V>>() {

        @Override
        public MapEntry<K, V> parsePartialFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
                throws InvalidProtocolBufferException {
            return new MapEntry<K, V>(Metadata.this, input, extensionRegistry);
        }
    };
}
WalletProtobufSerializer.java 文件源码 项目:wowdoge.org 阅读 23 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:quarkcoinj 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:animecoinj 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:peercoinj 阅读 25 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:dashj 阅读 24 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:NuBitsj 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 * 
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
WalletProtobufSerializer.java 文件源码 项目:bitcoinj 阅读 33 收藏 0 点赞 0 评论 0
/**
 * Cheap test to see if input stream is a wallet. This checks for a magic value at the beginning of the stream.
 *
 * @param is
 *            input stream to test
 * @return true if input stream is a wallet
 */
public static boolean isWallet(InputStream is) {
    try {
        final CodedInputStream cis = CodedInputStream.newInstance(is);
        final int tag = cis.readTag();
        final int field = WireFormat.getTagFieldNumber(tag);
        if (field != 1) // network_identifier
            return false;
        final String network = cis.readString();
        return NetworkParameters.fromID(network) != null;
    } catch (IOException x) {
        return false;
    }
}
ByteBufCodedOutputStream.java 文件源码 项目:incubator-pulsar 阅读 34 收藏 0 点赞 0 评论 0
/** Write an {@code int32} field, including tag, to the stream. */
public void writeInt32(final int fieldNumber, final int value) throws IOException {
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
    writeInt32NoTag(value);
}
ByteBufCodedOutputStream.java 文件源码 项目:incubator-pulsar 阅读 26 收藏 0 点赞 0 评论 0
public void writeInt64(final int fieldNumber, final long value) throws IOException {
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
    writeInt64NoTag(value);
}
ByteBufCodedOutputStream.java 文件源码 项目:incubator-pulsar 阅读 30 收藏 0 点赞 0 评论 0
public void writeUInt64(final int fieldNumber, final long value) throws IOException {
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
    writeUInt64NoTag(value);
}
ByteBufCodedOutputStream.java 文件源码 项目:incubator-pulsar 阅读 28 收藏 0 点赞 0 评论 0
/** Write a {@code bool} field, including tag, to the stream. */
public void writeBool(final int fieldNumber, final boolean value) throws IOException {
    writeTag(fieldNumber, WireFormat.WIRETYPE_VARINT);
    writeBoolNoTag(value);
}
ByteBufCodedOutputStream.java 文件源码 项目:incubator-pulsar 阅读 29 收藏 0 点赞 0 评论 0
/** Write a {@code bytes} field, including tag, to the stream. */
public void writeBytes(final int fieldNumber, final ByteString value) throws IOException {
    writeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED);
    writeBytesNoTag(value);
}


问题


面经


文章

微信
公众号

扫码关注公众号