Skip to content

Commit b0bed51

Browse files
committed
Finished up test class
1 parent b8310f5 commit b0bed51

File tree

1 file changed

+103
-31
lines changed

1 file changed

+103
-31
lines changed
Lines changed: 103 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,122 @@
11
package coffee.weneed.funkeys.rdf;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
45
import java.io.FileNotFoundException;
56
import java.io.FileOutputStream;
67
import java.io.IOException;
8+
import java.io.InputStream;
79
import java.io.UnsupportedEncodingException;
810
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
import java.net.URLConnection;
913
import java.nio.charset.StandardCharsets;
10-
11-
import coffee.weneed.utils.LogicUtil;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
1216

1317
public class RDFTest {
1418

15-
public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException {
16-
String s = new String(LogicUtil.downloadUrl(
17-
new File("G:\\UBFunkeys\\U.B. Funkeys 5.0 Full by Daleth\\U.B. Funkeys\\RadicaGame\\data\\system\\config.rdf").toURI().toURL()),
18-
"ISO-8859-1");
19-
String d = RDFUtil.decode(s);
20-
File f = new File("G:\\UBFunkeys\\New folder\\config.txt");
21-
f.delete();
19+
/**
20+
* https://stackoverflow.com/questions/2295221/java-net-url-read-stream-to-byte
21+
*
22+
* @author StackOverflow:ron-reiter
23+
* @param toDownload the to download
24+
* @return byte array
25+
*/
26+
public static byte[] downloadUrl(URL toDownload) {
27+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
28+
2229
try {
23-
FileOutputStream st = new FileOutputStream(f);
24-
st.write(d.getBytes(StandardCharsets.ISO_8859_1));
25-
st.flush();
26-
st.close();
27-
} catch (FileNotFoundException e) {
28-
// TODO Auto-generated catch block
29-
e.printStackTrace();
30+
byte[] chunk = new byte[4096];
31+
int bytesRead;
32+
33+
URLConnection con = toDownload.openConnection();
34+
con.setUseCaches(false);
35+
con.setDefaultUseCaches(false);
36+
InputStream stream = con.getInputStream();
37+
while ((bytesRead = stream.read(chunk)) > 0) {
38+
outputStream.write(chunk, 0, bytesRead);
39+
}
40+
stream.close();
3041
} catch (IOException e) {
31-
// TODO Auto-generated catch block
3242
e.printStackTrace();
43+
return null;
3344
}
34-
String e1 = RDFUtil.encode(d);
35-
f = new File("G:\\UBFunkeys\\New folder\\config.rdf");
36-
f.delete();
37-
try {
38-
FileOutputStream st = new FileOutputStream(f);
39-
st.write(e1.getBytes(StandardCharsets.ISO_8859_1));
40-
st.flush();
41-
st.close();
42-
} catch (FileNotFoundException e) {
43-
// TODO Auto-generated catch block
44-
e.printStackTrace();
45-
} catch (IOException e) {
46-
// TODO Auto-generated catch block
47-
e.printStackTrace();
45+
46+
return outputStream.toByteArray();
47+
}
48+
49+
public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException {
50+
51+
Path currentRelativePath = Paths.get("");
52+
String s = currentRelativePath.toAbsolutePath().toString();
53+
File input = new File(s + "/input");
54+
File output = new File(s + "/output");
55+
File info = new File(s + "/Readme.txt");
56+
if (!input.exists()) {
57+
input.mkdir();
58+
}
59+
if (!output.exists()) {
60+
output.mkdir();
61+
}
62+
if (!info.exists()) {
63+
String txt =
64+
"UBFunkeys RDF Util By Daleth with plenty of help from Vincentetcarine\nhttps://github.com/WeNeedCoffee/UBFunkeysRDF\n\nUsage: \n\n1. Run once to generate the required folders\n2. Put your gamedata files in /input, put .xml files as the decoded ones you want recompiled, and .rdf for encoded ones you want to decode.\n3. Run once.\n4. ???\n5. Profit! You have decoded/encoded the rdf files!\n\nDonate to my patreon if you feel my work helped you out! https://www.patreon.com/Dalethium\n";
65+
try {
66+
FileOutputStream st = new FileOutputStream(info);
67+
st.write(txt.getBytes());
68+
st.flush();
69+
st.close();
70+
} catch (FileNotFoundException e) {
71+
// TODO Auto-generated catch block
72+
e.printStackTrace();
73+
} catch (IOException e) {
74+
// TODO Auto-generated catch block
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
for (File f : input.listFiles()) {
80+
if (f.isDirectory()) {
81+
continue;
82+
}
83+
if (f.getName().toLowerCase().endsWith(".rdf")) {
84+
String d = RDFUtil.decode(new String(RDFTest.downloadUrl(f.toURI().toURL()), StandardCharsets.ISO_8859_1));
85+
File f1 = new File(output.getAbsolutePath().toString() + "/" + f.getName().substring(0, f.getName().length() - 4) + ".xml");
86+
if (f1.exists()) {
87+
f1.delete();
88+
}
89+
try {
90+
FileOutputStream st = new FileOutputStream(f1);
91+
st.write(d.getBytes(StandardCharsets.ISO_8859_1));
92+
st.flush();
93+
st.close();
94+
} catch (FileNotFoundException e) {
95+
// TODO Auto-generated catch block
96+
e.printStackTrace();
97+
} catch (IOException e) {
98+
// TODO Auto-generated catch block
99+
e.printStackTrace();
100+
}
101+
} else if (f.getName().toLowerCase().endsWith(".xml")) {
102+
String d = RDFUtil.encode(new String(RDFTest.downloadUrl(f.toURI().toURL()), StandardCharsets.ISO_8859_1));
103+
File f1 = new File(output.getAbsolutePath().toString() + "/" + f.getName().substring(0, f.getName().length() - 4) + ".rdf");
104+
if (f1.exists()) {
105+
f1.delete();
106+
}
107+
try {
108+
FileOutputStream st = new FileOutputStream(f1);
109+
st.write(d.getBytes(StandardCharsets.ISO_8859_1));
110+
st.flush();
111+
st.close();
112+
} catch (FileNotFoundException e) {
113+
// TODO Auto-generated catch block
114+
e.printStackTrace();
115+
} catch (IOException e) {
116+
// TODO Auto-generated catch block
117+
e.printStackTrace();
118+
}
119+
}
48120
}
49121
}
50122
}

0 commit comments

Comments
 (0)