Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.upem.net.udp;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.InetSocketAddress;
- import java.net.SocketAddress;
- import java.nio.ByteBuffer;
- import java.nio.CharBuffer;
- import java.nio.channels.DatagramChannel;
- import java.nio.charset.Charset;
- import java.util.Optional;
- import java.util.Scanner;
- public class ClientBetterUpperCaseUDP {
- public static Charset ASCII_CHARSET = Charset.forName("US-ASCII");
- public static int BUFFER_SIZE = 1024;
- /**
- * Create and return the String message represented by the ByteBuffer buffer in
- * the following representation: - the size (as an Big Indian int) of the
- * charsetName encoding in ASCII<br/>
- * - the bytes encoding this charsetName in ASCII<br/>
- * - the bytes encoding the message in this charsetName.<br/>
- * The accepted ByteBuffer buffer must be in <strong>write mode</strong> (need
- * to be flipped before to be used).
- *
- * @param buffer
- * a ByteBuffer containing the representation of a message
- * @return the String represented by bb or Optional.empty() if the bb is
- * mal-formed
- */
- public static Optional<String> decodeMessage(ByteBuffer buffer) {
- CharBuffer charbuff = CharBuffer.allocate(BUFFER_SIZE);
- buffer.flip();
- int csLength = buffer.getInt();
- ByteBuffer bb = ByteBuffer.allocate(csLength);
- for (int i = 0; i < csLength; i++) {
- bb.put(buffer.get());
- }
- String csString = new String(bb.array(), ASCII_CHARSET);
- Charset cs = Charset.forName(csString);
- charbuff = cs.decode(buffer);
- Optional<String> opt = Optional.of(charbuff.toString());
- return opt;
- }
- /**
- * Create and return a new ByteBuffer containing the representation of msg
- * string using the charset charsetName in the following format. - the size (as
- * an Big Indian int) of the charsetName encoding in ASCII<br/>
- * - the bytes encoding this charsetName in ASCII<br/>
- * - the bytes encoding msg in charsetName.<br/>
- * The returned ByteBuffer is in <strong>write mode</strong> (need to be flipped
- * before to be used).
- *
- * @param msg
- * the String to send
- * @param charsetName
- * name of the Charset to encode the String msg
- * @return a newly allocated ByteBuffer containing the representation of msg
- * @throws UnsupportedEncodingException
- */
- private static ByteBuffer encodeMessage(String msg, String charsetName) throws UnsupportedEncodingException {
- Charset cs = Charset.forName(charsetName);
- ByteBuffer csName = ASCII_CHARSET.encode(charsetName);
- String csNameAscii = new String(csName.array(), ASCII_CHARSET);
- ByteBuffer msgBuffer = cs.encode(msg);
- String msgCharset = new String(msgBuffer.array(), cs);
- ByteBuffer fBuffer = ByteBuffer.allocate(Integer.SIZE + csNameAscii.length() + msgCharset.length());
- fBuffer.putInt(csNameAscii.length());
- fBuffer.put(csName);
- fBuffer.put(msgBuffer);
- return fBuffer;
- }
- public static void usage() {
- System.out.println("Usage : ClientBetterUpperCaseUDP host port charsetName");
- }
- public static void main(String[] args) throws IOException {
- // check and retrieve parameters
- if (args.length != 3) {
- usage();
- return;
- }
- String host = args[0];
- int port = Integer.valueOf(args[1]);
- String charsetName = args[2];
- SocketAddress dest = new InetSocketAddress(host, port);
- // buff to receive messages
- ByteBuffer buff = ByteBuffer.allocateDirect(BUFFER_SIZE);
- try (Scanner scan = new Scanner(System.in); DatagramChannel dc = DatagramChannel.open()) {
- while (scan.hasNextLine()) {
- String line = scan.nextLine();
- ByteBuffer packet = encodeMessage(line, charsetName);
- packet.flip();
- dc.send(packet, dest);
- buff.clear();
- dc.receive(buff);
- Optional<String> res = decodeMessage(buff);
- if (res.isPresent()) {
- System.out.println("Received: " + res.get());
- } else {
- System.out.println("Received an invalid paquet");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment