Java UTF8 File Read readUTFByteArrayFromDataInput(final DataInput in)

Here you can find the source of readUTFByteArrayFromDataInput(final DataInput in)

Description

Reads a string from the given DataInput.

License

BSD License

Parameter

Parameter Description
in the DataInput from which the string will be read

Exception

Parameter Description
IOException an IO problem.

Return

The string

Declaration

public static String readUTFByteArrayFromDataInput(final DataInput in) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the terms of the New BSD License. Please see associated LICENSE file for terms.

import java.io.DataInput;

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

public class Main {
    /**//  w  w w . jav  a2 s  . c om
     * Reads a string from the given DataInput. This method assumes that the string is stored as an integer, indicating
     * number of bytes of the string, followed by a byte array which is the string itself, encoded in UTF-8. The method
     * {@link #writeStringAsUTFByteArrayToDataOutput(DataOutput, String)} writes a string into a DataOutput in this way.
     * 
     * @param in the DataInput from which the string will be read
     * @return The string
     * @throws IOException an IO problem.
     */
    public static String readUTFByteArrayFromDataInput(final DataInput in) throws IOException {
        int length = in.readInt();
        byte[] byteArray = new byte[length];
        in.readFully(byteArray);
        return new String(byteArray, StandardCharsets.UTF_8);
    }
}

Related

  1. readStringFromUTF8Stream(InputStream is)
  2. readStringUTF16LE(byte[] var0, int var1, int var2)
  3. readTextFile(File inputFile)
  4. readUTF8(DataInput buf)
  5. readUtf8String(DataInputStream in)
  6. utf8Reader(InputStream in)