summaryrefslogtreecommitdiffstats
path: root/juju/juju-vnfmadapter/Juju-vnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/gvnfm/jujuvnfmadapter/common/FileUtils.java
blob: 357f2c545bc40a3b9e8572e524d4dc38b9ebf432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * FileUtils
 * <br/>
 * <p>
 * </p>
 * 
 * @author quanzhong@huawei.com
 * @version NFVO 0.5 Sep 23, 2016
 */
public class FileUtils {

    public static final String UTF8 = "UTF-8";

    public static final String GBK = "GBK";

    private static Logger log = LoggerFactory.getLogger(FileUtils.class);

    private FileUtils(){
        
    }
    /**
     * get current classPath as str. and the trailing slash will be remove
     * <br/>
     * 
     * @return
     * @since  NFVO 0.5
     */
    public static String getClassPath(){
        String path = ClassLoader.getSystemClassLoader().getResource("./").getPath();
        if(path.endsWith("/")){
            path = path.substring(0, path.length()-1);
        }
        return path;
    }
    /**
     * read data from the file
     * <br/>
     * 
     * @param file
     * @param charsetName
     * @return
     * @throws IOException
     * @since  NFVO 0.5
     */
    public static byte[] readFile(File file, String charsetName) throws IOException {
	    if(file != null) {

		    try( FileReader reader = new FileReader(file)){
			    StringBuilder buffer = new StringBuilder();
			    char[] cbuf = new char[1024];
			    int legth;
			    while((legth = reader.read(cbuf)) != -1) {
				    buffer.append(new String(cbuf, 0, legth));
			    }
			    reader.close();
			    return buffer.toString().getBytes(charsetName);
		    }catch(IOException e){
			log.error("read file", e);
			throw e;
		}
	    }
	    return new byte[]{};
    }

    /**
     * write data as file to the filePath
     * <br/>
     * 
     * @param data
     * @param filePath
     * @return
     * @since  NFVO 0.5
     */
    public static int writeFile(byte[] data, String filePath) {
        try( 
            OutputStream out = new FileOutputStream(filePath)){
            out.write(data, 0, data.length);
            out.close();
            return 0;
        } catch (Exception e) {
            log.error("write file fail,filePath:"+filePath, e);
        }
        return -1;
    }
    
    /**
     * List all files in directory
     * @param file
     * @return
     * @throws FileNotFoundException 
     */
    public static  List<File> listFiles(File file) throws FileNotFoundException{
        List<File> list = new ArrayList<>();
        list(file, list);
        return list;
    }
    /**
     * List all files in directory
     * @param file
     * @param list
     * @throws FileNotFoundException 
     */
    private static void list(File file, List<File> list) throws FileNotFoundException{
        if(!file.exists()){
            throw new FileNotFoundException(file.getAbsolutePath());
        }
        File[] files = file.listFiles();
        if(files != null && files.length >  0 ){
            for(File f : files){
                if(f.isFile()){
                    list.add(f);
                }else{
                    list(f, list);
                }
            }
        }
    }
    
    
    /**
     * createDirs (cycle create)
     * <br/>
     * 
     * @param file
     * @since  NFVO 0.5
     */
    public static void createDirs(File file){
        if(file.getParentFile().exists()){
            file.mkdir();
        }else{
            createDirs(file.getParentFile());
            file.mkdir();
        }
    }
    
    /**
     *mkDirs 
     * <br/>
     * 
     * @param path
     * @since  NFVO 0.5
     */
    public static void mkDirs(String path){
        File file = new File(path);
        createDirs(file);
    }
    
    
    
    public static String getSuperUrl(String file) {
        return new File(file).getParentFile().getAbsolutePath();

    }

    /**
     * absolute url
     * 
     * @author sunny.sun
     * @return file this application absolute url
     */
    public static String getAppAbsoluteUrl() {

        // ��ȡ��class���ڵľ���·��
        String file = UnCompressUtil.class.getClassLoader().getResource("/") == null ? null
                : UnCompressUtil.class.getClassLoader().getResource("/").toString();
        if (file == null) {
            file = UnCompressUtil.class.getProtectionDomain().getCodeSource()
                    .getLocation().getFile().substring(1);
        }
        // ��class�ļ���war��ʱ������"zip:D:/ ..."����·��
        if (file.startsWith("zip")) {
            file = file.substring(4);

            // ��class�ļ���class�ļ���ʱ������"file:/F:/ ..."����·��
        } else if (file.startsWith("file")) {
            file = file.substring(6);

            // ��class�ļ���jar�ļ���ʱ������"jar:file:/F:/ ..."����·��
        } else if (file.startsWith("jar")) {
            file = file.substring(10);
        }

        if (!isWindows())
            return ("/" + file).replace("%20", " ");

        return file.replace("%20", " ");
    }

    public static String getWEBClassAbsoluteUrl() {

        return getAppAbsoluteUrl();
    }

    /**
     * @param floder
     * @return
     */
    public static File newFloder(String floder) {
        File file = new File(floder);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }



    public static void copyFile(String oldPath, String newPath, boolean flag)
	    throws IOException {
		    int byteread = 0;
		    File oldfile = new File(oldPath);
		    if (oldfile.exists()) {
			    if (flag == false) {
				    delFiles(newPath);
			    }
			    if (new File(newPath).exists() && flag == true) {
				    return;
			    }
			    newFile(newPath);
			    try(FileInputStream fis = new FileInputStream(oldPath);
					    FileOutputStream fos = new FileOutputStream(newPath)){
				    byte[] buffer = new byte[1024];
				    while ((byteread = fis.read(buffer)) != -1) {
					    fos.write(buffer, 0, byteread);
				    }
			    }
			    //fos.close();
			    //fis.close();
		    } else {
			    throw new FileNotFoundException("the " + oldfile + " is not exits ");
		    }

	    }

