MDMCK10

Java Guacamole string encoder/decoder

Oct 22nd, 2020
1,258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. class GuacUtil
  4. {
  5.     static String EncodeGuac(String... cypher)
  6.     {
  7.         StringBuilder command = new StringBuilder();
  8.         for (int i = 0; i < cypher.length; i++)
  9.         {
  10.             String current = cypher[i];
  11.             command.append(current.length());
  12.             command.append('.');
  13.             command.append(current);
  14.             command.append(i < cypher.length - 1 ? ',' : ';');
  15.         }
  16.  
  17.         return command.toString();
  18.     }
  19.  
  20.     static String[] DecodeGuac(String str)
  21.     {
  22.         int pos = -1;
  23.         ArrayList<String> sections = new ArrayList<>();
  24.  
  25.         for (;;)
  26.         {
  27.             int len = str.indexOf('.', pos + 1);
  28.             if (len == -1)
  29.             {
  30.                 break;
  31.             }
  32.  
  33.             pos = Integer.parseInt(str.substring(pos + 1, pos + 1 + len - (pos + 1))) + len + 1;
  34.             String repl = str.substring(len + 1, len + 1 + pos - (len + 1));
  35.             repl = repl.replaceAll("&#x27;", "'").replaceAll("&quot;", "\"").replaceAll("&#x2F;", "/").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
  36.             sections.add(repl);
  37.  
  38.             if (str.substring(pos, pos + 1).equals(";"))
  39.             {
  40.                 break;
  41.             }
  42.         }
  43.  
  44.         return sections.toArray(new String[0]);
  45.     }
  46. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×