EMMA Coverage Report (generated Wed Jun 28 22:15:27 PDT 2006)
[all classes][org.apache.derby.iapi.services.io]

COVERAGE SUMMARY FOR SOURCE FILE [FileUtil.java]

nameclass, %method, %block, %line, %
FileUtil.java100% (1/1)53%  (10/19)51%  (356/693)49%  (100.2/205)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class FileUtil100% (1/1)53%  (10/19)51%  (356/693)49%  (100.2/205)
FileUtil (): void 0%   (0/1)0%   (0/3)0%   (0/1)
copyDirectory (File, File): boolean 0%   (0/1)0%   (0/8)0%   (0/1)
copyDirectory (File, File, byte [], String []): boolean 0%   (0/1)0%   (0/97)0%   (0/27)
copyDirectory (StorageFactory, StorageFile, File): boolean 0%   (0/1)0%   (0/7)0%   (0/1)
copyDirectory (StorageFactory, StorageFile, File, byte [], String []): boolean 100% (1/1)86%  (84/98)74%  (20/27)
copyDirectory (String, String): boolean 0%   (0/1)0%   (0/10)0%   (0/1)
copyDirectory (WritableStorageFactory, File, StorageFile): boolean 100% (1/1)100% (7/7)100% (1/1)
copyDirectory (WritableStorageFactory, File, StorageFile, byte [], String [])... 100% (1/1)70%  (68/97)63%  (17/27)
copyFile (File, File): boolean 0%   (0/1)0%   (0/6)0%   (0/1)
copyFile (File, File, byte []): boolean 0%   (0/1)0%   (0/75)0%   (0/26)
copyFile (StorageFactory, StorageFile, File): boolean 100% (1/1)100% (7/7)100% (1/1)
copyFile (StorageFactory, StorageFile, File, byte []): boolean 100% (1/1)75%  (55/73)75%  (19.6/26)
copyFile (WritableStorageFactory, File, StorageFile): boolean 100% (1/1)100% (7/7)100% (1/1)
copyFile (WritableStorageFactory, File, StorageFile, byte []): boolean 100% (1/1)76%  (56/74)75%  (19.6/26)
getAbsoluteFile (File, String): File 0%   (0/1)0%   (0/20)0%   (0/6)
getInputStream (String, int): InputStream 100% (1/1)53%  (18/34)58%  (7/12)
newFile (File, String): File 100% (1/1)100% (13/13)100% (3/3)
removeDirectory (File): boolean 100% (1/1)80%  (41/51)69%  (11/16)
removeDirectory (String): boolean 0%   (0/1)0%   (0/6)0%   (0/1)

