Java ByteBuffer Read readFixedLengthString(ByteBuffer buf, int length)

Here you can find the source of readFixedLengthString(ByteBuffer buf, int length)

Description

read Fixed Length String

License

Open Source License

Declaration

public static String readFixedLengthString(ByteBuffer buf, int length) throws IOException 

Method Source Code

//package com.java2s;
/**/*w ww .j  a v a  2s .  c  om*/
 * Project: ${puma-common.aid}
 * <p/>
 * File Created at 2012-6-6 $Id$
 * <p/>
 * Copyright 2010 dianping.com. All rights reserved.
 * <p/>
 * This software is the confidential and proprietary information of Dianping
 * Company. ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with dianping.com.
 */

import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    public static String readFixedLengthString(ByteBuffer buf, int length) throws IOException {
        return new String(readBytes(buf, length));
    }

    public static String readFixedLengthString(ByteBuffer buf, int length, String encoding) throws IOException {
        return new String(readBytes(buf, length), encoding);
    }

    public static byte[] readBytes(ByteBuffer buf, int length) {
        if ((buf.position() + length) <= buf.limit()) {
            byte[] r = new byte[length];
            for (int i = 0; i < length; i++) {
                r[i] = buf.get();
            }
            return r;
        }
        return null;
    }
}

Related

  1. readCInt(ByteBuffer buffer)
  2. readCString(ByteBuffer buf, int len)
  3. readDERString(ByteBuffer buf)
  4. readDword(ByteBuffer buffer)
  5. readFileToByteBuffer(File file)
  6. readFixedLengthString(ByteBuffer byteBuffer, int size)
  7. readFixedPoint88(ByteBuffer bb)
  8. readFourByteInt(ByteBuffer buffer, int start)
  9. readFrom(File file, ByteBuffer buffer)