java.lang.Object
edu.jiangxin.apktoolbox.convert.protobuf.supervised.DescriptorCache

public final class DescriptorCache extends Object
Cache containing protobuf descriptors used by ProtoToJson in order to decode binary protobuf messages.

An instance can be created using the static factory methods emptyCache(), fromDirectory(Path) and fromFile(Path).

Additional descriptors can be added using addDescriptor(Descriptors.Descriptor), addDescriptors(Path) and addDescriptors(byte[]). They can be received using getByTypeName(String) and getDescriptors().

Descriptors can be obtained by applying a protoc command on the protobuf schema .proto, for example:


 protoc --descriptor_set_out foo.desc foo.proto
 
Author:
Daniel Tischner <zabuza.dev@gmail.com>
  • Method Details

    • emptyCache

      public static DescriptorCache emptyCache()
      Creates an instance that initially has no descriptors.
      Returns:
      The created instance
    • fromDirectory

      public static DescriptorCache fromDirectory(Path directory)
      Creates an instance from a directory containing descriptor files.

      The directory must not contain files that are not valid descriptor files.

      Descriptor files can be obtained by applying a protoc command on the protobuf schema .proto, for example:

      
       protoc --descriptor_set_out foo.desc foo.proto
       
      Parameters:
      directory - The directory containing the descriptor files, not null.
      Returns:
      The created instance that has all descriptors available in the given directory
      Throws:
      IllegalArgumentException - If the directory is not a directory
      UncheckedIOException - If an I/O error occurs during reading the files
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If a file is not a valid descriptor file
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException - If a file contains malformed descriptors
    • fromFile

      public static DescriptorCache fromFile(Path descriptorsFile)
      Creates an instance from a single descriptor file.

      Descriptor files can be obtained by applying a protoc command on the protobuf schema .proto, for example:

      
       protoc --descriptor_set_out foo.desc foo.proto
       
      Parameters:
      descriptorsFile - The descriptor file, not null.
      Returns:
      The created instance that has all descriptors available in the given file
      Throws:
      IllegalArgumentException - If the descriptorsFile is not a regular file
      UncheckedIOException - If an I/O error occurs during reading the files
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the file is not a valid descriptor file
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException - If the file contains malformed descriptors
    • addDescriptor

      public Optional<com.google.protobuf.Descriptors.Descriptor> addDescriptor(com.google.protobuf.Descriptors.Descriptor descriptor)
      Adds the given descriptor to the cache.

      Overwriting any descriptor previously registered for the same message type.

      Parameters:
      descriptor - The descriptor to add, not null
      Returns:
      The descriptor previously associated to the message type, if any
    • addDescriptors

      public void addDescriptors(Path descriptorsFile)
      Adds all descriptors given in a descriptor file to the cache.

      Overwriting any descriptors previously registered for the same message types.

      Descriptor files can be obtained by applying a protoc command on the protobuf schema .proto, for example:

      
       protoc --descriptor_set_out foo.desc foo.proto
       
      Parameters:
      descriptorsFile - The descriptor file, not null.
      Throws:
      IllegalArgumentException - If the descriptorsFile is not a regular file
      UncheckedIOException - If an I/O error occurs during reading the file
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the file is not a valid descriptor file
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException - If the file contains malformed descriptors
    • addDescriptors

      public void addDescriptors(byte[] descriptorsRaw)
      Adds all descriptors given as a raw descriptor set to the cache.

      Overwriting any descriptors previously registered for the same message types.

      Parameters:
      descriptorsRaw - The raw descriptor set, not null.
      Throws:
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException - If the file is not a valid descriptor file
      edu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException - If the file contains malformed descriptors
    • getByTypeName

      public Optional<com.google.protobuf.Descriptors.Descriptor> getByTypeName(String typeName)
      Gets the descriptor registered for the given message type name, if any.
      Parameters:
      typeName - The message type name, not null
      Returns:
      The descriptor registered for the given message type name, if any
    • getDescriptors

      public Collection<com.google.protobuf.Descriptors.Descriptor> getDescriptors()
      Gets all descriptors registered by this cache.
      Returns:
      An unmodifiable collection of all registered descriptors
    • getEntries

      public Collection<Map.Entry<String,com.google.protobuf.Descriptors.Descriptor>> getEntries()
      Gets all by this cache registered mappings of message type names to their descriptors.
      Returns:
      An unmodifiable collection of all registered message type name to descriptor mappings
    • isEmpty

      public boolean isEmpty()
      Whether the cache has no descriptors registered.
      Returns:
      True if the cache has no descriptors registered, false otherwise
    • size

      public int size()
      Gets how many descriptors are currently registered to the cache.
      Returns:
      The amount of descriptors registered to the cache