-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use bytes to represent memo text (#259)
The Java SDK assumes that all memo texts must be valid UTF 8 strings. However, that assumption is not valid. It turns out that any sequence of bytes is allowed to appear in the memo field (see stellar/go#2022 ). Therefore, the correct representation for the memo field is a byte array.
- Loading branch information
Showing
17 changed files
with
174 additions
and
71 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
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
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
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
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 |
---|---|---|
@@ -1,54 +1,63 @@ | ||
package org.stellar.sdk; | ||
|
||
import com.google.common.base.Objects; | ||
import org.stellar.sdk.xdr.MemoType; | ||
|
||
import java.nio.charset.Charset; | ||
import org.stellar.sdk.xdr.XdrString; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Represents MEMO_TEXT. | ||
*/ | ||
public class MemoText extends Memo { | ||
private String text; | ||
private XdrString text; | ||
|
||
public MemoText(String text) { | ||
this.text = checkNotNull(text, "text cannot be null"); | ||
this(new XdrString(checkNotNull(text, "text cannot be null"))); | ||
} | ||
|
||
public MemoText(byte[] text) { | ||
this(new XdrString(checkNotNull(text, "text cannot be null"))); | ||
} | ||
|
||
int length = text.getBytes((Charset.forName("UTF-8"))).length; | ||
public MemoText(XdrString text) { | ||
this.text = checkNotNull(text, "text cannot be null"); | ||
int length = this.text.getBytes().length; | ||
if (length > 28) { | ||
throw new MemoTooLongException("text must be <= 28 bytes. length=" + String.valueOf(length)); | ||
} | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
return this.text.toString(); | ||
} | ||
|
||
public byte[] getBytes() { | ||
return this.text.getBytes(); | ||
} | ||
|
||
@Override | ||
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(this.text); | ||
return memo; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(this.text); | ||
return this.text.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
MemoText memoText = (MemoText) o; | ||
return Objects.equal(this.text, memoText.text); | ||
return this.text.equals(memoText.text); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return text == null ? "" : text; | ||
return text == null ? "" : this.getText(); | ||
} | ||
} |
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
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
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
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
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
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
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
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
Oops, something went wrong.