RedisStreamReader implements Closeable { public int readInt() throws IOException { int length = 0; final byte[] allIntBytes = new byte[13]; while ((allIntBytes[length++] = readByte()) != RedisBytes.CARRIAGE_RETURN_BYTE) { ; } readByte(); int number = 0; final boolean negative = allIntBytes.length > 0 && allIntBytes[0] == RedisBytes.DASH_BYTE; for (int i = length - 2, j = 0; i > (negative ? 0 : -1); i--, j++) { number += (int) (Math.pow(10, j) * (allIntBytes[i] - RedisBytes.ZERO_BYTE)); } number = number * (negative ? -1 : 1); return number; } RedisStreamReader(InputStream inputStream); byte readByte(); String readString(); int readInt(); byte[] read(int length); @Override void close(); static final int BUFFER_SIZE; }
@Test public void readPostiveInt() throws IOException { final byte[] bytes = {'1', '0', '\r', '\n'}; setupInputStreamRead(bytes, 1); int actualInt = reader.readInt(); assertEquals(10, actualInt); }
@Test public void readNegativeInt() throws IOException { final byte[] bytes = {'-', '1', '0', '\r', '\n'}; setupInputStreamRead(bytes, 1); int actualInt = reader.readInt(); assertEquals(-10, actualInt); }