summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/utils/Tasks.java
blob: f3defd94afc1077d5c6bb88966d15f07daf6843c (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anjocaido.groupmanager.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.data.Group;

/**
 *
 * @author gabrielcouto
 */
public abstract class Tasks {

    public static void copy(InputStream src, File dst) throws IOException {
        InputStream in = src;
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        try {
            in.close();
        } catch (Exception e) {
        }
    }

    public static void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        copy(in, dst);
    }

    public static void removeOldFiles(GroupManager gm, File folder) {
        if (folder.isDirectory()) {
            long oldTime = System.currentTimeMillis() - (((long)gm.getGMConfig().getBackupDuration()*60*60)*1000);
            for (File olds : folder.listFiles()) {
                if (olds.isFile()) {
                    if (olds.lastModified() < oldTime) {
                        try {
                            olds.delete();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
    }

    public static String getDateString() {
        GregorianCalendar now = new GregorianCalendar();
        String date = "";
        date += now.get(Calendar.DAY_OF_MONTH);
        date += "-";
        date += now.get(Calendar.HOUR);
        date += "-";
        date += now.get(Calendar.MINUTE);
        return date;
    }

    public static String getStringListInString(List<String> list) {
        if (list == null) {
            return "";
        }
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            result += list.get(i);
            if (i < list.size() - 1) {
                result += ", ";
            }
        }
        return result;
    }

    public static String getStringArrayInString(String[] list) {
        if (list == null) {
            return "";
        }
        String result = "";
        for (int i = 0; i < list.length; i++) {
            result += list[i];
            if (i < ((list.length) - 1)) {
                result += ", ";
            }
        }
        return result;
    }

    public static String getGroupListInString(List<Group> list) {
        if (list == null) {
            return "";
        }
        String result = "";
        for (int i = 0; i < list.size(); i++) {
            result += list.get(i).getName();
            if (i < list.size() - 1) {
                result += ", ";
            }
        }
        return result;
    }
    
    public static String join(String[] arr, String separator) {
    	if (arr.length == 0)
            return "";
    	String out = arr[0].toString();
    	for (int i = 1; i < arr.length; i++)
            out += separator + arr[i];
    return out;
    }

}