code: @SuppressWarnings("unused")
      public static void dumpPacket(DatagramPacket packet, String prefix) {
          byte[] buffer = new byte[packet.getLength()];
          System.arraycopy(packet.getData(), packet.getOffset(), buffer, 0, packet.getLength());
          printBuffer(buffer, "Buffer to save");
          try {
              Path path = getNextPath(prefix);
              logger.info("Dumping buffer to {}", path);
              Files.write(path, buffer);
          } catch (IOException e) {
              logger.error("Error writing file: {}", e.getLocalizedMessage());
          }
      }
docs: Save @packet to a new file beginning with @prefix. Append a sequential suffix to ensure we don't
  overwrite existing files.
  @param packet The data packet to dump to disk
  @param prefix The start of the file name