/Users/lyon/j4p/src/utils/Example.java

1    package utils; 
2     
3     
4    public class Example { 
5     
6        public static void main(String[] args) { 
7            // Make up some source objects 
8            javax.swing.JLabel originalLabel = new javax.swing.JLabel("Base64 is great."); 
9            byte[] originalBytes = {(byte) -2, (byte) -1, (byte) 0, (byte) 1, (byte) 2}; 
10            
11           // Display original label 
12           System.out.println("Original JLabel: " + originalLabel); 
13            
14           // Encode serialized object 
15           String encLabel = Base64.encodeObject(originalLabel); 
16           String encGZLabel = Base64.encodeObject(originalLabel, Base64.GZIP); 
17           String encGZDontBreakLines = Base64.encodeObject(originalLabel, Base64.GZIP | Base64.DONT_BREAK_LINES); 
18            
19           // Print encoded label 
20           System.out.println("JLabel, encoded ( " + encLabel.getBytes().length + " bytes):\n" + encLabel); 
21           System.out.println("JLabel, gzipped and encoded ( " + encGZLabel.getBytes().length + " bytes):\n" + encGZLabel); 
22           System.out.println("JLabel, gzipped, encoded, no line breaks (not Base 64 compliant) ( " + 
23                   encGZDontBreakLines.getBytes().length + 
24                   " bytes):\n" + 
25                   encGZDontBreakLines); 
26            
27           // Decode label 
28           Object objLabel = Base64.decodeToObject(encLabel); 
29           Object objGZLabel = Base64.decodeToObject(encGZLabel); 
30           Object objGZDontBreakLines = Base64.decodeToObject(encGZDontBreakLines); 
31            
32           // Display decoded label 
33           System.out.println("Encoded JLabel -> decoded: " + objLabel); 
34           System.out.println("Encoded, gzipped JLabel -> decoded: " + objGZLabel); 
35           System.out.println("Encoded, gzipped, no line breaks JLabel -> decoded: " + objGZDontBreakLines); 
36            
37            
38           // Display original array 
39           System.out.println("\n\nOriginal array: "); 
40           for (int i = 0; i < originalBytes.length; i++) 
41               System.out.print(originalBytes[i] + " "); 
42           System.out.println(); 
43            
44           // Encode serialized bytes 
45           String encBytes = Base64.encodeBytes(originalBytes); 
46           String encGZBytes = Base64.encodeBytes(originalBytes, Base64.GZIP); 
47            
48           // Print encoded bytes 
49           System.out.println("Bytes, encoded ( " + encBytes.getBytes().length + " bytes):\n" + encBytes); 
50           System.out.println("Bytes, gzipped and encoded ( " + encGZBytes.getBytes().length + " bytes):\n" + encGZBytes); 
51           
52           // Decode bytes 
53           byte[] decBytes = Base64.decode(encBytes); 
54           byte[] decGZBytes = Base64.decode(encGZBytes); 
55            
56           // Display decoded bytes 
57           System.out.println("Encoded Bytes -> decoded: "); 
58           for (int i = 0; i < decBytes.length; i++) 
59               System.out.print(decBytes[i] + " "); 
60           System.out.println(); 
61           System.out.println("Encoded Bytes, gzipped -> decoded: "); 
62           for (int i = 0; i < decGZBytes.length; i++) 
63               System.out.print(decGZBytes[i] + " "); 
64           System.out.println(); 
65            
66            
67           // Try suspend, resume 
68           // Base64 -> PrintStream -> System.out 
69           { 
70               System.out.println("\n\nSuspend/Resume Base64.OutputStream"); 
71               Base64.OutputStream b64os = null; 
72               java.io.PrintStream ps = null; 
73    
74               try { 
75                   ps = new java.io.PrintStream(System.out); 
76                   b64os = new Base64.OutputStream(ps, Base64.ENCODE); 
77    
78                   b64os.suspendEncoding(); 
79                   b64os.write(new String("<mydata>").getBytes()); 
80    
81                   b64os.resumeEncoding(); 
82                   b64os.write(originalBytes); 
83    
84                   b64os.suspendEncoding(); 
85                   b64os.write(new String("</mydata>\n\n").getBytes()); 
86               }   // end try 
87               catch (java.io.IOException e) { 
88                   e.printStackTrace(); 
89               }   // end catch 
90               finally { 
91    
92                   try { 
93                       b64os.flush(); 
94                   } catch (Exception e) { 
95                   } 
96                   //try{ ps.close();    } catch( Exception e ){} // Closes System.out! 
97               }   // end finally 
98           }   // end suspsend/resume example 
99            
100           
101          // Encode something large to file, gzipped 
102          // ObjectOutput -> GZIP -> Base64 -> Buffer -> File 
103          { 
104              System.out.print("\n\nWriting to file example.gz.txt..."); 
105              java.io.ObjectOutputStream oos = null; 
106              java.util.zip.GZIPOutputStream gzos = null; 
107              Base64.OutputStream b64os = null; 
108              java.io.BufferedOutputStream bos = null; 
109              java.io.FileOutputStream fos = null; 
110   
111              try { 
112                  fos = new java.io.FileOutputStream("example.gz.txt"); 
113                  bos = new java.io.BufferedOutputStream(fos); 
114                  b64os = new Base64.OutputStream(bos, Base64.ENCODE); 
115                  gzos = new java.util.zip.GZIPOutputStream(b64os); 
116                  oos = new java.io.ObjectOutputStream(gzos); 
117   
118                  oos.writeObject(System.getProperties()); 
119              }   // end try 
120              catch (java.io.IOException e) { 
121                  e.printStackTrace(); 
122              }   // end catch 
123              finally { 
124                  try { 
125                      oos.close(); 
126                  } catch (Exception e) { 
127                  } 
128                  try { 
129                      gzos.close(); 
130                  } catch (Exception e) { 
131                  } 
132                  try { 
133                      b64os.close(); 
134                  } catch (Exception e) { 
135                  } 
136                  try { 
137                      bos.close(); 
138                  } catch (Exception e) { 
139                  } 
140                  try { 
141                      fos.close(); 
142                  } catch (Exception e) { 
143                  } 
144                  System.out.println("Done."); 
145              }   // end finally 
146               
147              // Read back in 
148              // File -> Buffer -> Base64 -> GZIP -> Object 
149              System.out.print("\n\nReading from file example.gz.txt..."); 
150              java.io.ObjectInputStream ois = null; 
151              java.util.zip.GZIPInputStream gzis = null; 
152              Base64.InputStream b64is = null; 
153              java.io.BufferedInputStream bis = null; 
154              java.io.FileInputStream fis = null; 
155   
156              try { 
157                  fis = new java.io.FileInputStream("example.gz.txt"); 
158                  bis = new java.io.BufferedInputStream(fis); 
159                  b64is = new Base64.InputStream(bis, Base64.DECODE); 
160                  gzis = new java.util.zip.GZIPInputStream(b64is); 
161                  ois = new java.io.ObjectInputStream(gzis); 
162   
163                  System.out.print(ois.readObject()); 
164              }   // end try 
165              catch (java.io.IOException e) { 
166                  e.printStackTrace(); 
167              }   // end catch 
168              catch (java.lang.ClassNotFoundException e) { 
169                  e.printStackTrace(); 
170              }   // end catch 
171              finally { 
172                  try { 
173                      ois.close(); 
174                  } catch (Exception e) { 
175                  } 
176                  try { 
177                      gzis.close(); 
178                  } catch (Exception e) { 
179                  } 
180                  try { 
181                      b64is.close(); 
182                  } catch (Exception e) { 
183                  } 
184                  try { 
185                      bis.close(); 
186                  } catch (Exception e) { 
187                  } 
188                  try { 
189                      fis.close(); 
190                  } catch (Exception e) { 
191                  } 
192                  System.out.println("Done."); 
193              }   // end finally 
194          }   // end example: large to file, gzipped 
195           
196           
197           
198           
199          // Encode something large to file, NOT gzipped 
200          // ObjectOutput -> Base64 -> Buffer -> File 
201          { 
202              System.out.print("\n\nWriting to file example.txt..."); 
203              java.io.ObjectOutputStream oos = null; 
204              Base64.OutputStream b64os = null; 
205              java.io.BufferedOutputStream bos = null; 
206              java.io.FileOutputStream fos = null; 
207   
208              try { 
209                  fos = new java.io.FileOutputStream("example.txt"); 
210                  bos = new java.io.BufferedOutputStream(fos); 
211                  b64os = new Base64.OutputStream(bos, Base64.ENCODE); 
212                  oos = new java.io.ObjectOutputStream(b64os); 
213   
214                  oos.writeObject(System.getProperties()); 
215              }   // end try 
216              catch (java.io.IOException e) { 
217                  e.printStackTrace(); 
218              }   // end catch 
219              finally { 
220                  try { 
221                      oos.close(); 
222                  } catch (Exception e) { 
223                  } 
224                  try { 
225                      b64os.close(); 
226                  } catch (Exception e) { 
227                  } 
228                  try { 
229                      bos.close(); 
230                  } catch (Exception e) { 
231                  } 
232                  try { 
233                      fos.close(); 
234                  } catch (Exception e) { 
235                  } 
236                  System.out.println("Done."); 
237              }   // end finally 
238          }   // end example: large to file, NOT gzipped 
239   
240   
241          System.out.println("\nExamples completed."); 
242   
243      }   // end main 
244   
245  }   // end class Example 
246