1 | import java.net.*; |
2 | import java.io.*; |
3 | import javax.swing.*; |
4 | import java.util.regex.*; |
5 | import java.util.*; |
6 | public class main { |
7 | public static void main(String[] args) throws Exception { |
8 | |
9 | String s = loadMainJava(); |
10 | s = s.replaceAll("new\\s+File\\(", "virtual.newFile("); |
11 | s = s.replaceAll("new\\s+FileInputStream\\(", "virtual.newFileInputStream("); |
12 | s = s.replaceAll("new\\s+FileOutputStream\\(", "virtual.newFileOutputStream("); |
13 | s += "\n\n" + loadSnippet("#2000350"); // load class virtual |
14 | saveMainJava(s); |
15 | } |
16 | |
17 | static String loadMainJava() throws IOException { |
18 | return loadTextFile("input/main.java", ""); |
19 | } |
20 | |
21 | static void saveMainJava(String s) throws IOException { |
22 | saveTextFile("output/main.java", s); |
23 | } |
24 | |
25 | static boolean preferCached = false; |
26 | |
27 | public static String loadSnippet(String snippetID) throws IOException { |
28 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
29 | } |
30 | |
31 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
32 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
33 | } |
34 | |
35 | public static long parseSnippetID(String snippetID) { |
36 | return Long.parseLong(shortenSnippetID(snippetID)); |
37 | } |
38 | |
39 | private static String shortenSnippetID(String snippetID) { |
40 | if (snippetID.startsWith("#")) |
41 | snippetID = snippetID.substring(1); |
42 | String httpBlaBla = "http://tinybrain.de/"; |
43 | if (snippetID.startsWith(httpBlaBla)) |
44 | snippetID = snippetID.substring(httpBlaBla.length()); |
45 | return snippetID; |
46 | } |
47 | |
48 | public static boolean isSnippetID(String snippetID) { |
49 | snippetID = shortenSnippetID(snippetID); |
50 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
51 | } |
52 | |
53 | public static boolean isInteger(String s) { |
54 | return Pattern.matches("\\-?\\d+", s); |
55 | } |
56 | |
57 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
58 | if (preferCached) { |
59 | initSnippetCache(); |
60 | String text = DiskSnippetCache_get(snippetID); |
61 | if (text != null) |
62 | return text; |
63 | } |
64 | |
65 | String text; |
66 | try { |
67 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
68 | text = loadPage(url); |
69 | } catch (FileNotFoundException e) { |
70 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
71 | } |
72 | |
73 | try { |
74 | initSnippetCache(); |
75 | DiskSnippetCache_put(snippetID, text); |
76 | } catch (IOException e) { |
77 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
78 | } |
79 | |
80 | return text; |
81 | } |
82 | |
83 | private static String loadPage(URL url) throws IOException { |
84 | System.out.println("Loading: " + url.toExternalForm()); |
85 | URLConnection con = url.openConnection(); |
86 | return loadPage(con, url); |
87 | } |
88 | |
89 | public static String loadPage(URLConnection con, URL url) throws IOException { |
90 | String contentType = con.getContentType(); |
91 | if (contentType == null) |
92 | throw new IOException("Page could not be read: " + url); |
93 | //Log.info("Content-Type: " + contentType); |
94 | String charset = guessCharset(contentType); |
95 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
96 | StringBuilder buf = new StringBuilder(); |
97 | while (true) { |
98 | int ch = r.read(); |
99 | if (ch < 0) |
100 | break; |
101 | //Log.info("Chars read: " + buf.length()); |
102 | buf.append((char) ch); |
103 | } |
104 | return buf.toString(); |
105 | } |
106 | |
107 | public static String guessCharset(String contentType) { |
108 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
109 | Matcher m = p.matcher(contentType); |
110 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
111 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
112 | } |
113 | |
114 | static File DiskSnippetCache_dir; |
115 | |
116 | public static void initDiskSnippetCache(File dir) { |
117 | DiskSnippetCache_dir = dir; |
118 | dir.mkdirs(); |
119 | } |
120 | |
121 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { |
122 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
123 | } |
124 | |
125 | private static File DiskSnippetCache_getFile(long snippetID) { |
126 | return new File(DiskSnippetCache_dir, "" + snippetID); |
127 | } |
128 | |
129 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { |
130 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
131 | } |
132 | |
133 | public static File DiskSnippetCache_getDir() { |
134 | return DiskSnippetCache_dir; |
135 | } |
136 | |
137 | public static void initSnippetCache() { |
138 | if (DiskSnippetCache_dir == null) |
139 | initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache")); |
140 | } |
141 | |
142 | |
143 | /** writes safely (to temp file, then rename) */ |
144 | public static void saveTextFile(String fileName, String contents) throws IOException { |
145 | File file = new File(fileName); |
146 | File parentFile = file.getParentFile(); |
147 | if (parentFile != null) |
148 | parentFile.mkdirs(); |
149 | String tempFileName = fileName + "_temp"; |
150 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
151 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
152 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
153 | printWriter.print(contents); |
154 | printWriter.close(); |
155 | if (file.exists() && !file.delete()) |
156 | throw new IOException("Can't delete " + fileName); |
157 | |
158 | if (!new File(tempFileName).renameTo(file)) |
159 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
160 | } |
161 | |
162 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
163 | if (!new File(fileName).exists()) |
164 | return defaultContents; |
165 | |
166 | FileInputStream fileInputStream = new FileInputStream(fileName); |
167 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
168 | return loadTextFile(inputStreamReader); |
169 | } |
170 | |
171 | public static String loadTextFile(Reader reader) throws IOException { |
172 | StringBuilder builder = new StringBuilder(); |
173 | try { |
174 | BufferedReader bufferedReader = new BufferedReader(reader); |
175 | String line; |
176 | while ((line = bufferedReader.readLine()) != null) |
177 | builder.append(line).append('\n'); |
178 | } finally { |
179 | reader.close(); |
180 | } |
181 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
182 | } |
183 | } |
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #2000351 |
Snippet name: | Compiled #618 |
Eternal ID of this version: | #2000351/1 |
Text MD5: | 176063502f62b8aabcec98a2839bfb70 |
Author: | stefan |
Category: | javax |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-05-12 20:57:43 |
Source code size: | 6456 bytes / 183 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 718 / 3898 |
Referenced in: | [show references] |