    /**
     * @param filePathAndName
     *            String exp c:/fqf.txt
     * @param fileContent
     *            String
     * @return boolean
     */
    public static boolean delFiles(String filePathAndName) {

        boolean flag = false;
        File myDelFile = new File(filePathAndName);
        if (!myDelFile.exists())
            return true;
        if (myDelFile.isDirectory()) {
            File[] fs = myDelFile.listFiles();
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isFile())
                    flag = fs[i].delete();
                if (fs[i].isDirectory()) {
                    flag = delFiles(fs[i].getAbsolutePath());
                    flag = fs[i].delete();
                }
            }
        }
        flag = myDelFile.delete();
        return flag;
    }

    /**
     * create new file
     * 
     * @param filePathAndName
     *            String
     * @return
     * @throws IOException
     */
    public static File newFile(String fileName) throws IOException {
        File file = new File(fileName);

        newFloder(file.getParentFile().toString());

        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    /**
     * the file down all the hidden files
     * 
     * @author sunny.sun
     * */
    public static List<File> getFiles(String path) {

        List<File> list = new ArrayList<File>();
        File file = new File(path);
        if (!file.exists()) {

            file.mkdirs();
        }

        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile() && !files[i].isHidden()) {
                list.add(files[i]);
            }
            if (files[i].isDirectory() && !files[i].isHidden()) {
                List<File> list2 = getFiles(files[i].getPath());// use myself
                list.addAll(list2);
            }
        }
        return list;
    }

    /**
     * causes the os of this computer is Windows
     * 
     * @author sunny.sun
     * */
    public static boolean isWindows() {

        String os = System.getProperty("os.name").toLowerCase();
        // windows
        return (os.indexOf("win") >= 0);

    }

    public static void copy(String oldfile, String newfile, boolean flag)
            throws IOException {
        File oldf = new File(oldfile);
        File newf = new File(newfile);
        boolean oisd = (oldfile.endsWith("/") || oldfile.endsWith("\\"));
        boolean nisd = (newfile.endsWith("/") || newfile.endsWith("\\"));

        if (!oldf.exists()) {
		 throw new FileNotFoundException("the  from data is not exists");
        }
        if (oldf.exists() && !newf.exists()) {
            if (newfile.endsWith("/") || newfile.endsWith("\\")) {
                newFloder(newfile);
            } else {
                newFile(newfile);
            }
        }
        if (oldf.exists() && oisd && !nisd) {
		 throw new FileNotFoundException("the  from data is directory,but the to data is a file");
        }
        if (!oisd && !nisd) {
            copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath(), flag);
        }
        if (oisd && nisd) {
            newFloder(newf.getAbsolutePath());
            List<File> list = getFiles(oldf.getAbsolutePath());
            for (int i = 0; i < list.size(); i++) {
                copyFile(list.get(i).getAbsolutePath(), newf.getAbsolutePath()
                        + "/" + list.get(i).getName(), flag);
            }
        }
        if (!oisd && nisd) {
            newFloder(newf.getAbsolutePath());
            copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath() + "/"
                    + oldf.getName(), flag);
        }

    }

    /**
     * causes the os of this computer is mac
     * 
     * @author sunny.sun
     * */
    public static boolean isMac() {

        String os = System.getProperty("os.name").toLowerCase();
        // Mac
        return (os.indexOf("mac") >= 0);

    }

    /**
     * causes the os of this computer is unix
     * 
     * @author sunny.sun
     * */
    public static boolean isUnix() {

        String os = System.getProperty("os.name").toLowerCase();
        // linux or unix
        return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);

    }

    /**
     * @param file
     * @return
     * @throws Exception
     */
    public static boolean isUsed(String file) throws IOException {
        File f = new File(file);
        if (!f.exists()) {
            throw new FileNotFoundException("the file is not exists ..");
        }
        File f1 = new File(file + ".temp");
        f.renameTo(f1);
        if (f.exists()) {
            return true;
        } else {
            f1.renameTo(f);
            return false;
        }

    }
    /**
     * Remove the file name's extension (only remove the last) 
     * <br/>
     * 
     * @param file
     * @return
     * @since  NFVO 0.5
     */
    public static String getBaseFileName(File file){
        if(file.getName().lastIndexOf(".") > 0){
            return file.getName().substring(0,file.getName().lastIndexOf("."));
        }else{
            return file.getName();
        }
       
    }
    
    /**
     * fix file path to linux seperate,and remove the head and end slash
     * <br/>
     * 
     * @param path
     * @return
     * @since  NFVO 0.5
     */
    public static String fixPath(String path){
        String newPath = path;
        if(path == null){
            return newPath;
        }
        newPath = newPath.replaceAll("\\\\", "/");
        if(newPath.startsWith("/")){
            newPath = newPath.substring(1, newPath.length()); 
        }
        if(newPath.endsWith("/")){
            newPath = newPath.substring(0, newPath.length()-1); 
        }
        return newPath;
    }
    
    /**
     * fix file path to linux seperate,and add the head and end slash
     * <br/>
     * 
     * @param path
     * @return
     * @since  NFVO 0.5
     */
    public static String getFriendlyPath(String path){
        String newPath = path;
        if(path == null){
            return newPath;
        }
        newPath = newPath.replaceAll("\\\\", "/");
        if(!newPath.startsWith("/")){
            newPath = "/"+newPath;
        }
        if(!newPath.endsWith("/")){
            newPath = newPath + "/";
        }
        return newPath;
    }
}