KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > remoteio > FileUtil


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.remoteio;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.omg.CORBA.UserException JavaDoc;
15 import org.omg.PortableServer.POA JavaDoc;
16
17 /**
18  * A Utilily class for creating remote files
19  * @author Carlos Arévalo
20  */

21 public final class FileUtil
22 {
23     private static final Log LOG = LogFactory.getLog(FileUtil.class);
24
25     private static final int BUFFER_SIZE = 2048;
26
27     /**
28      * Private constructor.
29      */

30     private FileUtil()
31     {
32     }
33
34     /**
35      * Copy the contents of a remote file to a local file
36      * @param remoteFile the remote file to be copied
37      * @param targetDir the directory to which the file will be copied
38      * @param fileName the name used to create the local file
39      * @return the path to the file where the remote file was copied
40      * @throws IOException if there is an IO error
41      */

42     public static String JavaDoc getRemoteFile(RemoteFile remoteFile, String JavaDoc targetDir, String JavaDoc fileName)
43             throws IOException JavaDoc
44     {
45         try
46         {
47             File JavaDoc localFile = new File JavaDoc(targetDir, fileName);
48
49             if (LOG.isDebugEnabled())
50             {
51                 LOG.debug("copying " + fileName);
52             }
53
54             RemoteInputStream ris = remoteFile.getInputStream();
55             InputStream JavaDoc in = new RemoteInputStreamAdapter(ris);
56             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(localFile);
57
58             byte[] b = new byte[BUFFER_SIZE];
59             int len = in.read(b);
60             while (len != -1)
61             {
62                 out.write(b, 0, len);
63                 len = in.read(b);
64             }
65
66             in.close();
67             out.close();
68
69             return localFile.getPath();
70         }
71         catch (RemoteIOException e)
72         {
73             e.printStackTrace();
74             throw new IOException JavaDoc(e.getMessage());
75         }
76     }
77
78     /**
79      * Read the contents of an InputStream and copy it to a file
80      * @param in the input stream to be read
81      * @param targetDir the directory where the new file will be created
82      * @param fileName the name of the file to be created
83      * @return the path of the create file
84      * @throws IOException if there is an IO error
85      */

86     public static String JavaDoc getFile(InputStream JavaDoc in, String JavaDoc targetDir, String JavaDoc fileName)
87             throws IOException JavaDoc
88     {
89         File JavaDoc localFile = new File JavaDoc(targetDir, fileName);
90         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(localFile);
91
92         byte[] b = new byte[BUFFER_SIZE];
93         int len = in.read(b);
94         while (len != -1)
95         {
96             out.write(b, 0, len);
97             len = in.read(b);
98         }
99
100         in.close();
101         out.close();
102
103         return localFile.getPath();
104     }
105
106     /**
107      * Creates a remote file object
108      * @param fileName the name of the local file on which the remote file will be created
109      * @param poa the poa to be used to create the remote file object reference
110      * @return the reference to the newly create remote file
111      * @throws UserException if there is a Corba exception during the creation process
112      */

113     public static RemoteFile createRemoteFile(String JavaDoc fileName, POA JavaDoc poa) throws UserException JavaDoc
114     {
115         RemoteFileImpl remoteFileImpl = new RemoteFileImpl(fileName, poa);
116         org.omg.CORBA.Object JavaDoc obj = poa.servant_to_reference(remoteFileImpl);
117         return RemoteFileHelper.narrow(obj);
118     }
119
120 }
121
Popular Tags