Source code: com/flexstor/common/io/xfile/unc/UncCopy.java
1 /*
2 * UncCopy.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:39 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.io.xfile.unc;
12
13 import java.io.BufferedOutputStream;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18
19 import com.flexstor.common.errorlogger.FlexError;
20 import com.flexstor.common.io.xfile.XFileCopy;
21 import com.flexstor.common.io.xfile.XFileCopyI;
22 import com.flexstor.common.util.UrlAnalyzer;
23
24 /**
25 * UncCopy copy files from the local file system
26 **/
27 public class UncCopy
28 implements XFileCopyI
29 {
30 // To get the version number from MKS
31 public final static String IDENTIFIER="$Id: UncCopy.java,v 1.2 2003/08/11 02:22:39 aleric Exp $";
32 private String sThisService = "UncCopy";
33
34 /**
35 * Connects to host using the credentials specified.
36 * Returns true if connection was sucessful; false otherwise.
37 */
38 public boolean connect( String sHost, String sUser, String sPassword )
39 {
40 return true;
41 }
42
43 /**
44 * Disconnects from host.
45 */
46 public void disconnect()
47 {}
48
49 public boolean copy( String sSourceFile, String sTargetFile )
50 {
51 // If the source file is not the same as the target
52 // file go ahead and do the copy, otherwise there
53 // is nothing to do.
54 if ( !sSourceFile.equals(sTargetFile) )
55 {
56 if ( checkDestination( sTargetFile ) )
57 {
58 try { return XFileCopy.copyFile( new FileInputStream(sSourceFile), new FileOutputStream(sTargetFile) ); }
59 catch ( FileNotFoundException fnfe )
60 {
61 fnfe.printStackTrace();
62 return false;
63 }
64 catch ( IOException ioe )
65 {
66 // In JDK1.1.8 the constructor for FileOutputStream throws a IOException instead of
67 // FileNotFoundException; we need to place this catch block here for compatibility
68 // issues with the java client and admin tool.
69 ioe.printStackTrace();
70 return false;
71 }
72 }
73 else
74 return false; // destination does not exist and cannot be created
75 }
76 else
77 return true; // source and destination are the same
78 }
79
80 public boolean copyToDir(String sSource, String sTargetDir)
81 {
82 char cSeparator = UrlAnalyzer.getSeparatorChar(sTargetDir);
83 String sTargetFile = sTargetDir;
84 if ( !sTargetFile.endsWith( String.valueOf(cSeparator) ) )
85 sTargetFile += cSeparator;
86
87 sTargetFile += UrlAnalyzer.getFileName(sSource);
88
89 return copy(sSource, sTargetFile);
90 }
91
92 public boolean copyBytes( byte[] byteArray, String destinationFile )
93 {
94 if ( checkDestination( destinationFile ) )
95 {
96 try
97 {
98 BufferedOutputStream outStream = new BufferedOutputStream( new FileOutputStream(destinationFile) );
99 outStream.write(byteArray);
100 outStream.flush();
101 outStream.close();
102 System.out.println("Success.. file copied");
103 return true;
104 }
105 catch(Exception ex)
106 {
107 System.out.println("Exception occured while writing to an FTP url " + ex);
108 ex.printStackTrace();
109 return false;
110 }
111 }
112 else
113 return false;
114 }
115
116 private boolean checkDestination( String destination )
117 {
118 UncFile destDir = new UncFile( (new UncFile(destination)).getParent() );
119
120 if ( destDir.exists() )
121 return true;
122
123 if ( destDir.mkdirs() == false )
124 {
125 //5501=Failure creating output directory path: %%1
126 new FlexError(FlexError.CRITICAL, sThisService, IDENTIFIER, 5501, new String[] { destDir.getAbsolutePath() });
127 return false;
128 }
129 return true;
130 }
131 }