fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Solution {
  9. public static Map<String, String> runtimeStorage = new HashMap<>();
  10.  
  11. public static void save(OutputStream outputStream) throws IOException {
  12. runtimeStorage.forEach((k, v) -> {
  13. if (k.contains("=")) k = k.replace("=", "\\=");
  14. if (v.contains("=")) v = v.replace("=", "\\=");
  15. if (k.contains(":")) k = k.replace(":", "\\:");
  16. if (v.contains(":")) v = v.replace(":", "\\:");
  17. if (k.contains(" ")) k = k.replace(" ", "\\ ");
  18. String line = String.format("%s:%s\n", k, v);
  19. try {
  20. outputStream.write(line.getBytes());
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. });
  25. outputStream.close();
  26. }
  27.  
  28. public static void load(InputStream inputStream) throws IOException {
  29. BufferedReader buffInputStream = new BufferedReader(new InputStreamReader(inputStream));
  30. String line;
  31. String previousLine = null;
  32. String key = null, value = null;
  33. int index;
  34. StringBuilder sb = new StringBuilder();
  35. while ((line = buffInputStream.readLine()) != null) {
  36. if (line.startsWith("#") || line.startsWith("!") || line.isEmpty()) continue;
  37. if (line.endsWith("\\")) {
  38. sb.append(line.trim());
  39. previousLine = line;
  40. continue;
  41. } else if (previousLine != null && previousLine.endsWith("\\")) sb.append(line.trim());
  42.  
  43. if (sb.length() > 0) line = sb.toString().replace("\\", "");
  44. if (line.contains("=") || line.contains(":")) {
  45. for (int i = 0; i < line.length(); i++) {
  46. if (line.charAt(i) == '=' && line.charAt(i - 1) != '\\') {
  47. key = line.substring(0, i).replace("\\", "").trim();
  48. value = line.substring(i + 1).trim();
  49. break;
  50. } else if (line.charAt(i) == ':' && line.charAt(i - 1) != '\\') {
  51. key = line.substring(0, i).replace("\\", "").trim();
  52. value = line.substring(i + 1).trim();
  53. break;
  54. }
  55. }
  56. }
  57. if (key != null && value != null) runtimeStorage.put(key, value);
  58. }
  59. buffInputStream.close();
  60. }
  61.  
  62. public static void main(String[] args) {
  63. }
  64. }
Success #stdin #stdout 0.06s 32544KB
stdin
Standard input is empty
stdout
Standard output is empty