|
27 | 27 |
|
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.InputStream; |
| 30 | +import java.net.MalformedURLException; |
| 31 | +import java.net.URL; |
30 | 32 | import java.util.Objects; |
31 | 33 |
|
32 | 34 | public class FileBasedProtobufBytesDecoder extends DescriptorBasedProtobufBytesDecoder |
@@ -56,20 +58,43 @@ public String getDescriptorFilePath() |
56 | 58 | @Override |
57 | 59 | protected DescriptorProtos.FileDescriptorSet loadFileDescriptorSet() |
58 | 60 | { |
59 | | - try (InputStream fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath)) { |
| 61 | + InputStream fin; |
| 62 | + DescriptorProtos.FileDescriptorSet descriptorSet; |
| 63 | + try { |
| 64 | + fin = this.getClass().getClassLoader().getResourceAsStream(descriptorFilePath); |
60 | 65 | if (fin == null) { |
61 | | - throw new ParseException(descriptorFilePath, "Descriptor not found in class path [%s]", descriptorFilePath); |
| 66 | + URL url; |
| 67 | + try { |
| 68 | + url = new URL(descriptorFilePath); |
| 69 | + } |
| 70 | + catch (MalformedURLException e) { |
| 71 | + throw new ParseException( |
| 72 | + descriptorFilePath, |
| 73 | + e, |
| 74 | + "Descriptor not found in class path or malformed URL: [%s]", descriptorFilePath |
| 75 | + ); |
| 76 | + } |
| 77 | + try (InputStream urlIn = url.openConnection().getInputStream()) { |
| 78 | + if (urlIn == null) { |
| 79 | + throw new ParseException( |
| 80 | + descriptorFilePath, |
| 81 | + "Descriptor not found at URL: [%s]", descriptorFilePath |
| 82 | + ); |
| 83 | + } |
| 84 | + descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(urlIn); |
| 85 | + } |
| 86 | + } else { |
| 87 | + descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(fin); |
62 | 88 | } |
63 | 89 |
|
64 | | - final var descriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(fin); |
65 | 90 | if (descriptorSet.getFileCount() == 0) { |
66 | 91 | throw new ParseException(null, "No file descriptors found in the descriptor set"); |
67 | 92 | } |
68 | 93 |
|
69 | 94 | return descriptorSet; |
70 | 95 | } |
71 | 96 | catch (IOException e) { |
72 | | - throw new ParseException(descriptorFilePath, e, "Failed to initialize descriptor"); |
| 97 | + throw new ParseException(descriptorFilePath, e, "Failed to initialize descriptor at [%s]", descriptorFilePath); |
73 | 98 | } |
74 | 99 | } |
75 | 100 |
|
|
0 commit comments