This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperUtils.java
76 lines (75 loc) · 1.88 KB
/
HelperUtils.java
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
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class HelperUtils
{
static List<String> findAllMatches (String regexStr, String text)
{
List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile(regexStr).matcher(text);
while(m.find()) allMatches.add(m.group());
return allMatches;
}
static String getWebpage (String url)
{
URL targetUrl;
String inputLine;
String webPage = "";
try
{
targetUrl = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(targetUrl.openStream()));
while((inputLine = in.readLine()) != null) webPage += inputLine;
}
catch(MalformedURLException ex)
{
//DOTO: error catching
return "";
}
catch(IOException ex)
{
//DOTO: error catching
return "";
}
return webPage;
}
static void createDirectory(String relDir)
{
try
{
Files.createDirectories(Paths.get(relDir));
}
catch(IOException ex)
{
//DOTO: error catching
return;
}
}
static void downloadSong(String file, String url)
{
try
{
ReadableByteChannel rbc = Channels.newChannel((new URL(url)).openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
catch(IOException ex)
{
//DOTO: error catching
return;
}
}
}