Skip to content

Commit

Permalink
Use XdrString class
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Dec 11, 2019
1 parent 4525412 commit 2f35c8b
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 75 deletions.
9 changes: 3 additions & 6 deletions src/main/java/org/stellar/sdk/ManageDataOperation.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.stellar.sdk;

import com.google.common.base.Objects;
import org.stellar.sdk.xdr.DataValue;
import org.stellar.sdk.xdr.ManageDataOp;
import org.stellar.sdk.xdr.OperationType;
import org.stellar.sdk.xdr.String64;
import org.stellar.sdk.xdr.*;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -43,7 +40,7 @@ public byte[] getValue() {
org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
ManageDataOp op = new ManageDataOp();
String64 name = new String64();
name.setString64(this.name.getBytes(Charset.forName("UTF-8")));
name.setString64(new XdrString(this.name));
op.setDataName(name);

if (value != null) {
Expand All @@ -70,7 +67,7 @@ public static class Builder {
* @param op {@link ManageDataOp}
*/
Builder(ManageDataOp op) {
name = new String(op.getDataName().getString64(), Charset.forName("UTF-8"));
name = op.getDataName().getString64().toString();
if (op.getDataValue() != null) {
value = op.getDataValue().getDataValue();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Memo fromXdr(org.stellar.sdk.xdr.Memo memo) {
case MEMO_ID:
return id(memo.getId().getUint64().longValue());
case MEMO_TEXT:
return text(memo.getText());
return text(memo.getText().getBytes());
case MEMO_HASH:
return hash(memo.getHash().getHash());
case MEMO_RETURN:
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/stellar/sdk/MemoText.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Objects;
import org.stellar.sdk.xdr.MemoType;
import org.stellar.sdk.xdr.XdrString;

import java.nio.charset.Charset;
import java.util.Arrays;
Expand Down Expand Up @@ -37,7 +38,7 @@ public byte[] getBytes() {
org.stellar.sdk.xdr.Memo toXdr() {
org.stellar.sdk.xdr.Memo memo = new org.stellar.sdk.xdr.Memo();
memo.setDiscriminant(MemoType.MEMO_TEXT);
memo.setText(text);
memo.setText(new XdrString(text));
return memo;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/stellar/sdk/SetOptionsOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ org.stellar.sdk.xdr.Operation.OperationBody toOperationBody() {
}
if (homeDomain != null) {
String32 homeDomain = new String32();
homeDomain.setString32(this.homeDomain.getBytes(Charset.forName("UTF-8")));
homeDomain.setString32(new XdrString(this.homeDomain));
op.setHomeDomain(homeDomain);
}
if (signer != null) {
Expand Down Expand Up @@ -208,7 +208,7 @@ public static class Builder {
highThreshold = op.getHighThreshold().getUint32().intValue();
}
if (op.getHomeDomain() != null) {
homeDomain = new String(op.getHomeDomain().getString32(), Charset.forName("UTF-8"));
homeDomain = op.getHomeDomain().getString32().toString();
}
if (op.getSigner() != null) {
signer = op.getSigner().getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public TransactionResponse deserialize(JsonElement json, Type typeOfT, JsonDeser
// so we must throw it as a runtime exception
throw new RuntimeException(e);
}
memo = Memo.text(transactionEnvelope.getTx().getMemo().getText());
memo = Memo.text(transactionEnvelope.getTx().getMemo().getText().getBytes());
} else {
String memoValue = json.getAsJsonObject().get("memo").getAsString();
BaseEncoding base64Encoding = BaseEncoding.base64();
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/org/stellar/sdk/xdr/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;

import com.google.common.base.Objects;
import java.util.Arrays;

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

Expand All @@ -27,33 +26,29 @@ public ErrorCode getCode() {
public void setCode(ErrorCode value) {
this.code = value;
}
private byte[] msg;
public byte[] getMsg() {
private XdrString msg;
public XdrString getMsg() {
return this.msg;
}
public void setMsg(byte[] value) {
public void setMsg(XdrString value) {
this.msg = value;
}
public static void encode(XdrDataOutputStream stream, Error encodedError) throws IOException{
ErrorCode.encode(stream, encodedError.code);
int msgsize = encodedError.msg.length;
stream.writeInt(msgsize);
stream.write(encodedError.getMsg(), 0, msgsize);
encodedError.msg.encode(stream);
}
public void encode(XdrDataOutputStream stream) throws IOException {
encode(stream, this);
}
public static Error decode(XdrDataInputStream stream) throws IOException {
Error decodedError = new Error();
decodedError.code = ErrorCode.decode(stream);
int msgsize = stream.readInt();
decodedError.msg = new byte[msgsize];
stream.read(decodedError.msg, 0, msgsize);
decodedError.msg = XdrString.decode(stream);
return decodedError;
}
@Override
public int hashCode() {
return Objects.hashCode(this.code, Arrays.hashCode(this.msg));
return Objects.hashCode(this.code, this.msg);
}
@Override
public boolean equals(Object object) {
Expand All @@ -62,6 +57,6 @@ public boolean equals(Object object) {
}

Error other = (Error) object;
return Objects.equal(this.code, other.code) && Arrays.equals(this.msg, other.msg);
return Objects.equal(this.code, other.code) && Objects.equal(this.msg, other.msg);
}
}
19 changes: 7 additions & 12 deletions src/main/java/org/stellar/sdk/xdr/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;

import com.google.common.base.Objects;
import java.util.Arrays;

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

Expand Down Expand Up @@ -55,11 +54,11 @@ public Hash getNetworkID() {
public void setNetworkID(Hash value) {
this.networkID = value;
}
private byte[] versionStr;
public byte[] getVersionStr() {
private XdrString versionStr;
public XdrString getVersionStr() {
return this.versionStr;
}
public void setVersionStr(byte[] value) {
public void setVersionStr(XdrString value) {
this.versionStr = value;
}
private Integer listeningPort;
Expand Down Expand Up @@ -95,9 +94,7 @@ public static void encode(XdrDataOutputStream stream, Hello encodedHello) throws
Uint32.encode(stream, encodedHello.overlayVersion);
Uint32.encode(stream, encodedHello.overlayMinVersion);
Hash.encode(stream, encodedHello.networkID);
int versionStrsize = encodedHello.versionStr.length;
stream.writeInt(versionStrsize);
stream.write(encodedHello.getVersionStr(), 0, versionStrsize);
encodedHello.versionStr.encode(stream);
stream.writeInt(encodedHello.listeningPort);
NodeID.encode(stream, encodedHello.peerID);
AuthCert.encode(stream, encodedHello.cert);
Expand All @@ -112,9 +109,7 @@ public static Hello decode(XdrDataInputStream stream) throws IOException {
decodedHello.overlayVersion = Uint32.decode(stream);
decodedHello.overlayMinVersion = Uint32.decode(stream);
decodedHello.networkID = Hash.decode(stream);
int versionStrsize = stream.readInt();
decodedHello.versionStr = new byte[versionStrsize];
stream.read(decodedHello.versionStr, 0, versionStrsize);
decodedHello.versionStr = XdrString.decode(stream);
decodedHello.listeningPort = stream.readInt();
decodedHello.peerID = NodeID.decode(stream);
decodedHello.cert = AuthCert.decode(stream);
Expand All @@ -123,7 +118,7 @@ public static Hello decode(XdrDataInputStream stream) throws IOException {
}
@Override
public int hashCode() {
return Objects.hashCode(this.ledgerVersion, this.overlayVersion, this.overlayMinVersion, this.networkID, Arrays.hashCode(this.versionStr), this.listeningPort, this.peerID, this.cert, this.nonce);
return Objects.hashCode(this.ledgerVersion, this.overlayVersion, this.overlayMinVersion, this.networkID, this.versionStr, this.listeningPort, this.peerID, this.cert, this.nonce);
}
@Override
public boolean equals(Object object) {
Expand All @@ -132,6 +127,6 @@ public boolean equals(Object object) {
}

Hello other = (Hello) object;
return Objects.equal(this.ledgerVersion, other.ledgerVersion) && Objects.equal(this.overlayVersion, other.overlayVersion) && Objects.equal(this.overlayMinVersion, other.overlayMinVersion) && Objects.equal(this.networkID, other.networkID) && Arrays.equals(this.versionStr, other.versionStr) && Objects.equal(this.listeningPort, other.listeningPort) && Objects.equal(this.peerID, other.peerID) && Objects.equal(this.cert, other.cert) && Objects.equal(this.nonce, other.nonce);
return Objects.equal(this.ledgerVersion, other.ledgerVersion) && Objects.equal(this.overlayVersion, other.overlayVersion) && Objects.equal(this.overlayMinVersion, other.overlayMinVersion) && Objects.equal(this.networkID, other.networkID) && Objects.equal(this.versionStr, other.versionStr) && Objects.equal(this.listeningPort, other.listeningPort) && Objects.equal(this.peerID, other.peerID) && Objects.equal(this.cert, other.cert) && Objects.equal(this.nonce, other.nonce);
}
}
19 changes: 7 additions & 12 deletions src/main/java/org/stellar/sdk/xdr/Memo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.IOException;

import com.google.common.base.Objects;
import java.util.Arrays;

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

Expand Down Expand Up @@ -35,11 +34,11 @@ public MemoType getDiscriminant() {
public void setDiscriminant(MemoType value) {
this.type = value;
}
private byte[] text;
public byte[] getText() {
private XdrString text;
public XdrString getText() {
return this.text;
}
public void setText(byte[] value) {
public void setText(XdrString value) {
this.text = value;
}
private Uint64 id;
Expand Down Expand Up @@ -71,9 +70,7 @@ public static void encode(XdrDataOutputStream stream, Memo encodedMemo) throws I
case MEMO_NONE:
break;
case MEMO_TEXT:
int textsize = encodedMemo.text.length;
stream.writeInt(textsize);
stream.write(encodedMemo.getText(), 0, textsize);
encodedMemo.text.encode(stream);
break;
case MEMO_ID:
Uint64.encode(stream, encodedMemo.id);
Expand All @@ -97,9 +94,7 @@ public static Memo decode(XdrDataInputStream stream) throws IOException {
case MEMO_NONE:
break;
case MEMO_TEXT:
int textsize = stream.readInt();
decodedMemo.text = new byte[textsize];
stream.read(decodedMemo.text, 0, textsize);
decodedMemo.text = XdrString.decode(stream);
break;
case MEMO_ID:
decodedMemo.id = Uint64.decode(stream);
Expand All @@ -115,7 +110,7 @@ public static Memo decode(XdrDataInputStream stream) throws IOException {
}
@Override
public int hashCode() {
return Objects.hashCode(Arrays.hashCode(this.text), this.id, this.hash, this.retHash, this.type);
return Objects.hashCode(this.text, this.id, this.hash, this.retHash, this.type);
}
@Override
public boolean equals(Object object) {
Expand All @@ -124,6 +119,6 @@ public boolean equals(Object object) {
}

Memo other = (Memo) object;
return Arrays.equals(this.text, other.text) && Objects.equal(this.id, other.id) && Objects.equal(this.hash, other.hash) && Objects.equal(this.retHash, other.retHash) && Objects.equal(this.type, other.type);
return Objects.equal(this.text, other.text) && Objects.equal(this.id, other.id) && Objects.equal(this.hash, other.hash) && Objects.equal(this.retHash, other.retHash) && Objects.equal(this.type, other.type);
}
}
20 changes: 8 additions & 12 deletions src/main/java/org/stellar/sdk/xdr/String32.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,35 @@

import java.io.IOException;

import java.util.Arrays;
import com.google.common.base.Objects;

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

// typedef string string32<32>;

// ===========================================================================
public class String32 implements XdrElement {
private byte[] string32;
public byte[] getString32() {
private XdrString string32;
public XdrString getString32() {
return this.string32;
}
public void setString32(byte[] value) {
public void setString32(XdrString value) {
this.string32 = value;
}
public static void encode(XdrDataOutputStream stream, String32 encodedString32) throws IOException {
int string32size = encodedString32.string32.length;
stream.writeInt(string32size);
stream.write(encodedString32.getString32(), 0, string32size);
encodedString32.string32.encode(stream);
}
public void encode(XdrDataOutputStream stream) throws IOException {
encode(stream, this);
}
public static String32 decode(XdrDataInputStream stream) throws IOException {
String32 decodedString32 = new String32();
int string32size = stream.readInt();
decodedString32.string32 = new byte[string32size];
stream.read(decodedString32.string32, 0, string32size);
decodedString32.string32 = XdrString.decode(stream);
return decodedString32;
}
@Override
public int hashCode() {
return Arrays.hashCode(this.string32);
return Objects.hashCode(this.string32);
}
@Override
public boolean equals(Object object) {
Expand All @@ -47,6 +43,6 @@ public boolean equals(Object object) {
}

String32 other = (String32) object;
return Arrays.equals(this.string32, other.string32);
return Objects.equal(this.string32, other.string32);
}
}
20 changes: 8 additions & 12 deletions src/main/java/org/stellar/sdk/xdr/String64.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,35 @@

import java.io.IOException;

import java.util.Arrays;
import com.google.common.base.Objects;

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

// typedef string string64<64>;

// ===========================================================================
public class String64 implements XdrElement {
private byte[] string64;
public byte[] getString64() {
private XdrString string64;
public XdrString getString64() {
return this.string64;
}
public void setString64(byte[] value) {
public void setString64(XdrString value) {
this.string64 = value;
}
public static void encode(XdrDataOutputStream stream, String64 encodedString64) throws IOException {
int string64size = encodedString64.string64.length;
stream.writeInt(string64size);
stream.write(encodedString64.getString64(), 0, string64size);
encodedString64.string64.encode(stream);
}
public void encode(XdrDataOutputStream stream) throws IOException {
encode(stream, this);
}
public static String64 decode(XdrDataInputStream stream) throws IOException {
String64 decodedString64 = new String64();
int string64size = stream.readInt();
decodedString64.string64 = new byte[string64size];
stream.read(decodedString64.string64, 0, string64size);
decodedString64.string64 = XdrString.decode(stream);
return decodedString64;
}
@Override
public int hashCode() {
return Arrays.hashCode(this.string64);
return Objects.hashCode(this.string64);
}
@Override
public boolean equals(Object object) {
Expand All @@ -47,6 +43,6 @@ public boolean equals(Object object) {
}

String64 other = (String64) object;
return Arrays.equals(this.string64, other.string64);
return Objects.equal(this.string64, other.string64);
}
}
3 changes: 1 addition & 2 deletions src/test/java/org/stellar/sdk/XdrDataStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static String backAndForthXdrStreaming(String inputString) throws IOExcep
XdrDataInputStream xdrInputStream = new XdrDataInputStream(new ByteArrayInputStream(xdrByteOutput));
xdrMemo = org.stellar.sdk.xdr.Memo.decode(xdrInputStream);

return Memo.text(xdrMemo.getText()).getText();

return xdrMemo.getText().toString();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/stellar/sdk/xdr/TransactionDecodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.google.common.io.BaseEncoding;
import org.junit.Test;
import org.stellar.sdk.Memo;
import org.stellar.sdk.MemoText;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down

0 comments on commit 2f35c8b

Please sign in to comment.