-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHunterResult.java
More file actions
197 lines (172 loc) · 4.51 KB
/
HunterResult.java
File metadata and controls
197 lines (172 loc) · 4.51 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
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
//
// This is the result class, that saves all results in HTML-format
// (c) Jimmy Larsson 1997
//
//
import java.util.*;
import java.io.*;
import java.net.*;
public final class HunterResult
{
protected RandomAccessFile outFile;
protected String outFileName;
protected int error;
protected HunterStatistics stats;
protected boolean addBlock;
protected Thread blockedThread;
// fileName shuold be without the ".html" extension
public HunterResult (String fileName, HunterStatistics stat)
{
outFileName = fileName;
stats = stat;
addBlock = false;
blockedThread = null;
error = 0;
// Sets error var on errors
// Open file
try
{
outFile = new RandomAccessFile (fileName + ".html", "rw");
} catch (IOException e1)
{
outFile = null;
error = 1;
}
if (error == 0)
{
this.writeHeader(outFile);
}
}
public int error ()
{
return error;
}
public void close ()
{
// Finish up
if (outFile != null)
{
writeFooter(outFile);
try {
outFile.close ();
} catch (IOException e1) {}
outFile = null;
}
}
public synchronized void add (HunterUrl url, int count)
{
if (addBlock)
{
blockedThread = Thread.currentThread ();
blockedThread.suspend();
}
if (outFile != null)
{
stats.addSuccessedUrl ();
url.setCount (count);
try {
outFile.writeUTF ("<A HREF=\"" + url.toString() + "\">" + url.toString() + "</A><BR>\n");
} catch (IOException e1) {}
}
}
// REturns the name of the temporary file
public String dumpTempResults ()
{
// Block everybody trying to add new results until we're done
addBlock = true;
RandomAccessFile tmpFile;
String tmpFileName = outFileName + "_tmp.html";
try {
tmpFile = new RandomAccessFile (tmpFileName, "rw");
}catch (IOException e1)
{
addBlock = false;
if (blockedThread != null)
{
blockedThread.resume();
blockedThread = null;
}
return null;
}
try {
outFile.getFD().sync();
long pos = outFile.getFilePointer ();
outFile.seek(0);
String tmpLine;
while ((tmpLine = outFile.readUTF()) != null)
{
tmpFile.writeUTF(tmpLine + "\n");
}
writeFooter (tmpFile);
tmpFile.getFD().sync();
tmpFile.close();
outFile.seek(pos);
} catch (IOException e1)
{
addBlock = false;
if (blockedThread != null)
{
blockedThread.resume();
blockedThread = null;
}
try {
tmpFile.close();
}catch (IOException e2) {}
return null;
}
addBlock = false;
if (blockedThread != null)
{
blockedThread.resume();
blockedThread = null;
}
return tmpFileName;
}
// Protected methods
protected void finalize ()
{
this.close ();
}
protected void writeHeader (RandomAccessFile file)
{
if (file != null)
{
try {
file.writeUTF ("<HTML><HEAD>\n");
file.writeUTF ("<TITLE>Hunter output</TITLE></HEAD>\n");
file.writeUTF ("<BODY BGCOLOR=\"#c3c3a3\" LINK=\"#0000ff\" VLINK=\"#551a8b\" ALINK=\"#ff8000\">\n");
file.writeUTF ("<FONT SIZE=+3>Hunt results:</FONT>\n");
file.writeUTF ("<HR ALIGN=LEFT SIZE=\"1\">\n");
} catch (IOException e1) {}
}
}
protected void writeFooter (RandomAccessFile file)
{
if (file != null)
{
try {
file.writeUTF ("<HR ALIGN=LEFT SIZE=\"1\">\n");
file.writeUTF ("<FONT SIZE=-1>" + stats.getProcessedUrls()+" URL's processed, "+stats.getBadUrls()+" bad, " + stats.getSuccessedUrls()+" successful, "+stats.getFailedUrls()+" failed. </FONT>\n");
file.writeUTF ("<HR ALIGN=LEFT SIZE=\"1\">\n");
file.writeUTF ("<I><FONT SIZE=-1>(C) Jimmy Larsson 1998</FONT></I>\n");
file.writeUTF ("</BODY></HTML>\n");
} catch (IOException e1) {}
}
}
// Main, just for testing...
public static void main (String args[])
{
HunterStatistics testStat = new HunterStatistics (null);
HunterResult testRes = new HunterResult ("HunterResult-test.html", testStat);
try {
testRes.add(new HunterUrl (new URL ("http", "emma.jimnet.se", 80, "/howto"), 1), 1);
testRes.add(new HunterUrl (new URL ("http", "emma.jimnet.se", 80, "/howto/mini"), 1), 1);
testRes.add(new HunterUrl (new URL ("http", "emma.jimnet.se", 80, "/howto/other-formats"), 1), 1);
testRes.close ();
System.out.print ("Output written to 'HunterResult-test.html'...\n");
} catch (MalformedURLException e1)
{
System.out.print ("HunterResult-test: Bad URL!");
}
}
}