}
public static byte[] serializeNlri(final CLinkstateDestination destination) {
+ final byte[] typeBytes = ByteArray.intToBytes(destination.getNlriType().getIntValue(), TYPE_LENGTH);
+
// FIXME: BUG-108: finish this
throw new UnsupportedOperationException("BUG-108: not implemented");
}
final byte[] msgBody = (ntf.getData() == null) ? new byte[ERROR_SIZE] : new byte[ERROR_SIZE + ntf.getData().length];
- msgBody[0] = ByteArray.intToBytes(ntf.getErrorCode())[Integer.SIZE / Byte.SIZE - 1];
+ msgBody[0] = UnsignedBytes.checkedCast(ntf.getErrorCode());
- msgBody[1] = ByteArray.intToBytes(ntf.getErrorSubcode())[Integer.SIZE / Byte.SIZE - 1];
+ msgBody[1] = UnsignedBytes.checkedCast(ntf.getErrorSubcode());
if (ntf.getData() != null) {
System.arraycopy(ntf.getData(), 0, msgBody, ERROR_SIZE, ntf.getData().length);
int offset = 0;
- msgBody[offset] = ByteArray.intToBytes(BGP_VERSION)[(Integer.SIZE / Byte.SIZE) - 1];
+ msgBody[offset] = UnsignedBytes.checkedCast(BGP_VERSION);
offset += VERSION_SIZE;
// When our AS number does not fit into two bytes, we report it as AS_TRANS
openAS = AS_TRANS;
}
- System.arraycopy(ByteArray.longToBytes(openAS), 6, msgBody, offset, AS_SIZE);
+ System.arraycopy(ByteArray.longToBytes(openAS, AS_SIZE), 0, msgBody, offset, AS_SIZE);
offset += AS_SIZE;
- System.arraycopy(ByteArray.intToBytes(open.getHoldTimer()), 2, msgBody, offset, HOLD_TIME_SIZE);
+ System.arraycopy(ByteArray.intToBytes(open.getHoldTimer(), HOLD_TIME_SIZE), 0, msgBody, offset, HOLD_TIME_SIZE);
offset += HOLD_TIME_SIZE;
System.arraycopy(Ipv4Util.bytesForAddress(open.getBgpIdentifier()), 0, msgBody, offset, BGP_ID_SIZE);
offset += BGP_ID_SIZE;
- msgBody[offset] = ByteArray.intToBytes(optParamsLength)[Integer.SIZE / Byte.SIZE - 1];
+ msgBody[offset] = UnsignedBytes.checkedCast(optParamsLength);
int index = MIN_MSG_LENGTH;
if (optParams != null) {
throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
}
if (UnsignedBytes.toInt(body[0]) != BGP_VERSION) {
- throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(body[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED, ByteArray.subByte(
- ByteArray.intToBytes(BGP_VERSION), 2, 2));
+ throw new BGPDocumentedException("BGP Protocol version " + UnsignedBytes.toInt(body[0]) + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
}
int offset = VERSION_SIZE;
}
private static byte[] putAS4BytesParameterValue(final CAs4Bytes param) {
- return ByteArray.subByte(ByteArray.longToBytes(param.getAs4BytesCapability().getAsNumber().getValue()), Long.SIZE / Byte.SIZE
- - AS4_LENGTH, AS4_LENGTH);
+ return ByteArray.longToBytes(param.getAs4BytesCapability().getAsNumber().getValue(), AS4_LENGTH);
}
}
\ No newline at end of file
final byte[] retBytes = new byte[COMMON_HEADER_LENGTH + body.length];
Arrays.fill(retBytes, 0, MARKER_LENGTH, UnsignedBytes.MAX_VALUE);
- System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH), Integer.SIZE / Byte.SIZE - LENGTH_FIELD_LENGTH,
- retBytes, MARKER_LENGTH, LENGTH_FIELD_LENGTH);
+ System.arraycopy(ByteArray.intToBytes(body.length + COMMON_HEADER_LENGTH, LENGTH_FIELD_LENGTH), 0, retBytes, MARKER_LENGTH,
+ LENGTH_FIELD_LENGTH);
retBytes[MARKER_LENGTH + LENGTH_FIELD_LENGTH] = UnsignedBytes.checkedCast(type);
ByteArray.copyWhole(body, retBytes, COMMON_HEADER_LENGTH);
@Before
public void setUp() {
- this.b1 = new Bandwidth(ByteArray.intToBytes(1000));
- this.b2 = new Bandwidth(ByteArray.intToBytes(2000));
- this.b3 = new Bandwidth(ByteArray.intToBytes(2000));
- this.b4 = new Bandwidth(ByteArray.intToBytes(100));
+ this.b1 = new Bandwidth(ByteArray.intToBytes(1000, 4));
+ this.b2 = new Bandwidth(ByteArray.intToBytes(2000, 4));
+ this.b3 = new Bandwidth(ByteArray.intToBytes(2000, 4));
+ this.b4 = new Bandwidth(ByteArray.intToBytes(100, 4));
}
@Test
}
// objLength
- System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH), Integer.SIZE / Byte.SIZE
- - OBJ_LENGTH_F_LENGTH, retBytes, OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
+ System.arraycopy(ByteArray.intToBytes(valueBytes.length + COMMON_OBJECT_HEADER_LENGTH, OBJ_LENGTH_F_LENGTH), 0, retBytes,
+ OBJ_LENGTH_F_OFFSET, OBJ_LENGTH_F_LENGTH);
ByteArray.copyWhole(valueBytes, retBytes, COMMON_OBJECT_HEADER_LENGTH);
return retBytes;
final TlvSerializer serializer = this.tlvReg.getTlvSerializer(tlv);
- final byte[] typeBytes = (ByteArray.cutBytes(ByteArray.intToBytes(serializer.getType()), (Integer.SIZE / 8) - TLV_TYPE_F_LENGTH));
+ final byte[] typeBytes = ByteArray.intToBytes(serializer.getType(), TLV_TYPE_F_LENGTH);
final byte[] valueBytes = serializer.serializeTlv(tlv);
- final byte[] lengthBytes = ByteArray.cutBytes(ByteArray.intToBytes(valueBytes.length), (Integer.SIZE / 8) - TLV_LENGTH_F_LENGTH);
+ final byte[] lengthBytes = ByteArray.intToBytes(valueBytes.length, TLV_LENGTH_F_LENGTH);
final byte[] bytes = new byte[TLV_HEADER_LENGTH + valueBytes.length + getPadding(TLV_HEADER_LENGTH + valueBytes.length, PADDED_TO)];
final byte[] retBytes = new byte[TLVS_F_OFFSET];
- System.arraycopy(ByteArray.longToBytes(lspaObj.getExcludeAny().getValue()), 4, retBytes, EXC_ANY_F_OFFSET, EXC_ANY_F_LENGTH);
- System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAny().getValue()), 4, retBytes, INC_ANY_F_OFFSET, INC_ANY_F_LENGTH);
- System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAll().getValue()), 4, retBytes, INC_ALL_F_OFFSET, INC_ALL_F_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(lspaObj.getExcludeAny().getValue(), EXC_ANY_F_LENGTH), 0, retBytes, EXC_ANY_F_OFFSET,
+ EXC_ANY_F_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAny().getValue(), INC_ANY_F_LENGTH), 0, retBytes, INC_ANY_F_OFFSET,
+ INC_ANY_F_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(lspaObj.getIncludeAll().getValue(), INC_ALL_F_LENGTH), 0, retBytes, INC_ALL_F_OFFSET,
+ INC_ALL_F_LENGTH);
retBytes[SET_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getSetupPriority());
retBytes[HOLD_PRIO_F_OFFSET] = UnsignedBytes.checkedCast(lspaObj.getHoldPriority());
final RpBuilder builder = new RpBuilder();
builder.setIgnore(header.isIgnore());
- //FIXME : change binary files
- //if (!header.isProcessingRule()) {
- //LOG.debug("Processed bit not set on RP OBJECT, ignoring it");
- // return null;
- //}
+ // FIXME : change binary files
+ // if (!header.isProcessingRule()) {
+ // LOG.debug("Processed bit not set on RP OBJECT, ignoring it");
+ // return null;
+ // }
builder.setProcessingRule(header.isProcessingRule());
final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_PRI_MF_LENGTH), retBytes, FLAGS_PRI_MF_OFFSET);
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(rPObj.getRequestId().getValue()), (Long.SIZE / Byte.SIZE)
- - RID_F_LENGTH, RID_F_LENGTH), retBytes, RID_F_OFFSET);
+ ByteArray.copyWhole(ByteArray.longToBytes(rPObj.getRequestId().getValue(), RID_F_LENGTH), retBytes, RID_F_OFFSET);
if (tlvs.length != 0) {
ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
}
throw new IllegalArgumentException("Min/Max values for SRP ID are reserved.");
}
final byte[] retBytes = new byte[MIN_SIZE];
- System.arraycopy(ByteArray.intToBytes(id.intValue()), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
+ System.arraycopy(ByteArray.intToBytes(id.intValue(), SRP_ID_SIZE), 0, retBytes, FLAGS_SIZE, SRP_ID_SIZE);
return retBytes;
}
ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
for (int i = 0; i < requestIDs.size(); i++) {
- System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue()), 4, retBytes, REQ_LIST_ITEM_LENGTH * i
- + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(requestIDs.get(i).getValue(), REQ_LIST_ITEM_LENGTH), 0, retBytes, REQ_LIST_ITEM_LENGTH
+ * i + REQ_ID_LIST_OFFSET, REQ_LIST_ITEM_LENGTH);
}
assert !(requestIDs.isEmpty()) : "Empty Svec Object - no request ids.";
return retBytes;
final SubobjectType s = subobject.getSubobjectType();
- System.arraycopy(ByteArray.longToBytes(((AsNumberSubobject) s).getAsNumber().getValue()), Long.SIZE / Byte.SIZE - AS_NUMBER_LENGTH,
- retBytes, AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(((AsNumberSubobject) s).getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes,
+ AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
return retBytes;
}
byte[] retBytes;
retBytes = new byte[CONTENT_LENGTH];
final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getRouterId()), 4, ROUTER_ID_NUMBER_LENGTH), retBytes,
- ROUTER_ID_NUMBER_OFFSET);
- System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
+ ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
+ System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
return retBytes;
}
flags.set(LPA_F_OFFSET, subobject.isProtectionAvailable());
flags.set(LPIU_F_OFFSET, subobject.isProtectionInUse());
retBytes[0] = ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH)[0];
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getRouterId()), 4, ROUTER_ID_NUMBER_LENGTH), retBytes,
- ROUTER_ID_NUMBER_OFFSET);
- System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
+ ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
+ System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
return retBytes;
}
if (!(subobject instanceof Type1Label)) {
throw new IllegalArgumentException("Unknown Label Subobject instance. Passed " + subobject.getClass() + ". Needed Type1Label.");
}
- return ByteArray.subByte(ByteArray.longToBytes(((Type1Label) subobject).getType1Label().longValue()), 4, LABEL_LENGTH);
+ return ByteArray.longToBytes(((Type1Label) subobject).getType1Label().longValue(), LABEL_LENGTH);
}
@Override
}
final byte[] retBytes = new byte[CONTENT_LENGTH];
final WavebandSwitchingLabel obj = (WavebandSwitchingLabel) subobject;
- System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue()), 0, retBytes, 0, WAVEB_F_LENGTH);
- System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue()), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
- System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue()), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH, END_F_LENGTH);
+ System.arraycopy(ByteArray.intToBytes(obj.getWavebandId().intValue(), WAVEB_F_LENGTH), 0, retBytes, 0, WAVEB_F_LENGTH);
+ System.arraycopy(ByteArray.intToBytes(obj.getStartLabel().intValue(), START_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH, START_F_LENGTH);
+ System.arraycopy(ByteArray.intToBytes(obj.getEndLabel().intValue(), END_F_LENGTH), 0, retBytes, WAVEB_F_LENGTH + START_F_LENGTH,
+ END_F_LENGTH);
return retBytes;
}
}
final byte[] retBytes = new byte[CONTENT_LENGTH];
final AsNumberSubobject obj = (AsNumberSubobject) subobject.getSubobjectType();
- System.arraycopy(ByteArray.longToBytes(obj.getAsNumber().getValue()), Long.SIZE / Byte.SIZE - AS_NUMBER_LENGTH, retBytes,
- AS_NUMBER_OFFSET, AS_NUMBER_LENGTH);
+ System.arraycopy(ByteArray.longToBytes(obj.getAsNumber().getValue(), AS_NUMBER_LENGTH), 0, retBytes, AS_NUMBER_OFFSET,
+ AS_NUMBER_LENGTH);
return retBytes;
}
if (prefix.getIpv4Prefix() != null) {
final byte[] retBytes = new byte[CONTENT4_LENGTH];
ByteArray.copyWhole(Ipv4Util.bytesForPrefix(prefix.getIpv4Prefix()), retBytes, IP_F_OFFSET);
- retBytes[PREFIX4_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
+ retBytes[PREFIX4_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
retBytes[ATTRIBUTE4_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
return retBytes;
} else {
final byte[] retBytes = new byte[CONTENT6_LENGTH];
ByteArray.copyWhole(Ipv6Util.bytesForPrefix(prefix.getIpv6Prefix()), retBytes, IP_F_OFFSET);
- retBytes[PREFIX6_F_OFFSET] = ByteArray.intToBytes(Ipv4Util.getPrefixLength(prefix))[Integer.SIZE / Byte.SIZE - 1];
+ retBytes[PREFIX6_F_OFFSET] = UnsignedBytes.checkedCast(Ipv4Util.getPrefixLength(prefix));
retBytes[ATTRIBUTE6_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
return retBytes;
}
retBytes = new byte[CONTENT_LENGTH];
final SrlgSubobject specObj = (SrlgSubobject) subobject.getSubobjectType();
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getSrlgId().getValue()), 4, SRLG_ID_NUMBER_LENGTH), retBytes,
- SRLG_ID_NUMBER_OFFSET);
+ ByteArray.copyWhole(ByteArray.longToBytes(specObj.getSrlgId().getValue(), SRLG_ID_NUMBER_LENGTH), retBytes, SRLG_ID_NUMBER_OFFSET);
retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
return retBytes;
final UnnumberedSubobject specObj = (UnnumberedSubobject) subobject.getSubobjectType();
retBytes[ATTRIBUTE_OFFSET] = UnsignedBytes.checkedCast(subobject.getAttribute().getIntValue());
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(specObj.getRouterId()), 4, ROUTER_ID_NUMBER_LENGTH), retBytes,
- ROUTER_ID_NUMBER_OFFSET);
- System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId()), Long.SIZE / Byte.SIZE - INTERFACE_ID_NUMBER_LENGTH, retBytes,
+ ByteArray.copyWhole(ByteArray.longToBytes(specObj.getRouterId(), ROUTER_ID_NUMBER_LENGTH), retBytes, ROUTER_ID_NUMBER_OFFSET);
+ System.arraycopy(ByteArray.longToBytes(specObj.getInterfaceId(), INTERFACE_ID_NUMBER_LENGTH), 0, retBytes,
INTERFACE_ID_NUMBER_OFFSET, INTERFACE_ID_NUMBER_LENGTH);
return retBytes;
}
final Ipv4 ipv4 = (Ipv4) afi;
ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4TunnelSenderAddress()), bytes, offset);
offset += IP4_F_LENGTH;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(lsp.getLspId().getValue()), 6, LSP_ID_F_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
offset += LSP_ID_F_LENGTH;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.intToBytes(lsp.getTunnelId().getValue()), 2, TUNNEL_ID_F_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
offset += TUNNEL_ID_F_LENGTH;
ByteArray.copyWhole(Ipv4Util.bytesForAddress(ipv4.getIpv4ExtendedTunnelId()), bytes, offset);
return bytes;
final Ipv6 ipv6 = (Ipv6) afi;
ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()), bytes, offset);
offset += IP6_F_LENGTH;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(lsp.getLspId().getValue()), 6, LSP_ID_F_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
offset += LSP_ID_F_LENGTH;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.intToBytes(lsp.getTunnelId().getValue()), 2, TUNNEL_ID_F_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
offset += TUNNEL_ID_F_LENGTH;
ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()), bytes, offset);
return bytes;
throw new IllegalArgumentException("LspErrorCodeTlv is mandatory.");
}
final LspErrorCode lsp = (LspErrorCode) tlv;
- return ByteArray.subByte(ByteArray.longToBytes(lsp.getErrorCode()), UPDATE_ERR_CODE_LENGTH, UPDATE_ERR_CODE_LENGTH);
+ return ByteArray.longToBytes(lsp.getErrorCode(), UPDATE_ERR_CODE_LENGTH);
}
@Override
final Order otlv = (Order) tlv;
final byte[] bytes = new byte[ORDR_DEL_LENGTH + ORDR_SETUP_LENGTH];
int offset = 0;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(otlv.getDelete()), 4, ORDR_DEL_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.longToBytes(otlv.getDelete(), ORDR_DEL_LENGTH), bytes, offset);
offset += ORDR_DEL_LENGTH;
- ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(otlv.getSetup()), 4, ORDR_SETUP_LENGTH), bytes, offset);
+ ByteArray.copyWhole(ByteArray.longToBytes(otlv.getSetup(), ORDR_SETUP_LENGTH), bytes, offset);
return bytes;
}
throw new IllegalArgumentException("OverloadedTlv is mandatory.");
}
final OverloadDuration odt = (OverloadDuration) tlv;
- return ByteArray.subByte(ByteArray.longToBytes(odt.getDuration()), OVERLOADED_DURATION_LENGTH, OVERLOADED_DURATION_LENGTH);
+ return ByteArray.longToBytes(odt.getDuration(), OVERLOADED_DURATION_LENGTH);
}
@Override
}
private byte[] serializerUserError(final UserError ue) {
- final byte[] enterprise = ByteArray.subByte(ByteArray.longToBytes(ue.getEnterprise().getValue()), 4, ENTERPRISE_F_LENGTH);
+ final byte[] enterprise = ByteArray.longToBytes(ue.getEnterprise().getValue(), ENTERPRISE_F_LENGTH);
final byte suborg = UnsignedBytes.checkedCast(ue.getSubOrg());
- final byte[] value = ByteArray.subByte(ByteArray.intToBytes(ue.getValue()), 2, USER_VALUE_F_LENGTH);
+ final byte[] value = ByteArray.intToBytes(ue.getValue(), USER_VALUE_F_LENGTH);
final byte[] desc = (ue.getDescription() == null) ? new byte[0] : ue.getDescription().getBytes();
final byte descLen = UnsignedBytes.checkedCast(desc.length);
// TODO: if we have any subobjects
offset += FLAGS_F_LENGTH;
bytes[offset] = UnsignedBytes.checkedCast(rsvp.getCode());
offset += ERROR_CODE_F_LENGTH;
- final byte[] value = ByteArray.subByte(ByteArray.intToBytes(rsvp.getValue().intValue()), 2, ERROR_VALUE_F_LENGTH);
+ final byte[] value = ByteArray.intToBytes(rsvp.getValue().intValue(), ERROR_VALUE_F_LENGTH);
ByteArray.copyWhole(value, bytes, offset);
return bytes;
}
throw new IllegalArgumentException("ReqMissingTlv is mandatory.");
}
final ReqMissing req = (ReqMissing) tlv;
- return ByteArray.subByte(ByteArray.longToBytes(req.getRequestId().getValue()), REQ_ID_LENGTH, REQ_ID_LENGTH);
+ return ByteArray.longToBytes(req.getRequestId().getValue(), REQ_ID_LENGTH);
}
@Override
* @return parsed array of bytes with length of Integer.SIZE/Byte.SIZE
*/
public static byte[] intToBytes(final int num) {
- final ByteBuffer bytesBuffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
- bytesBuffer.putInt(num);
+ return intToBytes(num, Integer.SIZE / Byte.SIZE);
+ }
- return bytesBuffer.array();
+ /**
+ * Parses integer to array of bytes
+ *
+ * @param num integer to be parsed
+ * @param size desired byte array length
+ * @return parsed array of bytes with length of size
+ */
+ public static byte[] intToBytes(final int num, final int size) {
+ final int finalSize = Integer.SIZE / Byte.SIZE;
+ final ByteBuffer bytesBuffer = ByteBuffer.allocate(finalSize);
+ bytesBuffer.putInt(num);
+ return ByteArray.subByte(bytesBuffer.array(), finalSize - size, size);
}
/**
* @param num long to be parsed
* @return parsed array of bytes with length of Long.SIZE/Byte.SIZE
*/
- public static byte[] longToBytes(final long num) {
- final ByteBuffer bytesBuffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
- bytesBuffer.putLong(num);
+ public static byte[] longToBytes(final int num) {
+ return longToBytes(num, Long.SIZE / Byte.SIZE);
+ }
- return bytesBuffer.array();
+ /**
+ * Parses long to array of bytes
+ *
+ * @param num long to be parsed
+ * @param size desired byte array length
+ * @return parsed array of bytes with length of size
+ */
+ public static byte[] longToBytes(final long num, final int size) {
+ final int finalSize = Long.SIZE / Byte.SIZE;
+ final ByteBuffer bytesBuffer = ByteBuffer.allocate(finalSize);
+ bytesBuffer.putLong(num);
+ return ByteArray.subByte(bytesBuffer.array(), finalSize - size, size);
}
/**
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte2(){
+ public void testSubByte2() {
ByteArray.subByte(new byte[0], 2, 2);
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte3(){
+ public void testSubByte3() {
ByteArray.subByte(this.before, 2, -1);
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte4(){
+ public void testSubByte4() {
ByteArray.subByte(this.before, -1, 2);
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte5(){
+ public void testSubByte5() {
ByteArray.subByte(this.before, 9, 2);
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte6(){
+ public void testSubByte6() {
ByteArray.subByte(this.before, 2, 19);
}
@Test(expected = IllegalArgumentException.class)
- public void testSubByte7(){
+ public void testSubByte7() {
ByteArray.subByte(this.before, 2, 7);
}
}
@Test(expected = IllegalArgumentException.class)
- public void testCutBytes2(){
+ public void testCutBytes2() {
ByteArray.cutBytes(new byte[0], 5);
}
@Test(expected = IllegalArgumentException.class)
- public void testCutBytes3(){
+ public void testCutBytes3() {
ByteArray.cutBytes(this.before, 9);
}
@Test(expected = IllegalArgumentException.class)
- public void testCutBytes4(){
+ public void testCutBytes4() {
ByteArray.cutBytes(this.before, 0);
}
@Test
public void testIntToBytes() {
- assertEquals(Integer.MAX_VALUE, ByteArray.bytesToInt(ByteArray.intToBytes(Integer.MAX_VALUE)));
- assertEquals(Integer.MIN_VALUE, ByteArray.bytesToInt(ByteArray.intToBytes(Integer.MIN_VALUE)));
- assertEquals(5, ByteArray.bytesToInt(ByteArray.intToBytes(5)));
+ assertEquals(Integer.MAX_VALUE, ByteArray.bytesToInt(ByteArray.intToBytes(Integer.MAX_VALUE, Integer.SIZE / Byte.SIZE)));
+ assertEquals(Integer.MIN_VALUE, ByteArray.bytesToInt(ByteArray.intToBytes(Integer.MIN_VALUE, Integer.SIZE / Byte.SIZE)));
+ assertEquals(2, ByteArray.intToBytes(12, 2).length);
+ assertArrayEquals(new byte[] { 0, 12 }, ByteArray.intToBytes(12, 2));
+ assertEquals(5, ByteArray.bytesToInt(ByteArray.intToBytes(5, 2)));
}
@Test
public void testLongToBytes_bytesToLong() {
- assertEquals(Long.MAX_VALUE, ByteArray.bytesToLong(ByteArray.longToBytes(Long.MAX_VALUE)));
- assertEquals(Long.MIN_VALUE, ByteArray.bytesToLong(ByteArray.longToBytes(Long.MIN_VALUE)));
- assertEquals(5, ByteArray.bytesToLong(ByteArray.longToBytes(5)));
+ assertEquals(Long.MAX_VALUE, ByteArray.bytesToLong(ByteArray.longToBytes(Long.MAX_VALUE, Long.SIZE / Byte.SIZE)));
+ assertEquals(Long.MIN_VALUE, ByteArray.bytesToLong(ByteArray.longToBytes(Long.MIN_VALUE, Long.SIZE / Byte.SIZE)));
+ assertArrayEquals(new byte[] { 0, 0, 5 }, ByteArray.longToBytes(5L, 3));
+ assertEquals(5, ByteArray.bytesToLong(ByteArray.longToBytes(5, 2)));
}
/**
- * if less than 4 bytes are converted, zero bytes should be appendet at the
- * buffer's start
+ * if less than 4 bytes are converted, zero bytes should be appendet at the buffer's start
*/
@Test
public void testBytesToLong_prependingZeros() {
}
@Test(expected = IllegalArgumentException.class)
- public void testBytesToInt(){
+ public void testBytesToInt() {
final byte[] b = new byte[Integer.SIZE + 1];
ByteArray.bytesToInt(b);
}
@Test(expected = IllegalArgumentException.class)
- public void testBytesToShort2(){
+ public void testBytesToShort2() {
final byte[] b = new byte[Short.SIZE + 1];
ByteArray.bytesToInt(b);
}
@Test
- public void testBytes(){
- assertTrue(ByteArray.bytesToInt(new byte[]{ 0, 0, 0, 15}) == 15);
- assertEquals(Float.valueOf((float) 1.4E-45), Float.valueOf(ByteArray.bytesToFloat(new byte[]{ 0, 0, 0, 1})));
+ public void testBytes() {
+ assertTrue(ByteArray.bytesToInt(new byte[] { 0, 0, 0, 15 }) == 15);
+ assertEquals(Float.valueOf((float) 1.4E-45), Float.valueOf(ByteArray.bytesToFloat(new byte[] { 0, 0, 0, 1 })));
assertEquals(Long.valueOf(16613001005322L), Long.valueOf(ByteArray.bytesToLong(this.before)));
- assertEquals(Short.valueOf((short) 1), Short.valueOf(ByteArray.bytesToShort(new byte[]{0, 1})));
+ assertEquals(Short.valueOf((short) 1), Short.valueOf(ByteArray.bytesToShort(new byte[] { 0, 1 })));
}
-
@Test
public void testCopyBitRange() {
assertEquals((byte) 10, ByteArray.copyBitsRange((byte) 0x28, 2, 4));
}
@Test(expected = IllegalArgumentException.class)
- public void testCopyBitsRange2(){
+ public void testCopyBitsRange2() {
ByteArray.copyBitsRange((byte) 0x28, -1, 4);
}
@Test(expected = IllegalArgumentException.class)
- public void testCopyBitsRange3(){
+ public void testCopyBitsRange3() {
ByteArray.copyBitsRange((byte) 0x28, 1, 187);
}
@Test(expected = IllegalArgumentException.class)
- public void testCopyBitsRange4(){
+ public void testCopyBitsRange4() {
ByteArray.copyBitsRange((byte) 0x28, 1, 40);
}
@Test(expected = IllegalArgumentException.class)
- public void testCopyBitsRange5(){
+ public void testCopyBitsRange5() {
ByteArray.copyBitsRange((byte) 0x28, 28, 2);
}
@Test(expected = IllegalArgumentException.class)
- public void testCopyBitsRange6(){
+ public void testCopyBitsRange6() {
ByteArray.copyBitsRange((byte) 0x28, 2, -2);
}
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
- public void testCopyWhole2(){
+ public void testCopyWhole2() {
ByteArray.copyWhole(new byte[0], new byte[1], 2);
}
@Test
public void testBytesToHexString() {
- final byte[] b = new byte[] { 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, };
+ final byte[] b = new byte[] { 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01, 0x16, 0x01,
+ 0x16, };
final String expected = "01 16 01 16 01 16 01 16 01 16 01 16 01 16 01 16\n01 16 ";
assertEquals(expected, ByteArray.bytesToHexString(b));
}
@Test
public void testFindByteSequence() {
- final byte[] bytes = new byte[] { (byte)36, (byte)41, (byte)55, (byte)101, (byte)38 };
- final byte[] sequence1 = new byte[] { (byte)36, (byte)41 };
+ final byte[] bytes = new byte[] { (byte) 36, (byte) 41, (byte) 55, (byte) 101, (byte) 38 };
+ final byte[] sequence1 = new byte[] { (byte) 36, (byte) 41 };
assertEquals(0, ByteArray.findByteSequence(bytes, sequence1));
- final byte[] sequence2 = new byte[] { (byte)55, (byte)38 };
+ final byte[] sequence2 = new byte[] { (byte) 55, (byte) 38 };
assertEquals(-1, ByteArray.findByteSequence(bytes, sequence2));
- final byte[] sequence3 = new byte[] { (byte)101, (byte)38 };
+ final byte[] sequence3 = new byte[] { (byte) 101, (byte) 38 };
assertEquals(3, ByteArray.findByteSequence(bytes, sequence3));
try {
- ByteArray.findByteSequence(bytes, new byte[] { (byte)36, (byte)41, (byte)55, (byte)101, (byte)38, (byte)66 });
+ ByteArray.findByteSequence(bytes, new byte[] { (byte) 36, (byte) 41, (byte) 55, (byte) 101, (byte) 38, (byte) 66 });
} catch (final IllegalArgumentException e) {
assertEquals("Sequence to be found is longer than the given byte array.", e.getMessage());
}
@Test
public void testMaskBytes() {
- final byte[] bytes = new byte[] {(byte)0xAC, (byte)0xA8, (byte)0x1F, (byte)0x08 };
+ final byte[] bytes = new byte[] { (byte) 0xAC, (byte) 0xA8, (byte) 0x1F, (byte) 0x08 };
try {
ByteArray.maskBytes(bytes, 48);
} catch (final IllegalArgumentException e) {
assertArrayEquals(bytes, ByteArray.maskBytes(bytes, 32));
- assertArrayEquals(new byte[] { (byte)0xAC, (byte)0x80, 0, 0}, ByteArray.maskBytes(bytes, 10));
+ assertArrayEquals(new byte[] { (byte) 0xAC, (byte) 0x80, 0, 0 }, ByteArray.maskBytes(bytes, 10));
}
}