1/*
2 
3   Derby - Class org.apache.derby.iapi.services.io.FileUtil
4 
5   Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11      http://www.apache.org/licenses/LICENSE-2.0
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 */
20 
21package org.apache.derby.iapi.services.io;
22 
23import org.apache.derby.io.StorageFactory;
24import org.apache.derby.io.WritableStorageFactory;
25import org.apache.derby.io.StorageFile;
26 
27import java.io.*;
28import java.net.*;
29 
30/**
31        A set of public static methods for dealing with File objects.
32*/
33public abstract class FileUtil {
34 
35    private static final int BUFFER_SIZE = 4096*4;
36        /**
37                Remove a directory and all of its contents.
38 
39                The results of executing File.delete() on a File object
40                that represents a directory seems to be platform
41                dependent. This method removes the directory
42                and all of its contents.
43 
44                @return true if the complete directory was removed, false if it could not be.
45                If false is returned then some of the files in the directory may have been removed.
46 
47        */
48        public static boolean removeDirectory(File directory) {
49 
50                // System.out.println("removeDirectory " + directory);
51 
52                if (directory == null)
53                        return false;
54                if (!directory.exists())
55                        return true;
56                if (!directory.isDirectory())
57                        return false;
58 
59                String[] list = directory.list();
60 
61                // Some JVMs return null for File.list() when the
62                // directory is empty.
63                if (list != null) {
64                        for (int i = 0; i < list.length; i++) {
65                                File entry = new File(directory, list[i]);
66 
67                                //                                System.out.println("\tremoving entry " + entry);
68 
69                                if (entry.isDirectory())
70                                {
71                                        if (!removeDirectory(entry))
72                                                return false;
73                                }
74                                else
75                                {
76                                        if (!entry.delete())
77                                                return false;
78                                }
79                        }
80                }
81 
82                return directory.delete();
83        }
84 
85        public static boolean removeDirectory(String directory)
86        {
87            return removeDirectory(new File(directory));
88        }
89 
90        /**
91          Copy a directory and all of its contents.
92          */
93        public static boolean copyDirectory(File from, File to)
94        {
95                return copyDirectory(from, to, (byte[])null, (String[])null);
96        }
97 
98        public static boolean copyDirectory(String from, String to)
99        {
100                return copyDirectory(new File(from), new File(to));
101        }
102 
103        /**
104                @param filter - array of names to not copy.
105        */
106        public static boolean copyDirectory(File from, File to, byte[] buffer, 
107                                                                                String[] filter)
108        {
109                //
110                // System.out.println("copyDirectory("+from+","+to+")");                
111 
112                if (from == null)
113                        return false;
114                if (!from.exists())
115                        return true;
116                if (!from.isDirectory())
117                        return false;
118 
119                if (to.exists())
120                {
121                        //                        System.out.println(to + " exists");
122                        return false;
123                }
124                if (!to.mkdirs())
125                {
126                        //                        System.out.println("can't make" + to);
127                        return false;
128                }                        
129 
130                String[] list = from.list();
131 
132                // Some JVMs return null for File.list() when the
133                // directory is empty.
134                if (list != null) {
135 
136                        if (buffer == null)
137                                buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
138 
139nextFile:        for (int i = 0; i < list.length; i++) {
140 
141                                String fileName = list[i];
142 
143                                if (filter != null) {
144                                        for (int j = 0; j < filter.length; j++) {
145                                                if (fileName.equals(filter[j]))
146                                                        continue nextFile;
147                                        }
148                                }
149 
150 
151                                File entry = new File(from, fileName);
152 
153                                //                                System.out.println("\tcopying entry " + entry);
154 
155                                if (entry.isDirectory())
156                                {
157                                        if (!copyDirectory(entry,new File(to,fileName),buffer,filter))
158                                                return false;
159                                }
160                                else
161                                {
162                                        if (!copyFile(entry,new File(to,fileName),buffer))
163                                                return false;
164                                }
165                        }
166                }
167                return true;
168        }                
169 
170        public static boolean copyFile(File from, File to)
171        {
172                return copyFile(from, to, (byte[])null);
173        }
174 
175        public static boolean copyFile(File from, File to, byte[] buf)
176        {
177                if (buf == null)
178                        buf = new byte[BUFFER_SIZE];
179 
180                //
181                //                System.out.println("Copy file ("+from+","+to+")");
182                FileInputStream from_s = null;
183                FileOutputStream to_s = null;
184 
185                try {
186                        from_s = new FileInputStream(from);
187                        to_s = new FileOutputStream(to);
188 
189                        for (int bytesRead = from_s.read(buf);
190                                 bytesRead != -1;
191                                 bytesRead = from_s.read(buf))
192                                to_s.write(buf,0,bytesRead);
193 
194                        from_s.close();
195                        from_s = null;
196 
197                        to_s.getFD().sync();  // RESOLVE: sync or no sync?
198                        to_s.close();
199                        to_s = null;
200                }
201                catch (IOException ioe)
202                {
203                        return false;
204                }
205                finally
206                {
207                        if (from_s != null)
208                        {
209                                try { from_s.close(); }
210                                catch (IOException ioe) {}
211                        }
212                        if (to_s != null)
213                        {
214                                try { to_s.close(); }
215                                catch (IOException ioe) {}
216                        }
217                }
218 
219                return true;
220        }
221 
222    public static boolean copyDirectory( StorageFactory storageFactory,
223                                         StorageFile from,
224                                         File to)
225    {
226        return copyDirectory( storageFactory, from, to, null, null, true);
227    }
228    
229 
230    public static boolean copyDirectory( StorageFactory storageFactory,
231                                         StorageFile from,
232                                         File to,
233                                         byte[] buffer,
234                                         String[] filter, 
235                                         boolean copySubDirs)
236    {
237                if (from == null)
238                        return false;
239                if (!from.exists())
240                        return true;
241                if (!from.isDirectory())
242                        return false;
243 
244                if (to.exists())
245                {
246                        //                        System.out.println(to + " exists");
247                        return false;
248                }
249                if (!to.mkdirs())
250                {
251                        //                        System.out.println("can't make" + to);
252                        return false;
253                }                        
254 
255                String[] list = from.list();
256 
257                // Some JVMs return null for File.list() when the
258                // directory is empty.
259                if (list != null)
260        {
261                        if (buffer == null)
262                                buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
263 
264          nextFile:
265            for (int i = 0; i < list.length; i++)
266            {
267                                String fileName = list[i];
268 
269                                if (filter != null) {
270                                        for (int j = 0; j < filter.length; j++) {
271                                                if (fileName.equals(filter[j]))
272                                                        continue nextFile;
273                                        }
274                                }
275 
276                                StorageFile entry = storageFactory.newStorageFile(from, fileName);
277 
278                                if (entry.isDirectory())
279                                {
280                    if(copySubDirs) {
281                        if (!copyDirectory( storageFactory, entry, 
282                                            new File(to,fileName), buffer, 
283                                            filter, copySubDirs))
284                            return false;
285                    }
286                    else {
287                        // the request is to not copy the directories, continue
288                        // to the next file in the list.
289                        continue nextFile;
290                    }
291 
292                                }
293                                else
294                                {
295                                        if (!copyFile( storageFactory, entry, new File(to,fileName), buffer))
296                                                return false;
297                                }
298                        }
299                }
300                return true;
301        } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
302 
303    public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to)
304    {
305        return copyFile( storageFactory, from, to, (byte[]) null);
306    }
307    
308        public static boolean copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf)
309        {
310                InputStream from_s = null;
311                FileOutputStream to_s = null;
312 
313                try {
314                        from_s = from.getInputStream();
315                        to_s = new FileOutputStream( to);
316 
317                        if (buf == null)
318                                buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
319 
320                        for (int bytesRead = from_s.read(buf);
321                                 bytesRead != -1;
322                                 bytesRead = from_s.read(buf))
323                                to_s.write(buf,0,bytesRead);
324 
325                        from_s.close();
326                        from_s = null;
327 
328                        to_s.getFD().sync();  // RESOLVE: sync or no sync?
329                        to_s.close();
330                        to_s = null;
331                }
332                catch (IOException ioe)
333                {
334                        return false;
335                }
336                finally
337                {
338                        if (from_s != null)
339                        {
340                                try { from_s.close(); }
341                                catch (IOException ioe) {}
342                        }
343                        if (to_s != null)
344                        {
345                                try { to_s.close(); }
346                                catch (IOException ioe) {}
347                        }
348                }
349 
350                return true;
351        } // end of copyFile( StorageFactory storageFactory, StorageFile from, File to, byte[] buf)
352 
353    public static boolean copyDirectory( WritableStorageFactory storageFactory,
354                                         File from,
355                                         StorageFile to)
356    {
357        return copyDirectory( storageFactory, from, to, null, null);
358    }
359    
360    public static boolean copyDirectory( WritableStorageFactory storageFactory,
361                                         File from,
362                                         StorageFile to,
363                                         byte[] buffer,
364                                         String[] filter)
365    {
366                if (from == null)
367                        return false;
368                if (!from.exists())
369                        return true;
370                if (!from.isDirectory())
371                        return false;
372 
373                if (to.exists())
374                {
375                        //                        System.out.println(to + " exists");
376                        return false;
377                }
378                if (!to.mkdirs())
379                {
380                        //                        System.out.println("can't make" + to);
381                        return false;
382                }                        
383 
384                String[] list = from.list();
385 
386                // Some JVMs return null for File.list() when the
387                // directory is empty.
388                if (list != null)
389        {
390                        if (buffer == null)
391                                buffer = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
392 
393          nextFile:
394            for (int i = 0; i < list.length; i++)
395            {
396                                String fileName = list[i];
397 
398                                if (filter != null) {
399                                        for (int j = 0; j < filter.length; j++) {
400                                                if (fileName.equals(filter[j]))
401                                                        continue nextFile;
402                                        }
403                                }
404 
405                                File entry = new File(from, fileName);
406 
407                                if (entry.isDirectory())
408                                {
409                                        if (!copyDirectory( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer, filter))
410                                                return false;
411                                }
412                                else
413                                {
414                                        if (!copyFile( storageFactory, entry, storageFactory.newStorageFile(to,fileName), buffer))
415                                                return false;
416                                }
417                        }
418                }
419                return true;
420        } // end of copyDirectory( StorageFactory sf, StorageFile from, File to, byte[] buf, String[] filter)
421 
422    public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to)
423    {
424        return copyFile( storageFactory, from, to, (byte[]) null);
425    }
426    
427        public static boolean copyFile( WritableStorageFactory storageFactory, File from, StorageFile to, byte[] buf)
428        {
429                InputStream from_s = null;
430                OutputStream to_s = null;
431 
432                try {
433                        from_s = new FileInputStream( from);
434                        to_s = to.getOutputStream();
435 
436                        if (buf == null)
437                                buf = new byte[BUFFER_SIZE]; // reuse this buffer to copy files
438 
439                        for (int bytesRead = from_s.read(buf);
440                                 bytesRead != -1;
441                                 bytesRead = from_s.read(buf))
442                                to_s.write(buf,0,bytesRead);
443 
444                        from_s.close();
445                        from_s = null;
446 
447                        storageFactory.sync( to_s, false);  // RESOLVE: sync or no sync?
448                        to_s.close();
449                        to_s = null;
450                }
451                catch (IOException ioe)
452                {
453                        return false;
454                }
455                finally
456                {
457                        if (from_s != null)
458                        {
459                                try { from_s.close(); }
460                                catch (IOException ioe) {}
461                        }
462                        if (to_s != null)
463                        {
464                                try { to_s.close(); }
465                                catch (IOException ioe) {}
466                        }
467                }
468 
469                return true;
470        } // end of copyFile
471 
472        /**
473                Convert a file path into a File object with an absolute path
474                relative to a passed in root. If path is absolute then
475                a file object constructed from new File(path) is returned,
476                otherwise a file object is returned from new File(root, path)
477                if root is not null, otherwise null is returned.
478        */
479        public static File getAbsoluteFile(File root, String path) {
480                File file = new File(path);
481                if (file.isAbsolute())
482                        return file;
483 
484                if (root == null)
485                        return null;
486 
487                return new File(root, path);
488        }
489 
490        /**
491                A replacement for new File(File, String) that correctly implements
492                the case when the first argument is null. The documentation for java.io.File
493                says that new File((File) null, name) is the same as new File(name).
494                This is not the case in pre 1.1.8 vms, a NullPointerException is thrown instead.
495        */
496        public static File newFile(File parent, String name) {
497 
498                if (parent == null)
499                        return new File(name);
500                else
501                        return new File(parent, name);
502        }
503 
504        /**
505         * Open an input stream to read a file or a URL
506         * @param fileOrURL        The file or URL to open.
507         * @param bufferSize 0 => no buffering.
508         * @return        an InputStream
509         * @exception StandardException        Thrown on failure
510         */
511        public static InputStream getInputStream(String fileOrURL,int bufferSize)
512                 throws IOException
513        {
514                InputStream is;
515                try {
516                        is = new FileInputStream( fileOrURL );
517                }
518 
519                catch (FileNotFoundException fnfe){
520                        try {
521                                is = new URL( fileOrURL ).openStream();
522                        } catch (MalformedURLException mfurle) {
523 
524                                // if it looks like an url throw this exception
525                                // otherwise throw the file not found exception
526                                // If there is no : or an early colon then it's
527                                // probably a file (e.g. /foo/myjar.jar or a:/foo/myjar.jar)
528                                if (fileOrURL.indexOf(':') > 2)
529                                        throw mfurle;
530                                throw fnfe;
531                        }
532                }
533                if (bufferSize > 0)
534                        is = new BufferedInputStream(is,bufferSize);
535 
536                return is;
537        }
538}

[all classes][org.apache.derby.iapi.services.io]
EMMA 2.0.5312 (C) Vladimir Roubtsov