common-utils/src/main/java/org/warp/commonutils/serialization/UTFUtils.java

22 lines
600 B
Java
Raw Normal View History

2020-06-12 18:36:36 +02:00
package org.warp.commonutils.serialization;
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 {
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);
}
public static final String readUTF(DataInput in) throws IOException {
2020-06-12 18:36:36 +02:00
int len = in.readInt();
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
}
}