Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

116
LINES

< > BotCompany Repo | #2000330 // loadSnippet and dependencies (function)

New Tinybrain snippet

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  
  private static String loadPage(URL url) throws IOException {
60  
    System.out.println("Loading: " + url.toExternalForm());
61  
    URLConnection con = url.openConnection();
62  
    return loadPage(con, url);
63  
  }
64  
65  
  public static String loadPage(URLConnection con, URL url) throws IOException {
66  
    String contentType = con.getContentType();
67  
    if (contentType == null)
68  
      throw new IOException("Page could not be read: " + url);
69  
    //Log.info("Content-Type: " + contentType);
70  
    String charset = guessCharset(contentType);
71  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
72  
    StringBuilder buf = new StringBuilder();
73  
    while (true) {
74  
      int ch = r.read();
75  
      if (ch < 0)
76  
        break;
77  
      //Log.info("Chars read: " + buf.length());
78  
      buf.append((char) ch);
79  
    }
80  
    return buf.toString();
81  
  }
82  
83  
  public static String guessCharset(String contentType) {
84  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
85  
    Matcher m = p.matcher(contentType);
86  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
87  
    return m.matches() ? m.group(1) : "ISO-8859-1";
88  
  }
89  
90  
  static File DiskSnippetCache_dir;
91  
92  
  public static void initDiskSnippetCache(File dir) {
93  
    DiskSnippetCache_dir = dir;
94  
    dir.mkdirs();
95  
  }
96  
97  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
98  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
99  
  }
100  
101  
  private static File DiskSnippetCache_getFile(long snippetID) {
102  
    return new File(DiskSnippetCache_dir, "" + snippetID);
103  
  }
104  
105  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
106  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
107  
  }
108  
109  
  public static File DiskSnippetCache_getDir() {
110  
    return DiskSnippetCache_dir;
111  
  }
112  
113  
  public static void initSnippetCache() {
114  
    if (DiskSnippetCache_dir == null)
115  
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
116  
  }

Author comment

Began life as a copy of #2000328

download  show line numbers   

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: #2000330
Snippet name: loadSnippet and dependencies (function)
Eternal ID of this version: #2000330/1
Text MD5: a5fc3732e1dbd2c6bd5c425fd00b4e99
Author: stefan
Category: javax
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-30 17:04:19
Source code size: 3996 bytes / 116 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 633 / 1979
Referenced in: [show references]