Java ByteBuffer Read readCString(ByteBuffer buf, int len)

Here you can find the source of readCString(ByteBuffer buf, int len)

Description

read C String

License

Open Source License

Declaration

public static String readCString(ByteBuffer buf, int len) 

Method Source Code


//package com.java2s;
/* /*w  ww .j  av  a 2s. com*/
 * Copyright (c) 2012, Massachusetts Institute of Technology
 * Released under the BSD 2-Clause License
 * http://opensource.org/licenses/BSD-2-Clause 
 */

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Main {
    public static String readCString(ByteBuffer buf, int len) {
        return readCString(buf, new byte[len], len);
    }

    public static String readCString(ByteBuffer buf, byte[] arr, int len) {
        buf.get(arr, 0, len);
        int i;

        for (i = 0; i < len; i++) {
            if (arr[i] == 0) {
                break;
            }
        }

        try {
            return new String(arr, 0, len, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return "";
        }
    }
}

Related

  1. readByteBuffer(ByteBuffer buf)
  2. readByteBufferFromFile(String filepath)
  3. readChars(ByteBuffer bb, int length)
  4. readCharsUTF8(ByteBuffer bb, int length)
  5. readCInt(ByteBuffer buffer)
  6. readDERString(ByteBuffer buf)
  7. readDword(ByteBuffer buffer)
  8. readFileToByteBuffer(File file)
  9. readFixedLengthString(ByteBuffer buf, int length)