Skip to content

Commit

Permalink
Merge pull request #87 from knoguchi/descriptor
Browse files Browse the repository at this point in the history
add descriptor loader
  • Loading branch information
cowtowncoder authored Jun 20, 2017
2 parents 2adb585 + 0f33b62 commit e1a976f
Show file tree
Hide file tree
Showing 9 changed files with 1,518 additions and 0 deletions.
4 changes: 4 additions & 0 deletions protobuf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ abstractions.
<artifactId>jackson-annotations</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
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();
}
}
Loading

0 comments on commit e1a976f

Please sign in to comment.