Advertisement
Guest User

Untitled

a guest
May 19th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. static class Reader
  2. {
  3. final private int BUFFER_SIZE = 1 << 16;
  4. private DataInputStream din;
  5. private byte[] buffer;
  6. private int bufferPointer, bytesRead;
  7.  
  8. public Reader()
  9. {
  10. din = new DataInputStream(System.in);
  11. buffer = new byte[BUFFER_SIZE];
  12. bufferPointer = bytesRead = 0;
  13. }
  14.  
  15. public Reader(String file_name) throws IOException
  16. {
  17. din = new DataInputStream(new FileInputStream(file_name));
  18. buffer = new byte[BUFFER_SIZE];
  19. bufferPointer = bytesRead = 0;
  20. }
  21.  
  22. public String readLine() throws IOException
  23. {
  24. byte[] buf = new byte[64]; // line length
  25. int cnt = 0, c;
  26. while ((c = read()) != -1)
  27. {
  28. if (c == '\n')
  29. break;
  30. buf[cnt++] = (byte) c;
  31. }
  32. return new String(buf, 0, cnt);
  33. }
  34.  
  35. public int nextInt() throws IOException
  36. {
  37. int ret = 0;
  38. byte c = read();
  39. while (c <= ' ')
  40. c = read();
  41. boolean neg = (c == '-');
  42. if (neg)
  43. c = read();
  44. do
  45. {
  46. ret = ret * 10 + c - '0';
  47. } while ((c = read()) >= '0' && c <= '9');
  48.  
  49. if (neg)
  50. return -ret;
  51. return ret;
  52. }
  53.  
  54. public long nextLong() throws IOException
  55. {
  56. long ret = 0;
  57. byte c = read();
  58. while (c <= ' ')
  59. c = read();
  60. boolean neg = (c == '-');
  61. if (neg)
  62. c = read();
  63. do {
  64. ret = ret * 10 + c - '0';
  65. }
  66. while ((c = read()) >= '0' && c <= '9');
  67. if (neg)
  68. return -ret;
  69. return ret;
  70. }
  71.  
  72. public double nextDouble() throws IOException
  73. {
  74. double ret = 0, div = 1;
  75. byte c = read();
  76. while (c <= ' ')
  77. c = read();
  78. boolean neg = (c == '-');
  79. if (neg)
  80. c = read();
  81.  
  82. do {
  83. ret = ret * 10 + c - '0';
  84. }
  85. while ((c = read()) >= '0' && c <= '9');
  86.  
  87. if (c == '.')
  88. {
  89. while ((c = read()) >= '0' && c <= '9')
  90. {
  91. ret += (c - '0') / (div *= 10);
  92. }
  93. }
  94.  
  95. if (neg)
  96. return -ret;
  97. return ret;
  98. }
  99.  
  100. private void fillBuffer() throws IOException
  101. {
  102. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  103. if (bytesRead == -1)
  104. buffer[0] = -1;
  105. }
  106.  
  107. private byte read() throws IOException
  108. {
  109. if (bufferPointer == bytesRead)
  110. fillBuffer();
  111. return buffer[bufferPointer++];
  112. }
  113.  
  114. public void close() throws IOException
  115. {
  116. if (din == null)
  117. return;
  118. din.close();
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement