/Users/lyon/j4p/src/futils/SerialUtils.java

1    package futils; 
2     
3    import java.io.*; 
4    import java.util.zip.GZIPInputStream; 
5    import java.util.zip.GZIPOutputStream; 
6     
7    public class SerialUtils { 
8        // futils.SerializeTest 
9        public static void testReadGzipObject() { 
10           try { 
11               System.out.println(readGzippedObject()); 
12           } catch (IOException e) { 
13               e.printStackTrace(); 
14           } catch (ClassNotFoundException e) { 
15               e.printStackTrace(); 
16           } 
17       } 
18    
19       public static Object readGzippedObject() 
20               throws 
21               IOException, 
22               FileNotFoundException, 
23               ClassNotFoundException { 
24           ObjectInputStream ois 
25                   = new ObjectInputStream(new GZIPInputStream(new FileInputStream(Futil.getReadFile("select an object")))); 
26           return ois.readObject(); 
27    
28       } 
29    
30       public static Object readObjectUncompressed() 
31               throws 
32               IOException, 
33               FileNotFoundException, 
34               ClassNotFoundException { 
35           FileInputStream fis 
36                   = new FileInputStream(Futil.getReadFile("select an object")); 
37           ObjectInputStream ois 
38                   = new ObjectInputStream(fis); 
39           Object o = ois.readObject(); 
40           fis.close(); 
41           return o; 
42       } 
43    
44       public static void saveGzipObject(Object o) 
45               throws IOException { 
46           FileOutputStream fos 
47                   = new FileOutputStream(Futil.getWriteFile("select an output file for the object")); 
48           GZIPOutputStream gos 
49                   = new GZIPOutputStream(fos); 
50           ObjectOutputStream oos 
51                   = new ObjectOutputStream(gos); 
52           oos.writeObject(o); 
53           oos.close(); 
54           gos.finish(); 
55       } 
56    
57       public static void saveReadGzipObjectTest() { 
58           try { 
59               SerialUtils.saveGzipObject("this is a gzipped record"); 
60           } catch (IOException e) { 
61               e.printStackTrace(); 
62           } 
63           testReadGzipObject(); 
64       } 
65    
66       public static void testSaveGzipString() { 
67           try { 
68               writeGzipString("this is a test of myn" 
69                       + "gzipper string thing!"); 
70           } catch (IOException e) { 
71               e.printStackTrace(); 
72           } 
73       } 
74    
75       public static void main(String[] args) { 
76           testReadWriteGzipString(); 
77       } 
78    
79       public static void testReadWriteGzipString() { 
80           try { 
81               writeGzipString("this is a test of a string in gzip"); 
82               System.out.println(readGzipString()); 
83           } catch (IOException e) { 
84               e.printStackTrace(); 
85           } 
86    
87       } 
88    
89       public static String readGzipString() 
90               throws IOException { 
91           File readFile = Futil.getReadFile( 
92                   "select an input file for the string"); 
93           FileInputStream fis 
94                   = new FileInputStream(readFile); 
95           GZIPInputStream gis 
96                   = new GZIPInputStream(fis); 
97           System.out.println("gis.available="+gis.available()); 
98           StringBuffer sb = new StringBuffer(); 
99           byte b[] = new byte[1]; 
100          while (gis.read(b)!= -1){ 
101             sb.append( new String(b)); 
102          }; 
103          fis.close(); 
104          return sb.toString(); 
105      } 
106   
107      public static void writeGzipString(String s) 
108              throws IOException { 
109          FileOutputStream fos 
110                  = new FileOutputStream(Futil.getWriteFile("select an output file for the object")); 
111          GZIPOutputStream gos 
112                  = new GZIPOutputStream(fos); 
113          byte b[] = s.getBytes(); 
114          gos.write(b); 
115          gos.finish(); 
116          gos.close(); 
117          fos.close(); 
118      } 
119   
120      public static void saveObjectUncompressed(Object o) 
121              throws IOException { 
122          FileOutputStream fos 
123                  = new FileOutputStream(Futil.getWriteFile("select an output file for the object")); 
124          ObjectOutputStream oos 
125                  = new ObjectOutputStream(fos); 
126          oos.writeObject(o); 
127          oos.close(); 
128      } 
129   
130      public static void print(Customer c[]) { 
131          for (int i = 0; i < c.length; i++) 
132              c[i].print(); 
133      } 
134   
135      public static void testTheTest() { 
136          try { 
137              regressionTest(); 
138              writeGzipString("this is a test of a compressed string"); 
139          } catch (IOException e) { 
140              e.printStackTrace(); 
141          } catch (ClassNotFoundException e) { 
142              e.printStackTrace(); 
143   
144          } 
145      } 
146   
147      private static void regressionTest() 
148              throws IOException, ClassNotFoundException { 
149          testUncompressedSave(); 
150          testCompressedSave(); 
151      } 
152   
153      private static void testUncompressedSave() 
154              throws IOException, ClassNotFoundException { 
155          Customer c1[] = new Customer[100]; 
156          for (int i = 0; i < 100; i++) { 
157              c1[i] = new Customer(); 
158              c1[i].setName("cust#" + i); 
159          } 
160   
161          System.out.println("Object out="); 
162          SerialUtils.saveObjectUncompressed(c1); 
163          System.out.println("Object in="); 
164          Object o = 
165                  SerialUtils.readObjectUncompressed(); 
166          if (o instanceof Customer[]) { 
167              Customer c[] = 
168                      (Customer[]) o; 
169              print(c); 
170          } 
171      } 
172   
173      private static void testCompressedSave() 
174              throws IOException, ClassNotFoundException { 
175          Customer c1[] = new Customer[100]; 
176          for (int i = 0; i < 100; i++) { 
177              c1[i] = new Customer(); 
178              c1[i].setName("cust#" + i); 
179          } 
180   
181          System.out.println("Object out="); 
182          saveGzipObject(c1); 
183          System.out.println("Object in="); 
184          Object o = 
185                  readGzippedObject(); 
186          if (o instanceof Customer[]) { 
187              Customer c[] = 
188                      (Customer[]) o; 
189              print(c); 
190          } 
191      } 
192  }