1 5 package ve.luz.ica.remoteio; 6 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 import org.omg.CORBA.UserException ; 15 import org.omg.PortableServer.POA ; 16 17 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 30 private FileUtil() 31 { 32 } 33 34 42 public static String getRemoteFile(RemoteFile remoteFile, String targetDir, String fileName) 43 throws IOException 44 { 45 try 46 { 47 File localFile = new File (targetDir, fileName); 48 49 if (LOG.isDebugEnabled()) 50 { 51 LOG.debug("copying " + fileName); 52 } 53 54 RemoteInputStream ris = remoteFile.getInputStream(); 55 InputStream in = new RemoteInputStreamAdapter(ris); 56 FileOutputStream out = new FileOutputStream (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 (e.getMessage()); 75 } 76 } 77 78 86 public static String getFile(InputStream in, String targetDir, String fileName) 87 throws IOException 88 { 89 File localFile = new File (targetDir, fileName); 90 FileOutputStream out = new FileOutputStream (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 113 public static RemoteFile createRemoteFile(String fileName, POA poa) throws UserException 114 { 115 RemoteFileImpl remoteFileImpl = new RemoteFileImpl(fileName, poa); 116 org.omg.CORBA.Object obj = poa.servant_to_reference(remoteFileImpl); 117 return RemoteFileHelper.narrow(obj); 118 } 119 120 } 121 | Popular Tags |