package org.umu.cops.stack;
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Shorts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.umu.cops.stack.COPSHeader.Flag;
* @return - a 2 byte array
*/
public static byte[] shortToBytes(final short val) {
- final byte[] out = new byte[2];
- out[0] = (byte) (val >> 8);
- out[1] = (byte) val;
- return out;
+ return Shorts.toByteArray(val);
}
/**
* @return - the short value
*/
public static short bytesToShort(final byte byte1, final byte byte2) {
- short out = 0;
- out |= ((short) byte1) << 8;
- out |= ((short) byte2) & 0xFF;
- return out;
+ return Shorts.fromBytes(byte1, byte2);
+ }
+
+ public static byte[] intToBytes(final int value) {
+ return Ints.toByteArray(value);
+ }
+
+ public static int bytesToInt(final byte byte1, final byte byte2, final byte byte3, final byte byte4) {
+ return Ints.fromBytes(byte1, byte2, byte3, byte4);
}
private static COPSMsg readBody(final Socket socket, final COPSHeaderData hdrData) throws IOException, COPSException {
final int expectedBytes = hdrData.msgByteCount - hdrData.header.getHdrLength();
final byte[] buffer = new byte[expectedBytes];
final int nread = readData(socket, buffer, expectedBytes);
-// buffer[expectedBytes] = (byte) '\0';
if (nread != expectedBytes) {
throw new COPSException("Bad COPS message");
}
+/*
+ * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
+ */
+
package org.umu.cops.stack;
import org.junit.Assert;
import org.junit.Test;
import org.pcmm.rcd.IPCMMClient;
+import java.util.Random;
+
/**
* Tests the public static COPSMsgParser methods
*/
Assert.assertEquals(byte2, outBytes[1]);
}
+ @Test
+ public void bytesToShortAndBack() {
+ final Random rnd = new Random();
+ final short val = (short)rnd.nextInt();
+ final byte[] bytes = COPSMsgParser.shortToBytes(val);
+ final short parsed = COPSMsgParser.bytesToShort(bytes[0], bytes[1]);
+ Assert.assertEquals(val, parsed);
+ }
+
+ @Test
+ public void testBytesToIntMin() {
+ final byte byte1 = (byte)0;
+ final byte byte2 = (byte)0;
+ final byte byte3 = (byte)0;
+ final byte byte4 = (byte)0;
+ final int val = COPSMsgParser.bytesToInt(byte1, byte2, byte3, byte4);
+ final byte[] outBytes = COPSMsgParser.intToBytes(val);
+ Assert.assertEquals(byte1, outBytes[0]);
+ Assert.assertEquals(byte2, outBytes[1]);
+ Assert.assertEquals(byte3, outBytes[2]);
+ Assert.assertEquals(byte4, outBytes[3]);
+ Assert.assertEquals(0, val);
+ }
+
+ @Test
+ public void intToBytesAndBack() {
+ final int val = 100001;
+ final byte[] bytes = COPSMsgParser.intToBytes(val);
+ final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
+ Assert.assertEquals(val, parsed);
+ System.out.println("Sucessfully converted value - " + val);
+ }
+
+ @Test
+ public void randomIntToBytesAndBack() {
+ for (int i = 0; i < 5; i++) {
+ final Random rnd = new Random();
+ final int val = rnd.nextInt();
+ final byte[] bytes = COPSMsgParser.intToBytes(val);
+ final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
+ Assert.assertEquals(val, parsed);
+ System.out.println("Sucessfully converted value - " + val);
+ }
+ }
+
@Test
public void testBytesToShortPCMMClientType() {
final byte[] outBytes = COPSMsgParser.shortToBytes(IPCMMClient.CLIENT_TYPE);