-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from knoguchi/descriptor
add descriptor loader
- Loading branch information
Showing
9 changed files
with
1,518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...obuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/schema/DescriptorLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.fasterxml.jackson.dataformat.protobuf.schema; | ||
|
||
|
||
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
/** | ||
* Class used for loading protobuf descriptors (from .desc files | ||
* or equivalent sources), to construct FileDescriptorSet. | ||
*/ | ||
public class DescriptorLoader | ||
{ | ||
private final String DESCRIPTOR_PROTO = "/descriptor.proto"; | ||
private ProtobufMapper descriptorMapper; | ||
private ProtobufSchema descriptorFileSchema; | ||
|
||
/** | ||
* Standard loader instance that is usually used for loading descriptor file. | ||
*/ | ||
public final static DescriptorLoader std = new DescriptorLoader(); | ||
|
||
public DescriptorLoader() {} | ||
|
||
|
||
/** | ||
* Public API | ||
*/ | ||
|
||
public FileDescriptorSet load(URL url) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(url.openStream()); | ||
} | ||
|
||
public FileDescriptorSet load(File f) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(new FileInputStream(f)); | ||
} | ||
|
||
public FileDescriptorSet load(InputStream in) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(in); | ||
} | ||
|
||
public FileDescriptorSet fromBytes(byte[] descriptorBytes) throws IOException | ||
{ | ||
return _loadFileDescriptorSet(new ByteArrayInputStream(descriptorBytes)); | ||
} | ||
|
||
protected FileDescriptorSet _loadFileDescriptorSet(InputStream in) throws IOException | ||
{ | ||
try { | ||
if (descriptorMapper == null) { | ||
createDescriptorMapper(); | ||
} | ||
return descriptorMapper.readerFor(FileDescriptorSet.class) | ||
.with(descriptorFileSchema) | ||
.readValue(in); | ||
} | ||
finally { | ||
try { | ||
in.close(); | ||
} | ||
catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
private void createDescriptorMapper() throws IOException | ||
{ | ||
// read Descriptor Proto | ||
descriptorMapper = new ProtobufMapper(); | ||
InputStream in = getClass().getResourceAsStream(DESCRIPTOR_PROTO); | ||
descriptorFileSchema = ProtobufSchemaLoader.std.load(in, "FileDescriptorSet"); | ||
in.close(); | ||
} | ||
} |
Oops, something went wrong.