Java File Read via ByteBuffer readFlashString(DataInputStream s)

Here you can find the source of readFlashString(DataInputStream s)

Description

read Flash String

License

Open Source License

Declaration

public static String readFlashString(DataInputStream s) 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.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

public class Main {
    public static CharsetDecoder stringDecoder = Charset.forName("UTF-8").newDecoder();

    public static String readFlashString(DataInputStream s) throws IOException {
        // Get the length.
        int len = s.readUnsignedShort();
        if (len == 0)
            return null;

        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++)
            bytes[i] = s.readByte();//w ww . j a v  a 2  s .  c  o  m

        return stringDecoder.decode(ByteBuffer.wrap(bytes)).toString();
    }
}

Related

  1. readFileIntoString(String path)
  2. readFileNIO(String path, StringBuilder builder)
  3. readFileToBuffer(java.io.File file)
  4. readFileToString(String path)
  5. readFileToString(String path)
  6. readFloat(BufferedReader br)
  7. ReadFloat(InputStream is)
  8. readFromBuffer(byte[] buffer, int start, int end)
  9. readFromFile(Path path)