Skip to content

Commit

Permalink
[python] Allows send more than 32k utf-8 string (#1719)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Apr 2, 2024
1 parent 62ef99c commit 89a22be
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion engines/python/setup/djl_python/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def retrieve_short(conn):


def retrieve_utf8(conn):
length = retrieve_short(conn)
length = retrieve_int(conn)
if length < 0:
return None

Expand Down
4 changes: 2 additions & 2 deletions engines/python/setup/djl_python/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def binary_encode(data: dict) -> bytes:
@staticmethod
def write_utf8(msg, val):
if val is None:
msg += struct.pack('>h', -1)
msg += struct.pack('>i', -1)
else:
buf = val.encode('utf-8')
msg += struct.pack('>h', len(buf))
msg += struct.pack('>i', len(buf))
msg += buf

def finalize(self, finalize_function, *args):
Expand Down
2 changes: 1 addition & 1 deletion engines/python/setup/djl_python/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def retrieve_short(bytearr: bytearray, start_iter):


def retrieve_utf8(bytearr: bytearray, start_iter):
length, start_iter = retrieve_short(bytearr, start_iter)
length, start_iter = retrieve_int(bytearr, start_iter)
if length < 0:
return None
end_iter = start_iter + length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static byte[] readBytes(ByteBuf in, int maxLength) {
* @return a string read from the buffer.
*/
public static String readUtf8(ByteBuf in) {
int len = in.readShort();
int len = in.readInt();
if (len < 0) {
return null;
}
Expand All @@ -69,10 +69,10 @@ public static String readUtf8(ByteBuf in) {
*/
public static void writeUtf8(ByteBuf buf, String value) {
if (value == null) {
buf.writeShort(-1);
buf.writeInt(-1);
} else {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
buf.writeShort(bytes.length);
buf.writeInt(bytes.length);
buf.writeBytes(bytes);
}
}
Expand Down

0 comments on commit 89a22be

Please sign in to comment.