• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jiangxincode / ApkToolBoxGUI / #1148

30 Aug 2025 01:07PM UTC coverage: 3.008% (-0.01%) from 3.018%
#1148

push

jiangxincode
add protolbuf converter with proto

0 of 217 new or added lines in 8 files covered. (0.0%)

1 existing line in 1 file now uncovered.

248 of 8244 relevant lines covered (3.01%)

0.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/jiangxin/apktoolbox/convert/protobuf/supervised/ProtoToJson.java
1
package edu.jiangxin.apktoolbox.convert.protobuf.supervised;
2

3
import com.google.protobuf.Descriptors;
4
import com.google.protobuf.DynamicMessage;
5
import com.google.protobuf.InvalidProtocolBufferException;
6
import com.google.protobuf.util.JsonFormat;
7

8
import java.io.ByteArrayInputStream;
9
import java.io.ByteArrayOutputStream;
10
import java.io.IOException;
11
import java.io.UncheckedIOException;
12
import java.nio.file.Files;
13
import java.nio.file.Path;
14
import java.util.Comparator;
15
import java.util.Objects;
16
import java.util.Optional;
17
import java.util.zip.InflaterInputStream;
18

19
public final class ProtoToJson {
20
    public static ProtoToJson fromCache(final DescriptorCache cache) {
NEW
21
        Objects.requireNonNull(cache);
×
22

NEW
23
        return new ProtoToJson(cache);
×
24
    }
25

26
    public static ProtoToJson fromEmptyCache() {
NEW
27
        return new ProtoToJson(DescriptorCache.emptyCache());
×
28
    }
29

30
    private static byte[] decompressZLib(final byte[] compressed) throws IOException {
NEW
31
        Objects.requireNonNull(compressed);
×
NEW
32
        try (final ByteArrayInputStream inputStream = new ByteArrayInputStream(compressed);
×
NEW
33
             final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
×
NEW
34
             final InflaterInputStream zLibOutputStream = new InflaterInputStream(inputStream)) {
×
NEW
35
            zLibOutputStream.transferTo(outputStream);
×
NEW
36
            return outputStream.toByteArray();
×
37
        }
38
    }
39

40
    private final DescriptorCache cache;
NEW
41
    private final JsonFormat.Printer printer = JsonFormat.printer();
×
42

NEW
43
    private ProtoToJson(final DescriptorCache cache) {
×
NEW
44
        this.cache = Objects.requireNonNull(cache);
×
NEW
45
    }
×
46

47
    public DescriptorCache getCache() {
NEW
48
        return cache;
×
49
    }
50

51
    public String toJson(final Path messageFile) {
NEW
52
        Objects.requireNonNull(messageFile);
×
53

54
        try {
NEW
55
            return toJson(Files.readAllBytes(messageFile), (String) null);
×
NEW
56
        } catch (final IOException e) {
×
NEW
57
            throw new UncheckedIOException(e);
×
58
        }
59
    }
60

61
    public String toJson(final byte[] messageRaw) {
NEW
62
        Objects.requireNonNull(messageRaw);
×
63

NEW
64
        return toJson(messageRaw, (String) null);
×
65
    }
66

67
    public String toJson(final Path messageFile, final String messageTypeName) {
NEW
68
        Objects.requireNonNull(messageFile);
×
69

70
        try {
NEW
71
            return toJson(Files.readAllBytes(messageFile), messageTypeName);
×
NEW
72
        } catch (final IOException e) {
×
NEW
73
            throw new UncheckedIOException(e);
×
74
        }
75
    }
76

77
    public String toJson(final byte[] messageRaw, final String messageTypeName) {
NEW
78
        Objects.requireNonNull(messageRaw);
×
79

NEW
80
        if (messageTypeName == null) {
×
NEW
81
            final Optional<DynamicMessage> message = parseWithBestMatchingDescriptor(messageRaw);
×
NEW
82
            return toJson(message.orElseThrow(NoDescriptorFoundException::new));
×
83
        }
84

NEW
85
        final Optional<Descriptors.Descriptor> descriptor = cache.getByTypeName(messageTypeName);
×
NEW
86
        return toJson(messageRaw, descriptor.orElseThrow(() -> new NoDescriptorFoundException(messageTypeName)));
×
87
    }
88

89
    @SuppressWarnings("WeakerAccess")
90
    public String toJson(final byte[] messageRaw, final Descriptors.Descriptor descriptor) {
NEW
91
        Objects.requireNonNull(messageRaw);
×
NEW
92
        Objects.requireNonNull(descriptor);
×
93

94
        try {
NEW
95
            return toJson(DynamicMessage.parseFrom(descriptor, messageRaw));
×
NEW
96
        } catch (final InvalidProtocolBufferException e) {
×
NEW
97
            throw new UncheckedInvalidProtocolBufferException(e);
×
98
        }
99
    }
100

101
    @SuppressWarnings("WeakerAccess")
102
    public String toJson(final DynamicMessage message) {
NEW
103
        Objects.requireNonNull(message);
×
104

105
        try {
NEW
106
            return printer.print(message);
×
NEW
107
        } catch (final InvalidProtocolBufferException e) {
×
NEW
108
            throw new UncheckedInvalidProtocolBufferException(e);
×
109
        }
110
    }
111

112
    private Optional<DynamicMessage> parseWithBestMatchingDescriptor(final byte[] messageRaw) {
NEW
113
        Objects.requireNonNull(messageRaw);
×
114

NEW
115
        return cache.getDescriptors()
×
NEW
116
                .stream()
×
NEW
117
                .map(descriptor -> {
×
118
                    try {
NEW
119
                        return DynamicMessage.parseFrom(descriptor, messageRaw);
×
NEW
120
                    } catch (final InvalidProtocolBufferException e) {
×
121
                        //noinspection ReturnOfNull
NEW
122
                        return null;
×
123
                    }
124
                })
NEW
125
                .filter(Objects::nonNull)
×
NEW
126
                .min(Comparator.comparingInt(message -> message.getUnknownFields()
×
NEW
127
                        .asMap()
×
NEW
128
                        .size()));
×
129
    }
130
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc