Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import prime_tass.connect.client_api.ConnectionClientAPI;
- import prime_tass.connect.client_api.interf.INetworkStatusInterface;
- import prime_tass.connect.client_api.interf.ISimpleNetworkInterface;
- import java.io.*;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- /**
- * Created by prog03 on 24.05.2016.
- */
- public class TestSerializ {
- public static Map<String,byte[]> DecompressZipObjectsToMap(byte[] bmInputData)throws Exception
- {
- Map<String,byte[]> m = new LinkedHashMap<String,byte[]>();
- try {
- ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream (bmInputData));
- ZipEntry ze;
- while((ze = zis.getNextEntry()) != null)
- {
- byte[] data_tmp = new byte[1024];
- int readed = zis.read(data_tmp);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while (readed != -1)
- {
- baos.write(data_tmp, 0, readed);
- readed = zis.read(data_tmp);
- }
- m.put(ze.getName(),baos.toByteArray());
- zis.closeEntry();
- }
- zis.close();
- return m;
- }
- catch (Exception e) {
- throw new Exception("DecompressZipObjectsToMap: Exception occured="+e.toString());
- }
- }
- /**
- * РАЗ-экранирование символов используемых в качестве разделителей
- * @param bm byte[]
- * @return byte[]
- */
- public static byte[] unShieldData(byte[] bm,int iSkipBytes)
- {
- try
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ByteArrayInputStream bais = new ByteArrayInputStream(bm);
- int sumb = bais.read();
- while(iSkipBytes--!=0)sumb = bais.read();
- boolean slash = false;
- while (sumb != -1)
- {
- switch (sumb)
- {
- case (92):
- {
- if (slash)
- {
- slash = false;
- baos.write(92);
- }
- else slash = true;
- break;
- }
- case (110):
- {
- if (slash)
- {
- slash = false;
- baos.write(13);
- }
- else baos.write(110);
- break;
- }
- case (114):
- {
- if (slash)
- {
- slash = false;
- baos.write(10);
- }
- else baos.write(114);
- break;
- }
- case (116):
- {
- if (slash)
- {
- slash = false;
- baos.write(9);
- }
- else baos.write(116);
- break;
- }
- default:
- {
- baos.write(sumb);
- }
- }
- sumb = bais.read();
- }
- baos.close();
- bais.close();
- return baos.toByteArray();
- }
- catch (Exception e)
- {
- return null;
- }
- }
- public static void main(String[] args)throws Exception
- {
- INetworkStatusInterface interf1 = new INetworkStatusInterface()
- {
- public void connectionEstablished()
- {
- System.out.println("Connection Established");
- }
- public void networkActivity()
- {
- // System.out.println("Network Activity");
- }
- public void serverRefusedConnection(String reason)
- {
- System.out.println("server Refused Connection "+reason);
- }
- public void wrongCredentials()
- {
- System.out.println("Wrong credentials");
- }
- public void disconnectedFromServer()
- {
- System.out.println("Disconnected From Server");
- }
- public void serverAcceptedCredentials()
- {
- System.out.println("serverAcceptedCredentials");
- }
- };
- ConnectionClientAPI cc = new ConnectionClientAPI(interf1);
- cc.assignNewConnectCredentials("user","123","localhost", ConnectionClientAPI.TYPE.INFO,6020,30000,30000,100);
- ISimpleNetworkInterface handler = new ISimpleNetworkInterface()
- {
- boolean data;
- public byte[][] getCommands()
- {
- NewsAlertImpl newsAlert=new NewsAlertImpl();
- /**
- * Sending 2 requests to server
- */
- byte[][] ret = new byte[1][];
- ret[0]= ("SerializCommand Two 1"+"\r").getBytes();
- if (data==true){
- ret=null;
- }
- return ret;
- }
- public void reciveAnswers(byte[][] datas)
- {
- NewsAlertImpl newsAlert;
- System.out.println("recived answer_1");
- data=true;
- //System.out.println("recived answer_2="+new String(datas[1]));
- byte[] x=datas[0];
- try {
- Map <String,byte[]> map=DecompressZipObjectsToMap(x);
- byte []z=map.get("example");
- byte []y=unShieldData(z,0);
- ByteArrayInputStream bis = new ByteArrayInputStream(y);
- ObjectInput in = null;
- try {
- in = new ObjectInputStream(bis);
- newsAlert = (NewsAlertImpl) in.readObject();
- System.out.println(newsAlert.getName());
- } finally {
- try {
- bis.close();
- } catch (IOException ex) {
- // ignore close exception
- }
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException ex) {
- // ignore close exception
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- cc.assignSink(handler);
- Thread.sleep(4000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement