Java ByteBuffer to String readString(ByteBuffer logBuf)

Here you can find the source of readString(ByteBuffer logBuf)

Description

Read a string from the log.

License

Open Source License

Declaration

public static String readString(ByteBuffer logBuf) 

Method Source Code


//package com.java2s;
/*-//from  w  w w. ja  v a2s  .co m
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2007 Oracle.  All rights reserved.
 *
 * $Id: LogUtils.java,v 1.50.2.1 2007/02/01 14:49:47 cwl Exp $
 */

import java.nio.ByteBuffer;

public class Main {
    private static final boolean DEBUG = false;
    public static final byte[] ZERO_LENGTH_BYTE_ARRAY = new byte[0];

    /**
     * Read a string from the log. The size is stored first as an integer.
     */
    public static String readString(ByteBuffer logBuf) {
        return new String(readByteArray(logBuf));
    }

    /**
     * Read a byte array from the log. The size is stored first as an integer.
     */
    public static byte[] readByteArray(ByteBuffer logBuf) {
        int size = readInt(logBuf); // how long is it?
        if (DEBUG) {
            System.out.println("pos = " + logBuf.position() + " byteArray is " + size + " on read");
        }
        if (size == 0) {
            return ZERO_LENGTH_BYTE_ARRAY;
        }
        byte[] b = new byte[size];
        logBuf.get(b); // read it out
        return b;
    }

    /**
     * Read a int from the log.
     */
    public static int readInt(ByteBuffer logBuf) {
        int ret = (logBuf.get() & 0xFF) << 0;
        ret += (logBuf.get() & 0xFF) << 8;
        ret += (logBuf.get() & 0xFF) << 16;
        ret += (logBuf.get() & 0xFF) << 24;
        return ret;
    }
}

Related

  1. readString(ByteBuffer buff, int len)
  2. readString(ByteBuffer buffer)
  3. readString(ByteBuffer buffer)
  4. readString(ByteBuffer byteBuffer)
  5. readString(ByteBuffer byteBuffer)
  6. readString(final ByteBuffer buffer, final String encoding)
  7. string(ByteBuffer b, Charset charset)
  8. string(ByteBuffer buffer)
  9. string(ByteBuffer buffer)