|
1 | 1 | package com.fasterxml.jackson.dataformat.protobuf.schema;
|
2 | 2 |
|
3 |
| - |
4 |
| -import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper; |
5 |
| - |
6 |
| -import java.io.ByteArrayInputStream; |
7 | 3 | import java.io.File;
|
8 |
| -import java.io.FileInputStream; |
9 | 4 | import java.io.IOException;
|
10 | 5 | import java.io.InputStream;
|
| 6 | +import java.io.Reader; |
11 | 7 | import java.net.URL;
|
12 | 8 |
|
| 9 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 10 | +import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper; |
| 11 | + |
13 | 12 | /**
|
14 | 13 | * Class used for loading protobuf descriptors (from .desc files
|
15 | 14 | * or equivalent sources), to construct FileDescriptorSet.
|
| 15 | + * |
| 16 | + * @since 2.9 |
16 | 17 | */
|
17 | 18 | public class DescriptorLoader
|
18 | 19 | {
|
19 |
| - private final String DESCRIPTOR_PROTO = "/descriptor.proto"; |
20 |
| - private ProtobufMapper descriptorMapper; |
21 |
| - private ProtobufSchema descriptorFileSchema; |
| 20 | + protected final static String DESCRIPTOR_PROTO = "/descriptor.proto"; |
22 | 21 |
|
23 | 22 | /**
|
24 |
| - * Standard loader instance that is usually used for loading descriptor file. |
| 23 | + * Fully configured reader for {@link FileDescriptorSet} objects. |
25 | 24 | */
|
26 |
| - public final static DescriptorLoader std = new DescriptorLoader(); |
27 |
| - |
28 |
| - public DescriptorLoader() {} |
| 25 | + protected final ObjectReader _reader; |
29 | 26 |
|
30 |
| - |
31 |
| - /** |
32 |
| - * Public API |
33 |
| - */ |
34 |
| - |
35 |
| - public FileDescriptorSet load(URL url) throws IOException |
36 |
| - { |
37 |
| - return _loadFileDescriptorSet(url.openStream()); |
| 27 | + public DescriptorLoader(ObjectReader reader) { |
| 28 | + _reader = reader; |
38 | 29 | }
|
39 | 30 |
|
40 |
| - public FileDescriptorSet load(File f) throws IOException |
| 31 | + public static DescriptorLoader construct(ProtobufMapper mapper) throws IOException |
41 | 32 | {
|
42 |
| - return _loadFileDescriptorSet(new FileInputStream(f)); |
| 33 | + ProtobufSchema schema; |
| 34 | + try (InputStream in = DescriptorLoader.class.getResourceAsStream(DESCRIPTOR_PROTO)) { |
| 35 | + schema = mapper.schemaLoader().load(in, "FileDescriptorSet"); |
| 36 | + } |
| 37 | + return new DescriptorLoader(mapper.readerFor(FileDescriptorSet.class) |
| 38 | + .with(schema)); |
43 | 39 | }
|
44 | 40 |
|
45 |
| - public FileDescriptorSet load(InputStream in) throws IOException |
| 41 | + /* |
| 42 | + /********************************************************************** |
| 43 | + /* Public API |
| 44 | + /********************************************************************** |
| 45 | + */ |
| 46 | + |
| 47 | + public FileDescriptorSet load(URL src) throws IOException |
46 | 48 | {
|
47 |
| - return _loadFileDescriptorSet(in); |
| 49 | + return _reader.readValue(src); |
48 | 50 | }
|
49 | 51 |
|
50 |
| - public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException |
| 52 | + public FileDescriptorSet load(File src) throws IOException |
51 | 53 | {
|
52 |
| - return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes)); |
| 54 | + return _reader.readValue(src); |
53 | 55 | }
|
54 | 56 |
|
55 |
| - protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException |
| 57 | + /** |
| 58 | + * Note: passed {@link java.io.InputStream} will be closed by this method. |
| 59 | + */ |
| 60 | + public FileDescriptorSet load(InputStream in) throws IOException |
56 | 61 | {
|
57 |
| - try { |
58 |
| - if (descriptorMapper == null) { |
59 |
| - createDescriptorMapper(); |
60 |
| - } |
61 |
| - return descriptorMapper.readerFor(FileDescriptorSet.class) |
62 |
| - .with(descriptorFileSchema) |
63 |
| - .readValue(in); |
64 |
| - } |
65 |
| - finally { |
66 |
| - try { |
67 |
| - in.close(); |
68 |
| - } |
69 |
| - catch (IOException e) { |
70 |
| - } |
71 |
| - } |
| 62 | + return _reader.readValue(in); |
72 | 63 | }
|
73 | 64 |
|
74 |
| - private void createDescriptorMapper() throws IOException |
| 65 | + /** |
| 66 | + * Note: passed {@link java.io.Reader} will be closed by this method. |
| 67 | + */ |
| 68 | + public FileDescriptorSet load(Reader r) throws IOException |
75 | 69 | {
|
76 |
| - // read Descriptor Proto |
77 |
| - descriptorMapper = new ProtobufMapper(); |
78 |
| - InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO); |
79 |
| - descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet"); |
80 |
| - in.close(); |
| 70 | + return _reader.readValue(r); |
81 | 71 | }
|
82 | 72 | }
|
0 commit comments