public CommandArgs<K, V> add(Map<K, V> map) { if (map.size() > 2) { realloc(buffer.capacity() + 16 * map.size()); } for (Map.Entry<K, V> entry : map.entrySet()) { write(codec.encodeMapKey(entry.getKey())); write(codec.encodeMapValue(entry.getValue())); } return this; }
private CommandArgs<K, V> write(String arg) { int length = arg.length(); buffer.mark(); if (buffer.remaining() < length) { int estimate = buffer.remaining() + length + 10; realloc(max(buffer.capacity() * 2, estimate)); } while (true) { try { buffer.put((byte) '$'); write(length); buffer.put(CRLF); for (int i = 0; i < length; i++) { buffer.put((byte) arg.charAt(i)); } buffer.put(CRLF); break; } catch (BufferOverflowException e) { buffer.reset(); realloc(buffer.capacity() * 2); } } count++; return this; }
private CommandArgs<K, V> write(byte[] arg) { buffer.mark(); if (buffer.remaining() < arg.length) { int estimate = buffer.remaining() + arg.length + 10; realloc(max(buffer.capacity() * 2, estimate)); } while (true) { try { buffer.put((byte) '$'); write(arg.length); buffer.put(CRLF); buffer.put(arg); buffer.put(CRLF); break; } catch (BufferOverflowException e) { buffer.reset(); realloc(buffer.capacity() * 2); } } count++; return this; }