1 | static boolean preferCached = false; |
2 | |
3 | public static String loadSnippet(String snippetID) throws IOException {
|
4 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
5 | } |
6 | |
7 | public static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
|
8 | return loadSnippet(parseSnippetID(snippetID), preferCached); |
9 | } |
10 | |
11 | public static long parseSnippetID(String snippetID) {
|
12 | return Long.parseLong(shortenSnippetID(snippetID)); |
13 | } |
14 | |
15 | private static String shortenSnippetID(String snippetID) {
|
16 | if (snippetID.startsWith("#"))
|
17 | snippetID = snippetID.substring(1); |
18 | String httpBlaBla = "http://tinybrain.de/"; |
19 | if (snippetID.startsWith(httpBlaBla)) |
20 | snippetID = snippetID.substring(httpBlaBla.length()); |
21 | return snippetID; |
22 | } |
23 | |
24 | public static boolean isSnippetID(String snippetID) {
|
25 | snippetID = shortenSnippetID(snippetID); |
26 | return isInteger(snippetID) && Long.parseLong(snippetID) != 0; |
27 | } |
28 | |
29 | public static boolean isInteger(String s) {
|
30 | return Pattern.matches("\\-?\\d+", s);
|
31 | } |
32 | |
33 | public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
|
34 | if (preferCached) {
|
35 | initSnippetCache(); |
36 | String text = DiskSnippetCache_get(snippetID); |
37 | if (text != null) |
38 | return text; |
39 | } |
40 | |
41 | String text; |
42 | try {
|
43 | URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
|
44 | text = loadPage(url); |
45 | } catch (FileNotFoundException e) {
|
46 | throw new IOException("Snippet #" + snippetID + " not found or not public");
|
47 | } |
48 | |
49 | try {
|
50 | initSnippetCache(); |
51 | DiskSnippetCache_put(snippetID, text); |
52 | } catch (IOException e) {
|
53 | System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")");
|
54 | } |
55 | |
56 | return text; |
57 | } |
58 | |
59 | static File DiskSnippetCache_dir; |
60 | |
61 | public static void initDiskSnippetCache(File dir) {
|
62 | DiskSnippetCache_dir = dir; |
63 | dir.mkdirs(); |
64 | } |
65 | |
66 | public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
|
67 | return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); |
68 | } |
69 | |
70 | private static File DiskSnippetCache_getFile(long snippetID) {
|
71 | return new File(DiskSnippetCache_dir, "" + snippetID); |
72 | } |
73 | |
74 | public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
|
75 | saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); |
76 | } |
77 | |
78 | public static File DiskSnippetCache_getDir() {
|
79 | return DiskSnippetCache_dir; |
80 | } |
81 | |
82 | public static void initSnippetCache() {
|
83 | if (DiskSnippetCache_dir == null) |
84 | initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
|
85 | } |
Began life as a copy of #2000330
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: | #2000498 |
| Snippet name: | loadSnippet and dependencies (function) |
| Eternal ID of this version: | #2000498/1 |
| Text MD5: | 34b0c7877ec11915386d81f28aa87735 |
| Author: | stefan |
| Category: | javax |
| Type: | New Tinybrain snippet |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-08-06 00:25:31 |
| Source code size: | 2811 bytes / 85 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 760 / 2538 |
| Referenced in: | [show references] |