1 package nmp.io; 2 3 import java.io.*; 4 5 /*** 6 * @version 0.045 12-Dec-2000 7 * @author Nuno Pregui´┐Ża 8 */ 9 public class Utilities 10 { 11 public final static int BUFFER_SIZE = 1024; 12 public final static byte[] globbuf = new byte[BUFFER_SIZE]; 13 /*** 14 * Produces filename 15 *@param path Pathname 16 *@param filename Filename 17 */ 18 public static String produceFullPath( String path, String filename) 19 { 20 char ch = java.io.File.separatorChar; 21 if( path.charAt( path.length() - 1) == ch) 22 { 23 if( filename.length() == 0 || filename.charAt( 0) != ch) 24 return path + filename; 25 else 26 return path.substring( 0, path.length() - 2) + filename; 27 } 28 else 29 { 30 if( filename.length() == 0 || filename.charAt( 0) != ch) 31 return path + String.valueOf( ch) + filename; 32 else 33 return path + filename; 34 } 35 } 36 37 /*** 38 * Returns the serialized form of the given object 39 */ 40 public static byte []serialize( Object obj) throws IOException { 41 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 42 ObjectOutputStream oos = new ObjectOutputStream( bos); 43 oos.writeObject( obj); 44 oos.flush(); 45 oos.close(); 46 bos.flush(); 47 bos.close(); 48 return bos.toByteArray(); 49 } 50 /*** 51 * Returns the given object 52 */ 53 public static Object unserialize( byte []arr) throws IOException { 54 try { 55 ByteArrayInputStream bis = new ByteArrayInputStream( arr); 56 ObjectInputStream ois = new ObjectInputStream( bis); 57 Object obj = ois.readObject(); 58 ois.close(); 59 bis.close(); 60 return obj; 61 } catch( ClassNotFoundException e) { 62 throw new IOException( "Class not found exception in unserialize"); 63 } 64 } 65 66 /*** 67 * Cleanups outpurstream and associated file - used to delete partially downloaded 68 * resources 69 */ 70 public static void cleanupCon( File f, OutputStream out) { 71 if( out != null) 72 try { 73 out.close(); 74 } catch( Exception e0) { 75 // do nothing 76 } 77 if( f != null) 78 f.delete(); 79 } 80 81 /*** 82 * Dumps in to out 83 */ 84 public static void copy( InputStream in, OutputStream out) throws IOException { 85 byte []buffer = new byte[BUFFER_SIZE]; 86 87 while( true) { 88 int len = in.read( buffer, 0, BUFFER_SIZE); 89 if( len <= 0) 90 break; 91 out.write( buffer, 0, len); 92 } 93 } 94 95 /*** 96 * Load the contents of the file f into buf (appending) 97 * 98 * @return Returns true if the file has been successfully read 99 */ 100 public static boolean loadText( File f, StringBuffer buf) throws IOException { 101 if( buf == null) 102 return false; 103 104 BufferedReader reader = new BufferedReader( new FileReader( f)); 105 char []buffer = new char[BUFFER_SIZE]; 106 107 while( true) { 108 int len = reader.read( buffer, 0, BUFFER_SIZE); 109 if( len <= 0) 110 break; 111 buf.append( buffer, 0, len); 112 } 113 reader.close(); 114 return true; 115 } 116 117 /*** 118 * Save the contents of buf in file f 119 * 120 * @return Returns true if the file has been successfully saved 121 */ 122 public static boolean saveText( File f, String buf) throws IOException { 123 if( buf == null) 124 return false; 125 if( f == null) 126 return false; 127 128 BufferedWriter writer = new BufferedWriter( new FileWriter( f)); 129 char []buffer = new char[BUFFER_SIZE]; 130 131 int len = buf.length(); 132 int curpos = 0; 133 134 while( curpos < len) { 135 int nextpos = curpos + BUFFER_SIZE; 136 if( nextpos > len) 137 nextpos = len; 138 buf.getChars( curpos, nextpos, buffer, 0); 139 writer.write( buffer, 0, nextpos - curpos); 140 curpos = nextpos; 141 } 142 writer.flush(); 143 writer.close(); 144 return true; 145 } 146 147 /*** 148 * Opens object output stream for appending 149 */ 150 public static ObjectOutputStream openAppend( File f) throws IOException, ClassNotFoundException { 151 if( ! f.exists()) 152 return new ObjectOutputStream( new FileOutputStream( f)); 153 File faux; 154 for( int i = 0; ; ) { 155 faux = new File( f.getAbsolutePath() + "." + i); 156 if( ! faux.exists()) 157 break; 158 } 159 if( ! f.renameTo( faux)) 160 throw new IOException( "Unable to rename"); 161 ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( faux))); 162 ObjectOutputStream oos = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream( f))); 163 for( ; ; ) { 164 Object obj = ois.readObject(); 165 if( obj == null) 166 break; 167 oos.writeObject( obj); 168 } 169 ois.close(); 170 return oos; 171 } 172 173 /*** 174 * Merges all file with name " prefix | int | suffix " into file filename 175 * 176 * @param delete If true original files are deleted 177 */ 178 public static void merge( int start, String filename, String prefix, String sufix, boolean delete) throws IOException { 179 File f = new File( filename); 180 BufferedOutputStream bos = null; 181 if( f.exists()) 182 bos = new BufferedOutputStream( new FileOutputStream( filename, true)); 183 else 184 bos = new BufferedOutputStream( new FileOutputStream( filename)); 185 for( int i = start; ; i++) { 186 File f2 = new File( prefix + i + sufix); 187 if( ! f2.exists()) 188 break; 189 BufferedInputStream bis = new BufferedInputStream( new FileInputStream( f2)); 190 copy( bis, bos); 191 bis.close(); 192 if( delete) 193 f2.delete(); 194 } 195 bos.flush(); 196 bos.close(); 197 } 198 199 /*** 200 * Copies files from src to dst 201 */ 202 public static void copyFile( String src, String dst) throws IOException{ 203 copyFile( new File(src), new File(dst)); 204 } 205 public static void copyFile( File src, File dst) throws IOException{ 206 InputStream is = new FileInputStream( src); 207 OutputStream os = new FileOutputStream( dst); 208 for( ; ; ) { 209 int count = is.read( globbuf); 210 if( count <= 0) 211 break; 212 os.write( globbuf, 0, count); 213 } 214 os.flush(); 215 os.close(); 216 is.close(); 217 } 218 /*** 219 * clean dir 220 */ 221 public static void cleanDir( String dir) { 222 File f = new File( dir); 223 if( ! f.isDirectory()) 224 return; 225 File[] fs = f.listFiles(); 226 if( fs != null) { 227 for( int i = 0; i < fs.length; i++) { 228 fs[i].delete(); 229 } 230 } 231 } 232 }

This page was automatically generated by Maven