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

110
LINES

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

New Tinybrain snippet

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  
  }
83  
84  
  static File DiskSnippetCache_dir;
85  
86  
  public static void initDiskSnippetCache(File dir) {
87  
    DiskSnippetCache_dir = dir;
88  
    dir.mkdirs();
89  
  }
90  
91  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
92  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
93  
  }
94  
95  
  private static File DiskSnippetCache_getFile(long snippetID) {
96  
    return new File(DiskSnippetCache_dir, "" + snippetID);
97  
  }
98  
99  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
100  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
101  
  }
102  
103  
  public static File DiskSnippetCache_getDir() {
104  
    return DiskSnippetCache_dir;
105  
  }
106  
107  
  public static void initSnippetCache() {
108  
    if (DiskSnippetCache_dir == null)
109  
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
110  
  }

Author comment

Began life as a copy of #2000327

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: #2000328
Snippet name: loadSnippet and dependencies (function)
Eternal ID of this version: #2000328/1
Text MD5: 9964ae3f5be0e90b147c77a93eb66560
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:29:26
Source code size: 3806 bytes / 110 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 557 / 119
Referenced in: [show references]