Skip to content

Commit

Permalink
feat(java): add the fromXdrBase64, fromXdrByteArray, toXdrBase64 and …
Browse files Browse the repository at this point in the history
…toXdrByteArray methods (stellar#164)
  • Loading branch information
overcat authored Aug 10, 2023
1 parent 6ad6ce3 commit 472eac8
Show file tree
Hide file tree
Showing 76 changed files with 2,175 additions and 11 deletions.
50 changes: 44 additions & 6 deletions lib/xdrgen/generators/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def render_definitions(node, constants_container)
end

def add_imports_for_definition(defn, imports)
imports.add("com.google.common.io.BaseEncoding")
imports.add("java.io.ByteArrayInputStream")
imports.add("java.io.ByteArrayOutputStream")

case defn
when AST::Definitions::Struct ;
defn.members.each do |m|
Expand Down Expand Up @@ -131,36 +135,36 @@ def render_definition(defn, constants_container)
end
end

def render_nested_definitions(defn, out)
def render_nested_definitions(defn, out, post_name="implements XdrElement")
return unless defn.respond_to? :nested_definitions
defn.nested_definitions.each{|ndefn|
case ndefn
when AST::Definitions::Struct ;
name = name ndefn
out.puts "public static class #{name} {"
out.puts "public static class #{name} #{post_name} {"
out.indent do
render_struct ndefn, out
render_nested_definitions ndefn , out
end
out.puts "}"
when AST::Definitions::Enum ;
name = name ndefn
out.puts "public static enum #{name} {"
out.puts "public static enum #{name} #{post_name} {"
out.indent do
render_enum ndefn, out
end
out.puts "}"
when AST::Definitions::Union ;
name = name ndefn
out.puts "public static class #{name} {"
out.puts "public static class #{name} #{post_name} {"
out.indent do
render_union ndefn, out
render_nested_definitions ndefn, out
end
out.puts "}"
when AST::Definitions::Typedef ;
name = name ndefn
out.puts "public static class #{name} {"
out.puts "public static class #{name} #{post_name} {"
out.indent do
render_typedef ndefn, out
end
Expand Down Expand Up @@ -242,6 +246,7 @@ def render_enum(enum, out)
encode(stream, this);
}
EOS
render_base64((name_string enum.name), out)
out.break
end

Expand Down Expand Up @@ -342,6 +347,8 @@ def render_struct(struct, out)
EOS

render_base64((name struct), out)

out.puts "public static final class Builder {"
out.indent do
struct.members.map { |m|
Expand Down Expand Up @@ -453,6 +460,7 @@ def render_typedef(typedef, out)
return #{equals_for_decl}(this.#{typedef.name}, other.#{typedef.name});
}
EOS
render_base64(typedef.name.camelize, out)
end

def render_union(union, out)
Expand Down Expand Up @@ -672,7 +680,7 @@ def render_union(union, out)
return #{equalExpression};
}
EOS

render_base64((name union), out)
out.break
end

Expand Down Expand Up @@ -704,6 +712,36 @@ def render_source_comment(out, defn)
EOS
end

def render_base64(return_type, out)
out.puts <<-EOS.strip_heredoc
@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}
@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}
public static #{return_type} fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes);
}
public static #{return_type} fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}
EOS
end

def encode_member(value, member, out)
case member.declaration
when AST::Declarations::Void
Expand Down
4 changes: 4 additions & 0 deletions lib/xdrgen/generators/java/XdrElement.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ import java.io.IOException;
*/
interface XdrElement {
void encode(XdrDataOutputStream stream) throws IOException;

String toXdrBase64() throws IOException;

byte[] toXdrByteArray() throws IOException;
}
37 changes: 37 additions & 0 deletions lib/xdrgen/generators/java/XdrString.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package <%= @namespace %>;

import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -36,6 +39,40 @@ public class XdrString implements XdrElement {
return this.bytes;
}

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

public static XdrString fromXdrBase64(String xdr) throws IOException {
return fromXdrBase64(xdr, Integer.MAX_VALUE);
}

public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream, maxSize);
}

public static XdrString fromXdrByteArray(byte[] xdr) throws IOException {
return fromXdrByteArray(xdr, Integer.MAX_VALUE);
}

@Override
public int hashCode() {
return Arrays.hashCode(this.bytes);
Expand Down
30 changes: 30 additions & 0 deletions lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package <%= @namespace %>;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;

Expand Down Expand Up @@ -56,6 +60,32 @@ public class XdrUnsignedHyperInteger implements XdrElement {
return number;
}

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes);
}

public static XdrUnsignedHyperInteger fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}

@Override
public int hashCode() {
return Objects.hashCode(this.number);
Expand Down
30 changes: 30 additions & 0 deletions lib/xdrgen/generators/java/XdrUnsignedInteger.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package <%= @namespace %>;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
Expand Down Expand Up @@ -44,6 +48,32 @@ public class XdrUnsignedInteger implements XdrElement {
stream.writeInt(number.intValue());
}

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes);
}

public static XdrUnsignedInteger fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}

@Override
public int hashCode() {
return Objects.hashCode(this.number);
Expand Down
28 changes: 28 additions & 0 deletions spec/output/generator_spec_java/block_comments.x/AccountFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

// === xdr source ============================================================

Expand Down Expand Up @@ -44,4 +47,29 @@ public static void encode(XdrDataOutputStream stream, AccountFlags value) throws
public void encode(XdrDataOutputStream stream) throws IOException {
encode(stream, this);
}
@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static AccountFlags fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes);
}

public static AccountFlags fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
*/
interface XdrElement {
void encode(XdrDataOutputStream stream) throws IOException;

String toXdrBase64() throws IOException;

byte[] toXdrByteArray() throws IOException;
}
37 changes: 37 additions & 0 deletions spec/output/generator_spec_java/block_comments.x/XdrString.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package MyXDR;

import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -36,6 +39,40 @@ public byte[] getBytes() {
return this.bytes;
}

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

public static XdrString fromXdrBase64(String xdr) throws IOException {
return fromXdrBase64(xdr, Integer.MAX_VALUE);
}

public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream, maxSize);
}

public static XdrString fromXdrByteArray(byte[] xdr) throws IOException {
return fromXdrByteArray(xdr, Integer.MAX_VALUE);
}

@Override
public int hashCode() {
return Arrays.hashCode(this.bytes);
Expand Down
Loading

0 comments on commit 472eac8

Please sign in to comment.