1 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException { |
2 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
3 | } |
4 | |
5 | public static long parseSnippetID(String snippetID) { |
6 | return Long.parseLong(shortenSnippetID(snippetID)); |
7 | } |
8 | |
9 | private static String shortenSnippetID(String snippetID) { |
10 | if (snippetID.startsWith("#")) |
11 | snippetID = snippetID.substring(1); |
12 | String httpBlaBla = "http://tinybrain.de/"; |
13 | if (snippetID.startsWith(httpBlaBla)) |
14 | snippetID = snippetID.substring(httpBlaBla.length()); |
15 | return snippetID; |
16 | } |
17 | |
18 | public static boolean isSnippetID(String snippetID) { |
19 | snippetID = shortenSnippetID(snippetID); |
20 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
21 | } |
22 | |
23 | public static boolean isInteger(String s) { |
24 | return Pattern.matches("\\-?\\d+", s); |
25 | } |
26 | |
27 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { |
28 | if (preferCached) { |
29 | initSnippetCache(); |
30 | String text = DiskSnippetCache_get(snippetID); |
31 | if (text != null) |
32 | return text; |
33 | } |
34 | |
35 | String text; |
36 | try { |
37 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID); |
38 | text = loadPage(url); |
39 | } catch (FileNotFoundException e) { |
40 | throw new IOException("Snippet #" + snippetID + " not found or not public"); |
41 | } |
42 | |
43 | try { |
44 | initSnippetCache(); |
45 | DiskSnippetCache_put(snippetID, text); |
46 | } catch (IOException e) { |
47 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); |
48 | } |
49 | |
50 | return text; |
51 | } |
52 | |
53 | private static String loadPage(URL url) throws IOException { |
54 | System.out.println("Loading: " + url.toExternalForm()); |
55 | URLConnection con = url.openConnection(); |
56 | return loadPage(con, url); |
57 | } |
58 | |
59 | public static String loadPage(URLConnection con, URL url) throws IOException { |
60 | String contentType = con.getContentType(); |
61 | if (contentType == null) |
62 | throw new IOException("Page could not be read: " + url); |
63 | //Log.info("Content-Type: " + contentType); |
64 | String charset = guessCharset(contentType); |
65 | Reader r = new InputStreamReader(con.getInputStream(), charset); |
66 | StringBuilder buf = new StringBuilder(); |
67 | while (true) { |
68 | int ch = r.read(); |
69 | if (ch < 0) |
70 | break; |
71 | //Log.info("Chars read: " + buf.length()); |
72 | buf.append((char) ch); |
73 | } |
74 | return buf.toString(); |
75 | } |
76 | |
77 | public static String guessCharset(String contentType) { |
78 | Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*"); |
79 | Matcher m = p.matcher(contentType); |
80 | /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ |
81 | return m.matches() ? m.group(1) : "ISO-8859-1"; |
82 | } |
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: | #2000327 |
Snippet name: | loadSnippet and dependencies (function) |
Eternal ID of this version: | #2000327/1 |
Text MD5: | 9235a0941f8fe6769c9fa4f1bc580bee |
Author: | stefan |
Category: | javax |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-29 01:20:42 |
Source code size: | 2864 bytes / 82 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 638 / 145 |
Referenced in: | [show references] |