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

173
LINES

< > BotCompany Repo | #2000371 // Translation of #2000370 with #629

New Tinybrain snippet

1  
public class main {
2  
  public static void main(String[] args) throws Exception {
3  
String prepost = loadSnippet("#1000288");
4  
saveMainJava(prepost.replace("[...]", loadMainJava());
5  
  }
6  
7  
  static String loadMainJava() throws IOException {
8  
    return loadTextFile("input/main.java", "");
9  
  }
10  
11  
  static void saveMainJava(String s) throws IOException {
12  
    saveTextFile("output/main.java", s);
13  
  }
14  
15  
  static boolean preferCached = false;
16  
17  
  public static String loadSnippet(String snippetID) throws IOException {
18  
    return loadSnippet(parseSnippetID(snippetID), preferCached);
19  
  }
20  
21  
  public static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
22  
    return loadSnippet(parseSnippetID(snippetID), preferCached);
23  
  }
24  
25  
  public static long parseSnippetID(String snippetID) {
26  
    return Long.parseLong(shortenSnippetID(snippetID));
27  
  }
28  
29  
  private static String shortenSnippetID(String snippetID) {
30  
    if (snippetID.startsWith("#"))
31  
      snippetID = snippetID.substring(1);
32  
    String httpBlaBla = "http://tinybrain.de/";
33  
    if (snippetID.startsWith(httpBlaBla))
34  
      snippetID = snippetID.substring(httpBlaBla.length());
35  
    return snippetID;
36  
  }
37  
38  
  public static boolean isSnippetID(String snippetID) {
39  
    snippetID = shortenSnippetID(snippetID);
40  
    return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
41  
  }
42  
43  
  public static boolean isInteger(String s) {
44  
    return Pattern.matches("\\-?\\d+", s);
45  
  }
46  
47  
  public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
48  
    if (preferCached) {
49  
      initSnippetCache();
50  
      String text = DiskSnippetCache_get(snippetID);
51  
      if (text != null)
52  
        return text;
53  
    }
54  
55  
    String text;
56  
    try {
57  
      URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
58  
      text = loadPage(url);
59  
    } catch (FileNotFoundException e) {
60  
      throw new IOException("Snippet #" + snippetID + " not found or not public");
61  
    }
62  
63  
    try {
64  
      initSnippetCache();
65  
      DiskSnippetCache_put(snippetID, text);
66  
    } catch (IOException e) {
67  
      System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
68  
    }
69  
70  
    return text;
71  
  }
72  
73  
  private static String loadPage(URL url) throws IOException {
74  
    System.out.println("Loading: " + url.toExternalForm());
75  
    URLConnection con = url.openConnection();
76  
    return loadPage(con, url);
77  
  }
78  
79  
  public static String loadPage(URLConnection con, URL url) throws IOException {
80  
    String contentType = con.getContentType();
81  
    if (contentType == null)
82  
      throw new IOException("Page could not be read: " + url);
83  
    //Log.info("Content-Type: " + contentType);
84  
    String charset = guessCharset(contentType);
85  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
86  
    StringBuilder buf = new StringBuilder();
87  
    while (true) {
88  
      int ch = r.read();
89  
      if (ch < 0)
90  
        break;
91  
      //Log.info("Chars read: " + buf.length());
92  
      buf.append((char) ch);
93  
    }
94  
    return buf.toString();
95  
  }
96  
97  
  public static String guessCharset(String contentType) {
98  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
99  
    Matcher m = p.matcher(contentType);
100  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
101  
    return m.matches() ? m.group(1) : "ISO-8859-1";
102  
  }
103  
104  
  static File DiskSnippetCache_dir;
105  
106  
  public static void initDiskSnippetCache(File dir) {
107  
    DiskSnippetCache_dir = dir;
108  
    dir.mkdirs();
109  
  }
110  
111  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
112  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
113  
  }
114  
115  
  private static File DiskSnippetCache_getFile(long snippetID) {
116  
    return new File(DiskSnippetCache_dir, "" + snippetID);
117  
  }
118  
119  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
120  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
121  
  }
122  
123  
  public static File DiskSnippetCache_getDir() {
124  
    return DiskSnippetCache_dir;
125  
  }
126  
127  
  public static void initSnippetCache() {
128  
    if (DiskSnippetCache_dir == null)
129  
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
130  
  }
131  
  
132  
133  
  /** writes safely (to temp file, then rename) */
134  
  public static void saveTextFile(String fileName, String contents) throws IOException {
135  
    File file = new File(fileName);
136  
    File parentFile = file.getParentFile();
137  
    if (parentFile != null)
138  
      parentFile.mkdirs();
139  
    String tempFileName = fileName + "_temp";
140  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
141  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
142  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
143  
    printWriter.print(contents);
144  
    printWriter.close();
145  
    if (file.exists() && !file.delete())
146  
      throw new IOException("Can't delete " + fileName);
147  
148  
    if (!new File(tempFileName).renameTo(file))
149  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
150  
  }
151  
152  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
153  
    if (!new File(fileName).exists())
154  
      return defaultContents;
155  
156  
    FileInputStream fileInputStream = new FileInputStream(fileName);
157  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
158  
    return loadTextFile(inputStreamReader);
159  
  }
160  
161  
  public static String loadTextFile(Reader reader) throws IOException {
162  
    StringBuilder builder = new StringBuilder();
163  
    try {
164  
      BufferedReader bufferedReader = new BufferedReader(reader);
165  
      String line;
166  
      while ((line = bufferedReader.readLine()) != null)
167  
        builder.append(line).append('\n');
168  
    } finally {
169  
      reader.close();
170  
    }
171  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
172  
  }
173  
}

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: #2000371
Snippet name: Translation of #2000370 with #629
Eternal ID of this version: #2000371/1
Text MD5: 7e8b753d39cffec562e09c37d60de09e
Author: someone
Category:
Type: New Tinybrain snippet
Gummipassword: apply translator 629
Uploaded from IP: 84.201.25.107
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-20 21:24:24
Source code size: 5946 bytes / 173 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 736 / 172
Referenced in: [show references]