SHARE
TWEET

Untitled

a guest May 20th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.ifmo.rain.dulaev.hello;
  2.  
  3. import java.net.DatagramPacket;
  4. import java.net.InetSocketAddress;
  5. import java.net.SocketAddress;
  6. import java.nio.charset.StandardCharsets;
  7.  
  8. public class PacketUtils {
  9.  
  10.     public static String encodeMessage(String prefix, int threadId, int requestId) {
  11.         return prefix + threadId + "_" + requestId;
  12.     }
  13.  
  14.     public static String decodeMessage(final DatagramPacket packet) {
  15.         return new String(packet.getData(), packet.getOffset(), packet.getLength(), StandardCharsets.UTF_8);
  16.     }
  17.  
  18.     public static DatagramPacket makeMessagePacket(final SocketAddress address, String message) {
  19.         byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
  20.         return new DatagramPacket(messageBytes, messageBytes.length, address);
  21.     }
  22.  
  23.     @Deprecated
  24.     public static SocketAddress getUnresolvedSocketAddress(final DatagramPacket packet) {
  25.         return InetSocketAddress.createUnresolved(String.valueOf(packet.getAddress()), packet.getPort());
  26.     }
  27.  
  28.     public static void fillMessage(final DatagramPacket packet, String message) {
  29.         packet.setData(message.getBytes(StandardCharsets.UTF_8));
  30.     }
  31.  
  32.     public static DatagramPacket newEmptyPacket(int receiveBufferSize) {
  33.         return new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
  34.     }
  35.  
  36.     public static DatagramPacket newEmptyPacket(final SocketAddress address, int receiveBufferSize) {
  37.         return new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize, address);
  38.     }
  39.  
  40.     public static void resetAndResize(final DatagramPacket packet, int receiveBufferSize) {
  41.         packet.setData(new byte[receiveBufferSize]);
  42.     }
  43.  
  44.     public static boolean checkValidResponse(String requestMessage, String responseMessage) {
  45.         return responseMessage.contains(requestMessage);
  46.     }
  47.  
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top