Class DescriptorCache
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 Summary
Modifier and TypeMethodDescriptionOptional<com.google.protobuf.Descriptors.Descriptor>
addDescriptor
(com.google.protobuf.Descriptors.Descriptor descriptor) Adds the given descriptor to the cache.void
addDescriptors
(byte[] descriptorsRaw) Adds all descriptors given as a raw descriptor set to the cache.void
addDescriptors
(Path descriptorsFile) Adds all descriptors given in a descriptor file to the cache.static DescriptorCache
Creates an instance that initially has no descriptors.static DescriptorCache
fromDirectory
(Path directory) Creates an instance from a directory containing descriptor files.static DescriptorCache
Creates an instance from a single descriptor file.Optional<com.google.protobuf.Descriptors.Descriptor>
getByTypeName
(String typeName) Gets the descriptor registered for the given message type name, if any.Collection<com.google.protobuf.Descriptors.Descriptor>
Gets all descriptors registered by this cache.Collection<Map.Entry<String,
com.google.protobuf.Descriptors.Descriptor>> Gets all by this cache registered mappings of message type names to their descriptors.boolean
isEmpty()
Whether the cache has no descriptors registered.int
size()
Gets how many descriptors are currently registered to the cache.
-
Method Details
-
emptyCache
Creates an instance that initially has no descriptors.- Returns:
- The created instance
-
fromDirectory
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 thedirectory
is not a directoryUncheckedIOException
- If an I/O error occurs during reading the filesedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If a file is not a valid descriptor fileedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException
- If a file contains malformed descriptors
-
fromFile
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 thedescriptorsFile
is not a regular fileUncheckedIOException
- If an I/O error occurs during reading the filesedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the file is not a valid descriptor fileedu.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
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 thedescriptorsFile
is not a regular fileUncheckedIOException
- If an I/O error occurs during reading the fileedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedInvalidProtocolBufferException
- If the file is not a valid descriptor fileedu.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 fileedu.jiangxin.apktoolbox.convert.protobuf.supervised.UncheckedDescriptorValidationException
- If the file contains malformed descriptors
-
getByTypeName
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
Gets all descriptors registered by this cache.- Returns:
- An unmodifiable collection of all registered descriptors
-
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
-