> For the complete documentation index, see [llms.txt](https://pcap.ardikars.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pcap.ardikars.com/packet-codec/using-packet-codec.md).

# Using packet codec

#### Example of working with packet codec.

```java
PacketBuffer packetBuffer = ..;

Ethernet ethernet = packetBuffer.cast(Ethernet.class);
System.out.println(ethernet);
if (ethernet.type() == Ip4.TYPE) {
  Ip4 ip4 = packetBuffer.readerIndex(ethernet.size()).cast(Ip4.class);
  System.out.println(ip4);
  if (ip4.protocol() == Tcp.TYPE) {
    Tcp tcp = packetBuffer.readerIndex(ethernet.size() + ip4.size()).cast(Tcp.class);
    System.out.println(tcp);
  } else if (ip4.protocol() == Udp.TYPE) {
    Udp udp = packetBuffer.readerIndex(ethernet.size() + ip4.size()).cast(Udp.class);
    System.out.println(udp);
  }
} else if (ethernet.type() == Ip6.TYPE) {
  Ip6 ip6 = packetBuffer.readerIndex(ethernet.size()).cast(Ip6.class);
  System.out.println(ip6);
  if (ip6.nextHeader() == Tcp.TYPE) {
    Tcp tcp = packetBuffer.readerIndex(ethernet.size() + ip6.size()).cast(Tcp.class);
    System.out.println(tcp);
  } else if (ip6.nextHeader() == Udp.TYPE) {
    Udp udp = packetBuffer.readerIndex(ethernet.size() + ip6.size()).cast(Udp.class);
    System.out.println(udp);
  }
}
```
