1 //Copyright (C) 2004, Brian Enigma <enigma at netninja.com>
2 //This file is part of MagicCodes.
3 //
4 //MagicCodes is free software; you can redistribute it and/or modify
5 //it under the terms of the GNU General Public License as published by
6 //the Free Software Foundation; either version 2 of the License, or
7 //(at your option) any later version.
8 //
9 //MagicCodes is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 //GNU General Public License for more details.
13 //
14 //You should have received a copy of the GNU General Public License
15 //along with Foobar; if not, write to the Free Software
16 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 package org.ninjasoft.magiccodes.plugins;
18
19 import java.io.*;
20
21 /***
22 * Plugin to analyze letter frequency
23 * @author enigma
24 */
25 public class AnalyzeFrequency implements Plugin {
26 public String getName() {
27 return "Analyze Frequency";
28 }
29 public String getDescription() {
30 return "Analyzes Letter Frequency";
31 }
32 public boolean usesKey() {
33 return false;
34 }
35 public boolean isInformational() {
36 return true;
37 }
38 public int[] doAction(int[] in, int[] key) {
39 int frequencies[] = new int[256];
40 int max = 0;
41 ByteArrayOutputStream out = new ByteArrayOutputStream();
42 // Zero out everything (not REALLY necessary in Java, but still good practice)
43 for (int i=0; i<frequencies.length; i++)
44 frequencies[0] = 0;
45 // Count everything
46 for (int i=0; i<in.length; i++) {
47 int letter = in[i] % 256;
48 max = Math.max(max, ++frequencies[letter]);
49 }
50 // Get the results
51 try{
52 out.write("Letter frequencies:\n".getBytes());
53 // Yeay for Bubble Sort/Search...
54 for (int i = max; i> 0; i--) {
55 for (int j = 0; j < frequencies.length; j++) {
56 if (frequencies[j] == i) {
57 String hexCode = Integer.toString(j, 16);
58 if (hexCode.length() == 1)
59 hexCode = "0" + hexCode;
60 out.write("Character 0x".getBytes());
61 out.write(hexCode.getBytes());
62 out.write(" (ASCII ".getBytes());
63 if ((j >= 0x20) && (j <= 0x7E)) {
64 out.write("\"".getBytes());
65 out.write((char) j);
66 out.write("\") ".getBytes());
67 }else{
68 out.write("unprintable) ".getBytes());
69 }
70 out.write(Integer.toString(frequencies[j]).getBytes());
71 out.write("\n".getBytes());
72 }
73 }
74 }
75 }catch(IOException e){
76 e.printStackTrace();
77 }
78 // Convert to result
79 int[] result = new int[out.size()];
80 byte[] data = out.toByteArray();
81 for (int i=0; i<data.length; i++)
82 result[i] = data[i];
83 return result;
84 }
85 }
This page was automatically generated by Maven