/** * Convert a string representation of an ethernet address into a byte * array. * * @param hex A hex string which represents an ethernet address. * @return A byte array that contains 6 octets if a valid hex string is * passed. {@code null} is returned if {@code null} is passed. * @throws IllegalArgumentException * The given string is not a string representation of an ethernet * address. */ public static byte[] toBytes(String hex) { byte[] bytes = ByteUtils.toBytes(hex); if (bytes != null) { checkAddress(bytes); } return bytes; }
/** * Convert the given hex string with ":" inserted into a byte array. * * <ul> * <li>{@code null} is returned if {@code null} is passed.</li> * <li>An empty byte array is returned if an empty string is passed.</li> * </ul> * * @param hex A hex string with ":" inserted. * @return A byte array. * @throws NumberFormatException * The given string is not a hex string. */ public static byte[] toBytes(String hex) { if (hex == null) { return null; } if (hex.isEmpty()) { return new byte[0]; } String[] octets = hex.split(HEX_SEPARATOR); byte[] bytes = new byte[octets.length]; for (int i = 0; i < octets.length; i++) { bytes[i] = (byte)parseHexOctet(octets[i]); } return bytes; }
/** * Create an {@link InetAddress} instance which represents the IP address * specified by the given byte array. * * @param bytes A byte array which represents the IP address. * @return An {@link InetAddress} instance. * @throws IllegalArgumentException * The given byte array does not represent an IP network mask. */ public static final InetAddress getInetAddress(byte[] bytes) { try { return InetAddress.getByAddress(bytes); } catch (Exception e) { throw new IllegalArgumentException( "Invalid raw IP address: " + ByteUtils.toHexString(bytes), e); } }
int nbits = getFieldNumBits(field, hent); try { ByteUtils.setBits(packetBytes, fieldBytes, off, nbits); } catch (RuntimeException e) { String msg = "Failed to serialize field: " + field + ByteUtils.toHexString(packetBytes));
hdrFieldBytes = ByteUtils.getBits(data, startOffset, numBits); } catch (RuntimeException e) { String msg = "Failed to deserialize field: " + hdrField + LOG.trace("Deserializing: {}: {}: {} (offset {} bitsize {})", getClass().getSimpleName(), hdrField, ByteUtils.toHexString(hdrFieldBytes), startOffset, numBits);
/** * {@inheritDoc} */ @Override protected void postSerializeCustomOperation(byte[] myBytes) throws PacketException { String field = CHECKSUM; try { HeaderField entry = HEADER_FORMAT.get(field); int off = getFieldOffset(field, entry); int nbits = getFieldNumBits(field, entry); byte[] cksum = toBytes(computeChecksum(myBytes, 0)); ByteUtils.setBits(myBytes, cksum, off, nbits); getHeaderFieldMap().put(CHECKSUM, cksum); } catch (RuntimeException e) { throw new PacketException("Failed to update checksum.", e); } }
/** * Parse a string representation of Ethernet address. * * @param str A string representation of Ethernet address. * @return An {@code EthernetAddress} object. * @throws BadRequestException * Invalid string is passed to {@code str}. */ protected final EthernetAddress parseEthernetAddress(String str) { try { byte[] b = ByteUtils.toBytes(str); return new EthernetAddress(b); } catch (Exception e) { throw new BadRequestException("Invalid MAC address."); } }
/** * Return a string representation of this object. * * @return A string representation of this object. */ @Override public final String toString() { StringBuilder builder = new StringBuilder(). append(getClass().getSimpleName()).append('{'); String sep = ""; for (String field: getHeaderFormat().keySet()) { byte[] value = hdrFieldsMap.get(field); builder.append(sep).append(field).append('='). append(ByteUtils.toHexString(value)); sep = ", "; } return builder.append('}').toString(); }
if (c == ByteUtils.HEX_SEPARATOR_CHAR) { count++; int octet = ByteUtils.parseHexOctet(builder.toString()); value = (value << Byte.SIZE) | (long)octet; builder = new StringBuilder(); int octet = ByteUtils.parseHexOctet(builder.toString()); return (value << Byte.SIZE) | (long)octet;
/** * Return a {@link MacAddress} instance which represents this ethernet * address. * * @return A {@link MacAddress} instance. */ public MacAddress getMacAddress() { MacAddress mac = macAddress; if (mac == null) { byte[] bytes = getBytesImpl(); String hex = ByteUtils.toHexString(bytes); mac = new MacAddress(hex); macAddress = mac; } return mac; }