public static PeerMessage parse ( ByteBuffer buffer , TorrentInfo torrent ) throws ParseException { int length = buffer . getInt ( ) ; if ( length == 0 ) { return KeepAliveMessage . parse ( buffer , torrent ) ; } else if ( length != buffer . remaining ( ) ) { throw new ParseException ( "Message size did not match announced " + "size!" , 0 ) ; } Type type = Type . get ( buffer . get ( ) ) ; if ( type == null ) { throw new ParseException ( "Unknown message ID!" , buffer . position ( ) - 1 ) ; } switch ( type ) { case CHOKE : return ChokeMessage . parse ( buffer . slice ( ) , torrent ) ; case UNCHOKE : return UnchokeMessage . parse ( buffer . slice ( ) , torrent ) ; case INTERESTED : return InterestedMessage . parse ( buffer . slice ( ) , torrent ) ; case NOT_INTERESTED : return NotInterestedMessage . parse ( buffer . slice ( ) , torrent ) ; case HAVE : return HaveMessage . parse ( buffer . slice ( ) , torrent ) ; case BITFIELD : return BitfieldMessage . parse ( buffer . slice ( ) , torrent ) ; case REQUEST : return RequestMessage . parse ( buffer . slice ( ) , torrent ) ; case PIECE : return PieceMessage . parse ( buffer . slice ( ) , torrent ) ; case CANCEL : return CancelMessage . parse ( buffer . slice ( ) , torrent ) ; default : throw new IllegalStateException ( "Message type should have " + "been properly defined by now." ) ; } }
Parse the given buffer into a peer protocol message .