Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: Freenet/client/Zipper.java


1   package Freenet.client;
2   
3   /**
4    * Zips and unzips strings
5    **/
6   public class Zipper
7   {
8       public static void main(String[] args)
9       {
10    if (args.length != 1)
11        {
12      System.out.println("Usage java Freenet.client.Zipper string");
13        }
14    else
15        {
16      String[] oz = unzip(args[0], 2);
17      System.out.println(zip(oz));
18        }
19      }
20      
21      /**
22       * unzips a string into parts parts
23       * @param str The string to unzip
24       * @param parts The number of parts to unzip it into
25       * @return The parts that the string has been unzipped into
26       **/
27  
28      public static String[] unzip(String str, int parts)
29      {
30    StringBuffer[] stra = new StringBuffer[parts];
31    char[] strC = str.toCharArray();
32    for (int x=1; x<strC.length; x++)
33        {
34      strC[x] ^=  strC[x-1];
35        }
36  
37    for (int x=0; x<stra.length; x++)
38    {
39        stra[x] = new StringBuffer(str.length() / (parts));
40    }
41    int pos = 0;
42    boolean fl = false;
43    while(!fl)
44    {
45      for (int x=0; x<stra.length; x++)
46          {
47        try
48            {
49              stra[x].append(strC[pos]);
50            }
51        catch (ArrayIndexOutOfBoundsException e)
52            {
53          fl = true;
54          break;
55            }
56        pos++;
57          }
58    }
59    String[] ret = new String[parts];
60    for (int x=0; x<ret.length; x++)
61    {
62        ret[x] = stra[x].toString();
63    }
64    return ret;
65      }
66  
67      /**
68       * Zips a string back together
69       * @param stra An array of the component strings
70       * @return The complete string
71       **/
72      public static String zip(String[] stra)
73      {
74    char[] ret;
75    int strl = 0;
76    for (int x=0; x<stra.length; x++)
77        {
78      strl += stra[x].length();
79        }
80    ret = new char[strl];
81    boolean fl = false;
82    int posO = 0;
83    int posI = 0;
84    while(!fl)
85        {
86      for (int x=0; x<stra.length; x++)
87          {
88        try
89            {
90          ret[posO] = stra[x].charAt(posI);
91          posO++;
92            }
93        catch (IndexOutOfBoundsException e)
94            {
95          fl = true;
96          break;
97            }
98          }
99      posI++;
100       }
101   for (int x=ret.length-1; x>0; x--)
102       {
103     ret[x] ^= ret[x-1];
104       }
105   return new String(ret);
106     }
107 }
108