예제 #1
0
  @Override
  protected byte[] getBytes() {
    final List<Byte> byteList =
        new ArrayList<>(Bytes.asList(COPSMsgParser.shortToBytes(protocol.getValue())));
    byteList.add(tosOverwrite);
    byteList.add(tosMask);
    byteList.addAll(Bytes.asList(srcAddress.getAddress()));
    byteList.addAll(Bytes.asList(dstAddress.getAddress()));
    byteList.addAll(Bytes.asList(COPSMsgParser.shortToBytes(srcPort)));
    byteList.addAll(Bytes.asList(COPSMsgParser.shortToBytes(dstPort)));
    byteList.add(priority);

    // reserved padding
    byteList.addAll(Bytes.asList((byte) 0, (byte) 0, (byte) 0));

    return Bytes.toArray(byteList);
  }
예제 #2
0
  /**
   * Returns a Classifier object from a byte array
   *
   * @param data - the data to parse
   * @return - the object or null if cannot be parsed TODO - make me more robust as exceptions can
   *     be swallowed here.
   */
  public static Classifier parse(final byte[] data) {
    final List<Byte> bytes = Bytes.asList(data);

    try {
      final byte[] srcAddrBytes = Bytes.toArray(bytes.subList(4, 8));
      final byte[] dstAddrBytes = Bytes.toArray(bytes.subList(8, 12));
      return new Classifier(
          Protocol.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
          data[2],
          data[3],
          (Inet4Address) InetAddress.getByAddress(srcAddrBytes),
          (Inet4Address) InetAddress.getByAddress(dstAddrBytes),
          COPSMsgParser.bytesToShort(data[12], data[13]),
          COPSMsgParser.bytesToShort(data[14], data[15]),
          data[16]);
    } catch (UnknownHostException e) {
      return null;
    }
  }