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

jiangxincode / ApkToolBoxGUI / #1149

30 Aug 2025 01:23PM UTC coverage: 3.023% (+0.02%) from 3.008%
#1149

push

jiangxincode
code optimized

0 of 13 new or added lines in 3 files covered. (0.0%)

3 existing lines in 1 file now uncovered.

248 of 8204 relevant lines covered (3.02%)

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) {
21
        Objects.requireNonNull(cache);
×
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

115
        return cache.getDescriptors()
×
116
                .stream()
×
117
                .map(descriptor -> {
×
118
                    try {
119
                        return DynamicMessage.parseFrom(descriptor, messageRaw);
×
120
                    } catch (final InvalidProtocolBufferException e) {
×
121
                        //noinspection ReturnOfNull
122
                        return null;
×
123
                    }
124
                })
125
                .filter(Objects::nonNull)
×
126
                .min(Comparator.comparingInt(message -> message.getUnknownFields()
×
127
                        .asMap()
×
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