Java UTF8 File Read readUtf8String(DataInputStream in)

Here you can find the source of readUtf8String(DataInputStream in)

Description

Reads a UTF-8 encoded string from the buffer.

License

Open Source License

Parameter

Parameter Description
buf The buffer.

Return

The string.

Declaration

public static String readUtf8String(DataInputStream in) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.DataInputStream;

import java.io.IOException;
import java.nio.charset.Charset;

public class Main {
    /**/* w ww.  j av a 2s  .  co m*/
     * The UTF-8 character set.
     */
    private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");

    /**
     * Reads a UTF-8 encoded string from the buffer.
     * @param buf The buffer.
     * @return The string.
     */
    public static String readUtf8String(DataInputStream in) throws IOException {
        int len = in.readUnsignedShort();

        byte[] bytes = new byte[len];
        in.readFully(bytes);

        return new String(bytes, CHARSET_UTF8);
    }
}

Related

  1. readResourceUtf8(Class contextClass, String filename)
  2. readStringFromUTF8Stream(InputStream is)
  3. readStringUTF16LE(byte[] var0, int var1, int var2)
  4. readTextFile(File inputFile)
  5. readUTF8(DataInput buf)
  6. readUTFByteArrayFromDataInput(final DataInput in)
  7. utf8Reader(InputStream in)