Advertisement
Guest User

Untitled

a guest
May 24th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. import prime_tass.connect.client_api.ConnectionClientAPI;
  2. import prime_tass.connect.client_api.interf.INetworkStatusInterface;
  3. import prime_tass.connect.client_api.interf.ISimpleNetworkInterface;
  4.  
  5. import java.io.*;
  6. import java.util.LinkedHashMap;
  7. import java.util.Map;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipInputStream;
  10.  
  11. /**
  12. * Created by prog03 on 24.05.2016.
  13. */
  14. public class TestSerializ {
  15.  
  16. public static Map<String,byte[]> DecompressZipObjectsToMap(byte[] bmInputData)throws Exception
  17. {
  18. Map<String,byte[]> m = new LinkedHashMap<String,byte[]>();
  19. try {
  20. ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream (bmInputData));
  21.  
  22. ZipEntry ze;
  23. while((ze = zis.getNextEntry()) != null)
  24. {
  25. byte[] data_tmp = new byte[1024];
  26. int readed = zis.read(data_tmp);
  27.  
  28. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  29. while (readed != -1)
  30. {
  31. baos.write(data_tmp, 0, readed);
  32. readed = zis.read(data_tmp);
  33. }
  34. m.put(ze.getName(),baos.toByteArray());
  35. zis.closeEntry();
  36. }
  37. zis.close();
  38.  
  39. return m;
  40. }
  41. catch (Exception e) {
  42. throw new Exception("DecompressZipObjectsToMap: Exception occured="+e.toString());
  43. }
  44. }
  45.  
  46.  
  47.  
  48. /**
  49. * РАЗ-экранирование символов используемых в качестве разделителей
  50. * @param bm byte[]
  51. * @return byte[]
  52. */
  53. public static byte[] unShieldData(byte[] bm,int iSkipBytes)
  54. {
  55. try
  56. {
  57. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  58. ByteArrayInputStream bais = new ByteArrayInputStream(bm);
  59. int sumb = bais.read();
  60.  
  61. while(iSkipBytes--!=0)sumb = bais.read();
  62.  
  63. boolean slash = false;
  64. while (sumb != -1)
  65. {
  66. switch (sumb)
  67. {
  68. case (92):
  69. {
  70. if (slash)
  71. {
  72. slash = false;
  73. baos.write(92);
  74. }
  75. else slash = true;
  76. break;
  77. }
  78. case (110):
  79. {
  80. if (slash)
  81. {
  82. slash = false;
  83. baos.write(13);
  84. }
  85. else baos.write(110);
  86. break;
  87. }
  88. case (114):
  89. {
  90. if (slash)
  91. {
  92. slash = false;
  93. baos.write(10);
  94. }
  95. else baos.write(114);
  96. break;
  97. }
  98. case (116):
  99. {
  100. if (slash)
  101. {
  102. slash = false;
  103. baos.write(9);
  104. }
  105. else baos.write(116);
  106. break;
  107. }
  108. default:
  109. {
  110. baos.write(sumb);
  111. }
  112. }
  113. sumb = bais.read();
  114. }
  115. baos.close();
  116. bais.close();
  117. return baos.toByteArray();
  118. }
  119. catch (Exception e)
  120. {
  121. return null;
  122. }
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. public static void main(String[] args)throws Exception
  130. {
  131. INetworkStatusInterface interf1 = new INetworkStatusInterface()
  132. {
  133. public void connectionEstablished()
  134. {
  135. System.out.println("Connection Established");
  136. }
  137.  
  138. public void networkActivity()
  139. {
  140. // System.out.println("Network Activity");
  141. }
  142.  
  143. public void serverRefusedConnection(String reason)
  144. {
  145. System.out.println("server Refused Connection "+reason);
  146. }
  147.  
  148. public void wrongCredentials()
  149. {
  150. System.out.println("Wrong credentials");
  151. }
  152.  
  153. public void disconnectedFromServer()
  154. {
  155. System.out.println("Disconnected From Server");
  156. }
  157.  
  158. public void serverAcceptedCredentials()
  159. {
  160. System.out.println("serverAcceptedCredentials");
  161. }
  162. };
  163.  
  164.  
  165. ConnectionClientAPI cc = new ConnectionClientAPI(interf1);
  166. cc.assignNewConnectCredentials("user","123","localhost", ConnectionClientAPI.TYPE.INFO,6020,30000,30000,100);
  167.  
  168. ISimpleNetworkInterface handler = new ISimpleNetworkInterface()
  169. {
  170. boolean data;
  171. public byte[][] getCommands()
  172. {
  173. NewsAlertImpl newsAlert=new NewsAlertImpl();
  174. /**
  175. * Sending 2 requests to server
  176. */
  177. byte[][] ret = new byte[1][];
  178. ret[0]= ("SerializCommand Two 1"+"\r").getBytes();
  179. if (data==true){
  180. ret=null;
  181. }
  182. return ret;
  183. }
  184.  
  185. public void reciveAnswers(byte[][] datas)
  186. {
  187. NewsAlertImpl newsAlert;
  188. System.out.println("recived answer_1");
  189.  
  190. data=true;
  191. //System.out.println("recived answer_2="+new String(datas[1]));
  192. byte[] x=datas[0];
  193. try {
  194. Map <String,byte[]> map=DecompressZipObjectsToMap(x);
  195. byte []z=map.get("example");
  196. byte []y=unShieldData(z,0);
  197. ByteArrayInputStream bis = new ByteArrayInputStream(y);
  198. ObjectInput in = null;
  199. try {
  200. in = new ObjectInputStream(bis);
  201. newsAlert = (NewsAlertImpl) in.readObject();
  202. System.out.println(newsAlert.getName());
  203.  
  204. } finally {
  205. try {
  206. bis.close();
  207. } catch (IOException ex) {
  208. // ignore close exception
  209. }
  210. try {
  211. if (in != null) {
  212. in.close();
  213. }
  214. } catch (IOException ex) {
  215. // ignore close exception
  216. }
  217. }
  218. } catch (Exception e) {
  219. e.printStackTrace();
  220. }
  221. }
  222.  
  223. };
  224.  
  225. cc.assignSink(handler);
  226.  
  227. Thread.sleep(4000);
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement