2020-06-12 18:36:36 +02:00
|
|
|
package org.warp.commonutils.serialization;
|
|
|
|
|
2021-01-27 03:04:35 +01:00
|
|
|
import java.io.DataInput;
|
|
|
|
import java.io.DataOutput;
|
2020-06-12 18:36:36 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
|
|
public class UTFUtils {
|
2021-01-27 03:04:35 +01:00
|
|
|
public static final void writeUTF(DataOutput out, String utf) throws IOException {
|
2020-06-12 18:36:36 +02:00
|
|
|
byte[] bytes = utf.getBytes(StandardCharsets.UTF_8);
|
|
|
|
out.writeInt(bytes.length);
|
|
|
|
out.write(bytes);
|
|
|
|
}
|
|
|
|
|
2021-01-27 03:04:35 +01:00
|
|
|
public static final String readUTF(DataInput in) throws IOException {
|
2020-06-12 18:36:36 +02:00
|
|
|
int len = in.readInt();
|
2021-01-27 03:04:35 +01:00
|
|
|
byte[] data = new byte[len];
|
|
|
|
in.readFully(data, 0, len);
|
|
|
|
return new String(data, StandardCharsets.UTF_8);
|
2020-06-12 18:36:36 +02:00
|
|
|
}
|
|
|
|
}
|