correct way to create a packetHandler class

In my game every packet type has a enum value assigned to it (so when packets come in between the server and the client I can tell what the packet is meant for e.g. is it a login packet, or a ping packet). My server currently has a class to handle the packets that the server gets sent:

public class PacketHandler {
	
	public static void HandlePacket(byte[] data, InetAddress ipAddress, int port){
		String message = PacketUtils.GetDataFromByteStream(data);
		Packets type = PacketUtils.GetPacketIdFromByteStream(data);
		
		switch(type) {
			case INVALID:
                                //do whatever if we get an invalid packet
				break;
			case ALIVE: 
                                //do whatever if we get an alive packet
				break;
			case DISCONNECT:
                                //do whatever if we get an disconnect packet
				break;
			default:
				break;
		}
	}

in the switch statement we will perform whatever action we need to depending on what packet we recieve. My problem is this is really going to be hard to maintain. Is there a better and cleaner way of doing this. Having all of this code in one file is going to make this file really big.

Any inputs into the matter would be amazing. I really want to know what are some cleaner ways of doing this expecally from a OOPS stand point.

You could use a HashMap. Register your detail packet handler classes in the map and your HandlePacket method just execute their doAction method.
Something along those lines:


Map<Packet, DetailPacketHandler> handlerMap =  new HashMap<Packet, DetailPacketHandler>();

public PacketHandler(){
  handlerMap.put(INVALID, new InvalidPacketHandler());
  handlerMap.put(ALIVE, new AlivePacketHandler());
  handlerMap.put(new DisconnectedPacketHandler());
}

public void HandlePacket(byte[] data, InetAddress ipAddress, int port){
  String message = PacketUtils.GetDataFromByteStream(data);
  Packets type = PacketUtils.GetPacketIdFromByteStream(data);
  
  DetailPacketHandler currentHandler = (DetailPacketHandler) handlerMap.get(type);
  if (currentHandler != null) {
    currentHandler.doAction(message, ipAddress, port);
  } else {
    // fallback in here
  }
}

At least I manage my game states that way