Java UTF File Write writeUtf8String(final DataOutputStream out, final String str)

Here you can find the source of writeUtf8String(final DataOutputStream out, final String str)

Description

Writes a UTF-8 string to the buffer.

License

Open Source License

Parameter

Parameter Description
out The buffer.
str The string.

Exception

Parameter Description
IOException When opening the file failed.

Declaration

public static void writeUtf8String(final DataOutputStream out, final String str) throws IOException 

Method Source Code


//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);
    }
}

Related

  1. writeToFile(String outputFile, String contents)
  2. writeToFile(String outputFilePath, String generated)
  3. writeUTF8(DataOutput buf, String value)
  4. writeUTF8(final String filePath, final String content)
  5. writeUTF8(OutputStream out, String str)
  6. writeUTF8WithLength(OutputStream out, String str)