Java Code Examples for com.google.protobuf.CodedInputStream#skipField()

The following examples show how to use com.google.protobuf.CodedInputStream#skipField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the entry.
 *
 * @param <K> the key type
 * @param <V> the value type
 * @param input the input
 * @param metadata the metadata
 * @param extensionRegistry the extension registry
 * @return the map. entry
 * @throws IOException Signals that an I/O exception has occurred.
 */
static <K, V> Map.Entry<K, V> parseEntry(CodedInputStream input, Metadata<K, V> metadata,
        ExtensionRegistryLite extensionRegistry) throws IOException {
    K key = metadata.defaultKey;
    V value = metadata.defaultValue;
    while (true) {
        int tag = input.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == CodedConstant.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
            key = parseField(input, extensionRegistry, metadata.keyType, key);
        } else if (tag == CodedConstant.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
            value = parseField(input, extensionRegistry, metadata.valueType, value);
        } else {
            if (!input.skipField(tag)) {
                break;
            }
        }
    }
    return new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
}
 
Example 2
Source File: MapEntryLite.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an entry off of the input into the map. This helper avoids allocaton of a {@link MapEntryLite} by parsing
 * directly into the provided {@link MapFieldLite}.
 *
 * @param map the map
 * @param input the input
 * @param extensionRegistry the extension registry
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void parseInto(MapFieldLite<K, V> map, CodedInputStream input, ExtensionRegistryLite extensionRegistry)
        throws IOException {
    int length = input.readRawVarint32();
    final int oldLimit = input.pushLimit(length);
    K key = metadata.defaultKey;
    V value = metadata.defaultValue;

    while (true) {
        int tag = input.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == CodedConstant.makeTag(KEY_FIELD_NUMBER, metadata.keyType.getWireType())) {
            key = parseField(input, extensionRegistry, metadata.keyType, key);
        } else if (tag == CodedConstant.makeTag(VALUE_FIELD_NUMBER, metadata.valueType.getWireType())) {
            value = parseField(input, extensionRegistry, metadata.valueType, value);
        } else {
            if (!input.skipField(tag)) {
                break;
            }
        }
    }

    input.checkLastTagWas(0);
    input.popLimit(oldLimit);
    map.put(key, value);
}
 
Example 3
Source File: TransactionUtil.java    From snowblossom with Apache License 2.0 4 votes vote down vote up
public static ArrayList<ByteString> extractWireFormatTxOut(Transaction tx)
  throws ValidationException
{
  try
  {
    CodedInputStream code_in = CodedInputStream.newInstance(tx.getInnerData().toByteArray());

    ArrayList<ByteString> lst = new ArrayList<>();

    while(true)
    {
      int tag = code_in.readTag();
      if (tag == 0) break;
      
      // The least signficiate 3 bits are the proto field type
      // so shift to get out field number, which is 5 for TranasctionOutput
      if (tag >> 3 == 5)
      {
        ByteArrayOutputStream b_out = new ByteArrayOutputStream();
        CodedOutputStream c_out = CodedOutputStream.newInstance(b_out);
        code_in.skipField(tag, c_out);
        c_out.flush();

        ByteString bs = ByteString.copyFrom(b_out.toByteArray());

        // So funny story...when you get an inner message like this as opposed to just serializing
        // the object, protobuf puts a tag and size on the coded input stream.  So we need to figure
        // out how many bytes that is an trim it off.
        CodedInputStream read_again = CodedInputStream.newInstance(bs.toByteArray());
        // Expected tag
        int tag2 = read_again.readTag();
        // Size of element
        int size = read_again.readInt32();

        // All we really care is how many bytes those two take.  For shorter messages
        // it will be 2, but could be higher if protobuf needs more bytes to encode the size
        int offset = read_again.getTotalBytesRead();

        bs = bs.substring(offset);
        lst.add(bs);
      }
      else
      {
        code_in.skipField(tag);
      }

    }
    return lst;
  }
  catch(java.io.IOException e)
  {
    throw new ValidationException(e);
  }
}
 
Example 4
Source File: TrackManager.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public FileDataSource loadData(InputStream inputStream, String filePath) throws Exception {
    long propertiesOffset = 0L;
    Track track = new Track();
    CodedInputStream input = CodedInputStream.newInstance(inputStream);
    boolean done = false;
    while (!done) {
        long offset = input.getTotalBytesRead();
        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_VERSION: {
                // skip version
                input.skipField(tag);
                break;
            }
            case FIELD_POINT: {
                int length = input.readRawVarint32();
                int oldLimit = input.pushLimit(length);
                readPoint(track, input);
                input.popLimit(oldLimit);
                input.checkLastTagWas(0);
                break;
            }
            case FIELD_NAME: {
                propertiesOffset = offset;
                track.name = input.readBytes().toStringUtf8();
                break;
            }
            case FIELD_COLOR: {
                track.style.color = input.readUInt32();
                break;
            }
            case FIELD_WIDTH: {
                track.style.width = input.readFloat();
                break;
            }
        }
    }
    inputStream.close();
    track.id = 31 * filePath.hashCode() + 1;
    FileDataSource dataSource = new FileDataSource();
    dataSource.name = track.name;
    dataSource.tracks.add(track);
    track.source = dataSource;
    dataSource.propertiesOffset = propertiesOffset;
    return dataSource;
}