forked from stellar/xdrgen
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(java): Fix the representation of UnsignedInteger and UnsignedHype…
…rInteger. (stellar#163)
- Loading branch information
Showing
22 changed files
with
1,314 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package <%= @namespace %>; | ||
|
||
import com.google.common.base.Objects; | ||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* Represents XDR Unsigned Hyper Integer. | ||
* | ||
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.5">XDR: External Data | ||
* Representation Standard</a> | ||
*/ | ||
public class XdrUnsignedHyperInteger implements XdrElement { | ||
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); | ||
public static final BigInteger MIN_VALUE = BigInteger.ZERO; | ||
private final BigInteger number; | ||
|
||
public XdrUnsignedHyperInteger(BigInteger number) { | ||
if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { | ||
throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); | ||
} | ||
this.number = number; | ||
} | ||
|
||
public XdrUnsignedHyperInteger(Long number) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException( | ||
"number must be greater than or equal to 0 if you want to construct it from Long"); | ||
} | ||
this.number = BigInteger.valueOf(number); | ||
} | ||
|
||
@Override | ||
public void encode(XdrDataOutputStream stream) throws IOException { | ||
stream.write(getBytes()); | ||
} | ||
|
||
public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { | ||
byte[] bytes = new byte[8]; | ||
stream.readFully(bytes); | ||
BigInteger uint64 = new BigInteger(1, bytes); | ||
return new XdrUnsignedHyperInteger(uint64); | ||
} | ||
|
||
private byte[] getBytes() { | ||
byte[] bytes = number.toByteArray(); | ||
byte[] paddedBytes = new byte[8]; | ||
|
||
int numBytesToCopy = Math.min(bytes.length, 8); | ||
int copyStartIndex = bytes.length - numBytesToCopy; | ||
System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); | ||
return paddedBytes; | ||
} | ||
|
||
public BigInteger getNumber() { | ||
return number; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(this.number); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (!(object instanceof XdrUnsignedHyperInteger)) { | ||
return false; | ||
} | ||
|
||
XdrUnsignedHyperInteger other = (XdrUnsignedHyperInteger) object; | ||
return Objects.equal(this.number, other.number); | ||
} | ||
|
||
public String toString() { | ||
return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; | ||
} | ||
} |
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,65 @@ | ||
package <%= @namespace %>; | ||
|
||
import com.google.common.base.Objects; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Represents XDR Unsigned Integer. | ||
* | ||
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.2">XDR: External Data | ||
* Representation Standard</a> | ||
*/ | ||
public class XdrUnsignedInteger implements XdrElement { | ||
public static final long MAX_VALUE = (1L << 32) - 1; | ||
public static final long MIN_VALUE = 0; | ||
private final Long number; | ||
|
||
public XdrUnsignedInteger(Long number) { | ||
if (number < MIN_VALUE || number > MAX_VALUE) { | ||
throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); | ||
} | ||
this.number = number; | ||
} | ||
|
||
public XdrUnsignedInteger(Integer number) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException( | ||
"number must be greater than or equal to 0 if you want to construct it from Integer"); | ||
} | ||
this.number = number.longValue(); | ||
} | ||
|
||
public Long getNumber() { | ||
return number; | ||
} | ||
|
||
public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { | ||
int intValue = stream.readInt(); | ||
long uint32Value = Integer.toUnsignedLong(intValue); | ||
return new XdrUnsignedInteger(uint32Value); | ||
} | ||
|
||
@Override | ||
public void encode(XdrDataOutputStream stream) throws IOException { | ||
stream.writeInt(number.intValue()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(this.number); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (!(object instanceof XdrUnsignedInteger)) { | ||
return false; | ||
} | ||
|
||
XdrUnsignedInteger other = (XdrUnsignedInteger) object; | ||
return Objects.equal(this.number, other.number); | ||
} | ||
|
||
public String toString() { | ||
return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
spec/output/generator_spec_java/block_comments.x/XdrUnsignedHyperInteger.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,77 @@ | ||
package MyXDR; | ||
|
||
import com.google.common.base.Objects; | ||
import java.io.IOException; | ||
import java.math.BigInteger; | ||
|
||
/** | ||
* Represents XDR Unsigned Hyper Integer. | ||
* | ||
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.5">XDR: External Data | ||
* Representation Standard</a> | ||
*/ | ||
public class XdrUnsignedHyperInteger implements XdrElement { | ||
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615"); | ||
public static final BigInteger MIN_VALUE = BigInteger.ZERO; | ||
private final BigInteger number; | ||
|
||
public XdrUnsignedHyperInteger(BigInteger number) { | ||
if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) { | ||
throw new IllegalArgumentException("number must be between 0 and 2^64 - 1 inclusive"); | ||
} | ||
this.number = number; | ||
} | ||
|
||
public XdrUnsignedHyperInteger(Long number) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException( | ||
"number must be greater than or equal to 0 if you want to construct it from Long"); | ||
} | ||
this.number = BigInteger.valueOf(number); | ||
} | ||
|
||
@Override | ||
public void encode(XdrDataOutputStream stream) throws IOException { | ||
stream.write(getBytes()); | ||
} | ||
|
||
public static XdrUnsignedHyperInteger decode(XdrDataInputStream stream) throws IOException { | ||
byte[] bytes = new byte[8]; | ||
stream.readFully(bytes); | ||
BigInteger uint64 = new BigInteger(1, bytes); | ||
return new XdrUnsignedHyperInteger(uint64); | ||
} | ||
|
||
private byte[] getBytes() { | ||
byte[] bytes = number.toByteArray(); | ||
byte[] paddedBytes = new byte[8]; | ||
|
||
int numBytesToCopy = Math.min(bytes.length, 8); | ||
int copyStartIndex = bytes.length - numBytesToCopy; | ||
System.arraycopy(bytes, copyStartIndex, paddedBytes, 8 - numBytesToCopy, numBytesToCopy); | ||
return paddedBytes; | ||
} | ||
|
||
public BigInteger getNumber() { | ||
return number; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(this.number); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (!(object instanceof XdrUnsignedHyperInteger)) { | ||
return false; | ||
} | ||
|
||
XdrUnsignedHyperInteger other = (XdrUnsignedHyperInteger) object; | ||
return Objects.equal(this.number, other.number); | ||
} | ||
|
||
public String toString() { | ||
return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
spec/output/generator_spec_java/block_comments.x/XdrUnsignedInteger.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,65 @@ | ||
package MyXDR; | ||
|
||
import com.google.common.base.Objects; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Represents XDR Unsigned Integer. | ||
* | ||
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.2">XDR: External Data | ||
* Representation Standard</a> | ||
*/ | ||
public class XdrUnsignedInteger implements XdrElement { | ||
public static final long MAX_VALUE = (1L << 32) - 1; | ||
public static final long MIN_VALUE = 0; | ||
private final Long number; | ||
|
||
public XdrUnsignedInteger(Long number) { | ||
if (number < MIN_VALUE || number > MAX_VALUE) { | ||
throw new IllegalArgumentException("number must be between 0 and 2^32 - 1 inclusive"); | ||
} | ||
this.number = number; | ||
} | ||
|
||
public XdrUnsignedInteger(Integer number) { | ||
if (number < 0) { | ||
throw new IllegalArgumentException( | ||
"number must be greater than or equal to 0 if you want to construct it from Integer"); | ||
} | ||
this.number = number.longValue(); | ||
} | ||
|
||
public Long getNumber() { | ||
return number; | ||
} | ||
|
||
public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException { | ||
int intValue = stream.readInt(); | ||
long uint32Value = Integer.toUnsignedLong(intValue); | ||
return new XdrUnsignedInteger(uint32Value); | ||
} | ||
|
||
@Override | ||
public void encode(XdrDataOutputStream stream) throws IOException { | ||
stream.writeInt(number.intValue()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(this.number); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (!(object instanceof XdrUnsignedInteger)) { | ||
return false; | ||
} | ||
|
||
XdrUnsignedInteger other = (XdrUnsignedInteger) object; | ||
return Objects.equal(this.number, other.number); | ||
} | ||
|
||
public String toString() { | ||
return "XdrUnsignedInteger(number=" + this.getNumber() + ")"; | ||
} | ||
} |
Oops, something went wrong.