{"action":"opened","issue":{"url":"https://api.github.com/repos/YaserCuw/LZWtool/issues/1","labels_url":"https://api.github.com/repos/YaserCuw/LZWtool/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/YaserCuw/LZWtool/issues/1/comments","events_url":"https://api.github.com/repos/YaserCuw/LZWtool/issues/1/events","html_url":"https://github.com/YaserCuw/LZWtool/issues/1","id":123380753,"number":1,"title":"Mainactivity_LZW encode/decode tool","user":{"login":"YaserCuw","id":15625390,"avatar_url":"https://avatars.githubusercontent.com/u/15625390?v=3","gravatar_id":"","url":"https://api.github.com/users/YaserCuw","html_url":"https://github.com/YaserCuw","followers_url":"https://api.github.com/users/YaserCuw/followers","following_url":"https://api.github.com/users/YaserCuw/following{/other_user}","gists_url":"https://api.github.com/users/YaserCuw/gists{/gist_id}","starred_url":"https://api.github.com/users/YaserCuw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/YaserCuw/subscriptions","organizations_url":"https://api.github.com/users/YaserCuw/orgs","repos_url":"https://api.github.com/users/YaserCuw/repos","events_url":"https://api.github.com/users/YaserCuw/events{/privacy}","received_events_url":"https://api.github.com/users/YaserCuw/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-21T23:24:25Z","updated_at":"2015-12-21T23:24:25Z","closed_at":null,"body":"package com.example.lzw;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\nimport java.util.List;\r\nimport java.util.Map;\r\n\r\nimport com.example.lzw.R;\r\n\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.view.View;\r\nimport android.view.View.OnClickListener;\r\nimport android.widget.Button;\r\nimport android.widget.EditText;\r\nimport android.widget.TextView;\r\n\r\npublic class MainActivity extends Activity {\r\n\r\n\tprivate EditText etencodeString;\r\n\tprivate EditText etdecodeString;\r\n\tprivate Button btnen;\r\n\tprivate Button btnde;\r\n\r\n\tprivate TextView tvEncryptStr;\r\n\r\n\tList<Integer> encodesData;\r\n\r\n\t@Override\r\n\tprotected void onCreate(Bundle savedInstanceState) {\r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\tsetContentView(R.layout.activity_main);\r\n\r\n\t\tetencodeString = (EditText) findViewById(R.id.editText1);\r\n\t\tetdecodeString = (EditText) findViewById(R.id.editText2);\r\n\r\n\t\tbtnen = (Button) findViewById(R.id.button1);\r\n\t\tbtnde = (Button) findViewById(R.id.button2);\r\n\r\n\t\ttvEncryptStr = (TextView) findViewById(R.id.tv1);\r\n\r\n\t\tbtnen.setOnClickListener(new OnClickListener() {\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View arg0) {\r\n\r\n\t\t\t\tencodesData = encodeString(etencodeString.getText().toString());\r\n\t\t\t\tString str = \"\";\r\n\t\t\t\tfor (int i = 0; i < encodesData.size(); i++) {\r\n\t\t\t\t\tstr += encodesData.get(i);\r\n\t\t\t\t}\r\n\r\n\t\t\t\ttvEncryptStr.setText(str);\r\n\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\tbtnde.setOnClickListener(new OnClickListener() {\r\n\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View v) {\r\n\t\t\t\tetdecodeString.setText(decodeString(encodesData));\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n\r\n\t@Override\r\n\tpublic boolean onCreateOptionsMenu(Menu menu) {\r\n\t\t// Inflate the menu; this adds items to the action bar if it is present.\r\n\t\tgetMenuInflater().inflate(R.menu.activity_main, menu);\r\n\t\treturn true;\r\n\t}\r\n\r\n\tpublic static List<Integer> encodeString(String uncompressed) {\r\n\t\t// Build the dictionary.\r\n\t\tint dictSize = 256;\r\n\t\tMap<String, Integer> dictionary = new HashMap<String, Integer>();\r\n\t\tfor (int i = 0; i < 256; i++)\r\n\t\t\tdictionary.put(\"\" + (char) i, i);\r\n\r\n\t\tString w = \"\";\r\n\t\tList<Integer> result = new ArrayList<Integer>();\r\n\t\tfor (char c : uncompressed.toCharArray()) {\r\n\t\t\tString wc = w + c;\r\n\t\t\tif (dictionary.containsKey(wc))\r\n\t\t\t\tw = wc;\r\n\t\t\telse {\r\n\t\t\t\tresult.add(dictionary.get(w));\r\n\t\t\t\t// Add wc to the dictionary.\r\n\t\t\t\tdictionary.put(wc, dictSize++);\r\n\t\t\t\tw = \"\" + c;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\t// Output the code for w.\r\n\t\tif (!w.equals(\"\"))\r\n\t\t\tresult.add(dictionary.get(w));\r\n\t\treturn result;\r\n\t}\r\n\r\n\tpublic static String decodeString(List<Integer> compressed) {\r\n\t\t// Build the dictionary.\r\n\t\tint dictSize = 256;\r\n\t\tMap<Integer, String> dictionary = new HashMap<Integer, String>();\r\n\t\tfor (int i = 0; i < 256; i++)\r\n\t\t\tdictionary.put(i, \"\" + (char) i);\r\n\r\n\t\tString w = \"\" + (char) (int) compressed.remove(0);\r\n\t\tStringBuffer result = new StringBuffer(w);\r\n\t\tfor (int k : compressed) {\r\n\t\t\tString entry;\r\n\t\t\tif (dictionary.containsKey(k))\r\n\t\t\t\tentry = dictionary.get(k);\r\n\t\t\telse if (k == dictSize)\r\n\t\t\t\tentry = w + w.charAt(0);\r\n\t\t\telse\r\n\t\t\t\tthrow new IllegalArgumentException(\"Bad compressed k: \" + k);\r\n\r\n\t\t\tresult.append(entry);\r\n\r\n\t\t\t// Add w+entry[0] to the dictionary.\r\n\t\t\tdictionary.put(dictSize++, w + entry.charAt(0));\r\n\r\n\t\t\tw = entry;\r\n\t\t}\r\n\t\treturn result.toString();\r\n\t}\r\n\r\n}\r\n"}}