package org.jvirtanen.parity.net.poe;

import static java.nio.charset.StandardCharsets.*;

import java.nio.ByteBuffer;

class ByteBuffers {

    private static final byte SPACE = ' ';

    static String getString(ByteBuffer buffer, int length) {
        byte[] bytes = new  byte[length];

        buffer.get(bytes);

        return new String(bytes, US_ASCII);
    }

    static void putString(ByteBuffer buffer, String value, int length) {
        byte[] bytes = value.getBytes(US_ASCII);

        int i = 0;

        for (; i < Math.min(bytes.length, length); i++)
            buffer.put(bytes[i]);

        for (; i < length; i++)
            buffer.put(SPACE);
    }

}

@@ -438,58 +438,8 @@
 ) %7B%0A
-        byte%5B%5D bytes = value.getBytes(US_ASCII);%0A%0A
     
@@ -486,20 +486,22 @@
 min(
-bytes
+value
 .length
+()
 , le
@@ -539,16 +539,29 @@
 put(
+(
 byte
-s%5Bi%5D
+)value.charAt(i)
 );%0A%0A

Improve string encoding in network protocols