Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public class InputReader {
  2. private InputStream stream;
  3. private byte[] buf = new byte[1024];
  4.  
  5. private int curChar;
  6.  
  7. private int numChars;
  8.  
  9. public InputReader(InputStream stream) {
  10. this.stream = stream;
  11. }
  12.  
  13. public int read() {
  14. if (numChars == -1)
  15. throw new RuntimeException();
  16. if (curChar >= numChars) {
  17. curChar = 0;
  18. try {
  19. numChars = stream.read(buf);
  20. } catch (IOException e) {
  21. throw new RuntimeException();
  22. }
  23. if (numChars <= 0)
  24. return -1;
  25. }
  26. return buf[curChar++];
  27. }
  28.  
  29. public String readString() {
  30. final StringBuilder stringBuilder = new StringBuilder();
  31. int c = read();
  32. while (isSpaceChar(c))
  33. c = read();
  34. do {
  35. stringBuilder.append((char)c);
  36. c = read();
  37. } while (!isSpaceChar(c));
  38. return stringBuilder.toString();
  39. }
  40.  
  41. public int readInt() {
  42. int c = read();
  43. while (isSpaceChar(c))
  44. c = read();
  45. int sgn = 1;
  46. if (c == '-') {
  47. sgn = -1;
  48. c = read();
  49. }
  50. int res = 0;
  51. do {
  52. res *= 10;
  53. res += c - '0';
  54. c = read();
  55. } while (!isSpaceChar(c));
  56. return res * sgn;
  57. }
  58.  
  59. public long readLong() {
  60. int c = read();
  61. while (isSpaceChar(c))
  62. c = read();
  63. int sgn = 1;
  64. if (c == '-') {
  65. sgn = -1;
  66. c = read();
  67. }
  68. long res = 0;
  69. do {
  70. res *= 10;
  71. res += c - '0';
  72. c = read();
  73. } while (!isSpaceChar(c));
  74. return res * sgn;
  75. }
  76.  
  77. public boolean isSpaceChar(int c) {
  78. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement