Here you can find the source of writeUtf8String(final DataOutputStream out, final String str)
Parameter | Description |
---|---|
out | The buffer. |
str | The string. |
Parameter | Description |
---|---|
IOException | When opening the file failed. |
public static void writeUtf8String(final DataOutputStream out, final String str) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.charset.Charset; public class Main { /**//www . java2 s . c o m * The UTF-8 character set. */ private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); private static final int MAXLEN = 65535; /** * Writes a UTF-8 string to the buffer. * * @param out The buffer. * @param str The string. * @throws IOException When opening the file failed. */ public static void writeUtf8String(final DataOutputStream out, final String str) throws IOException { byte[] bytes = str.getBytes(CHARSET_UTF8); if (bytes.length > MAXLEN) { throw new IllegalArgumentException("Encoded UTF-8 string too long."); } out.writeShort(bytes.length); out.write(bytes); } }