-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking7.java
More file actions
121 lines (100 loc) · 3.35 KB
/
Networking7.java
File metadata and controls
121 lines (100 loc) · 3.35 KB
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
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class Networking7 {
static ArrayList<URL> allUrls;
public static int exists(char[] src, String toFind){
char[] srcArr=src;
char[] toFindArr=toFind.toCharArray();
for(int i=0;i<srcArr.length-toFind.length()+1;i++){
boolean found=true;
for(int j=0;j<toFindArr.length;j++){
if(toFindArr[j] != srcArr[i+j]){
found=false;
}
}
if(found){
return i;
}
}
return -1;
}
public static int exists(String src, String toFind){
char[] srcArr=src.toCharArray();
char[] toFindArr=toFind.toCharArray();
for(int i=0;i<srcArr.length-toFind.length()+1;i++){
boolean found=true;
for(int j=0;j<toFindArr.length;j++){
if(toFindArr[j] != srcArr[i+j]){
found=false;
}
}
if(found){
return i;
}
}
return -1;
}
public static String getLink(int i, char[] tempArr){
int linkLength=0;
int i2=i;
while(tempArr[i2]!='\''){
linkLength++;
i2++;
}
char[] link=new char[linkLength];
for(int i3=0;i3<linkLength;i3++){
link[i3]=tempArr[i+i3];
}
return new String(link);
}
public static ArrayList<URL> getLinks(URL relative, URL url){
ArrayList<URL> arrayList=new ArrayList<>();
try{
BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream()));
String temp;
String toFind="<a";
while((temp = in.readLine()) != null) {
int i;
if((i = exists(temp, toFind))!=-1){
char[] tempArr=temp.toCharArray();
i=exists(tempArr, "href=\'")+("href=\'").length();
String link=getLink(i, tempArr);
// System.out.println(link);
arrayList.add(new URL(relative, link));
}
}
}catch (Exception e){
}
return arrayList;
}
static final int MAX_LEVEL=3;
public static void crawl(URLConnection conn, int level) throws Exception{
ArrayList<URL> urls=getLinks(conn.getURL(), conn.getURL());
for(int i=0;i<urls.size();i++){
for(int j=0;j<level-1;j++){
System.out.print("\t");
}
System.out.print(urls.get(i));
System.out.println();
if(level<MAX_LEVEL){
crawl(urls.get(i).openConnection(), ++level);
}
}
}
public static void main(String[] args) throws Exception{
if(args.length<1) {
System.out.println("please enter a website");
return;
}
allUrls=new ArrayList<>();
URL myUrl = new URI(args[0]).toURL();
URLConnection conn=myUrl.openConnection();
crawl(conn, 0);
}
}