Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

In this page you can find the example usage for java.nio ByteBuffer get.

Prototype

public abstract byte get();

Source Link

Document

Returns the byte at the current position and increases the position by 1.

Usage

From source file:Main.java

/**
 * Merge long value from two byte buffer. First bytes of long will be extracted from first byte buffer and second from second one.
 * How many bytes will be read from first buffer determines based on <code>buffer.remaining()</code> value
 * /*from  w  w  w.j a  v  a  2  s.c  o  m*/
 * @param buffer
 *          to read first part of value
 * @param buffer1
 *          to read second part of value
 * @return merged value
 */
public static long mergeLongFromBuffers(ByteBuffer buffer, ByteBuffer buffer1) {
    long result = 0;
    int remaining = buffer.remaining();
    for (int i = 0; i < remaining; ++i) {
        result = result | (MASK & buffer.get());
        result = result << SIZE_OF_BYTE_IN_BITS;
    }
    for (int i = 0; i < SIZE_OF_LONG - remaining - 1; ++i) {
        result = result | (MASK & buffer1.get());
        result = result << SIZE_OF_BYTE_IN_BITS;
    }
    result = result | (MASK & buffer1.get());
    return result;
}

From source file:Main.java

private static String urlencode(final String content, final Charset charset, final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }/*from  ww  w  .ja v a  2 s. c o m*/
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}

From source file:Main.java

public static byte[] getByteArray(final ByteBuffer buff) {
    if (buff == null) {
        return null;
    }//from w w  w .ja v  a  2s .  co  m
    buff.clear();
    final byte[] inds = new byte[buff.limit()];
    for (int x = 0; x < inds.length; x++) {
        inds[x] = buff.get();
    }
    return inds;
}

From source file:com.sm.message.Header.java

public static Header toHeader(byte[] data) {
    ByteBuffer buf = ByteBuffer.wrap(data);
    byte release = buf.get();
    long version = buf.getLong();
    long node = buf.getLong();
    byte[] n = new byte[data.length - FIRST];
    buf.get(n);//from   w  ww. j a  va  2  s .  co m
    try {
        String name = new String(n, "UTF-8");
        return new Header(name, version, release, node);
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex.getMessage());
    }

}

From source file:com.stratagis.geoevent.adapter.nmeaplus.NmeaPlusInboundAdapter.java

private static List<byte[]> index(ByteBuffer in) {

    List<byte[]> messages = new ArrayList<byte[]>();
    for (int i = -1; in.hasRemaining();) {
        byte b = in.get();
        if (b == ((byte) '$')) // bom
        {/*from  w  w w.j av a2s  .  c  o m*/
            i = in.position();
            in.mark();
        } else if (b == ((byte) '\r') || b == ((byte) '\n')) // eom
        {
            if (i != -1) {
                byte[] message = new byte[in.position() - 1 - i];
                System.arraycopy(in.array(), i, message, 0, message.length);
                messages.add(message);
            }
            i = -1;
            in.mark();
        } else if (messages.isEmpty() && i == -1)
            in.mark();
    }
    return messages;
}

From source file:com.twitter.distributedlog.DLSN.java

@VisibleForTesting
static DLSN deserialize0(String dlsn) {
    byte[] data = Base64.decodeBase64(dlsn);
    ByteBuffer bb = ByteBuffer.wrap(data);
    byte version = bb.get();
    if (VERSION0 != version || VERSION0_LEN != data.length) {
        throw new IllegalArgumentException("Invalid DLSN " + dlsn);
    }//  w ww  .j a  v a 2 s .c o m
    return new DLSN(bb.getLong(), bb.getLong(), bb.getLong());
}

From source file:com.twitter.distributedlog.DLSN.java

/**
 * Deserialize the DLSN from bytes array.
 *
 * @param data/*from  w  ww.j  a  v  a2 s . c  o m*/
 *          serialized bytes
 * @return dlsn
 */
public static DLSN deserializeBytes(byte[] data) {
    ByteBuffer bb = ByteBuffer.wrap(data);
    byte version = bb.get();
    if (VERSION0 == version) {
        if (VERSION0_LEN != data.length) {
            throw new IllegalArgumentException("Invalid version zero DLSN " + Hex.encodeHexString(data));
        }
    } else if (VERSION1 == version) {
        if (VERSION1_LEN != data.length) {
            throw new IllegalArgumentException("Invalid version one DLSN " + Hex.encodeHexString(data));
        }
    } else {
        throw new IllegalArgumentException(
                "Invalid DLSN : version = " + version + ", " + Hex.encodeHexString(data));
    }
    return new DLSN(bb.getLong(), bb.getLong(), bb.getLong());
}

From source file:Main.java

/**
 * Finds next Nth MPEG bitstream marker 0x000001xx and returns the data that
 * preceeds it as a ByteBuffer slice//from  w  ww  .  ja v  a  2 s . com
 * 
 * Segment byte order is always little endian
 * 
 * @param buf
 * @return
 */
public static final ByteBuffer gotoMarker(ByteBuffer buf, int n, int mmin, int mmax) {
    if (!buf.hasRemaining())
        return null;

    int from = buf.position();
    ByteBuffer result = buf.slice();
    result.order(ByteOrder.BIG_ENDIAN);

    int val = 0xffffffff;
    while (buf.hasRemaining()) {
        val = (val << 8) | (buf.get() & 0xff);
        if (val >= mmin && val <= mmax) {
            if (n == 0) {
                buf.position(buf.position() - 4);
                result.limit(buf.position() - from);
                break;
            }
            --n;
        }
    }
    return result;
}

From source file:com.tera.common.util.ConsolePrinter.java

/**
 * Convert data from given ByteBuffer to hex
 * //from w  w  w .  j  a  va2 s  .com
 * @param data
 * @return hex
 */
public static String toHex(ByteBuffer data) {
    StringBuilder result = new StringBuilder();
    int counter = 0;
    int b;
    while (data.hasRemaining()) {
        if (counter % 16 == 0)
            result.append(String.format("%04X: ", counter));

        b = data.get() & 0xff;
        result.append(String.format("%02X ", b));

        counter++;
        if (counter % 16 == 0) {
            result.append("  ");
            toText(data, result, 16);
            result.append("\n");
        }
    }
    int rest = counter % 16;
    if (rest > 0) {
        for (int i = 0; i < 17 - rest; i++) {
            result.append("   ");
        }
        toText(data, result, rest);
    }
    return result.toString();
}

From source file:io.mycat.util.ByteBufferUtil.java

public static int readShortLength(ByteBuffer bb) {
    int length = (bb.get() & 0xFF) << 8;
    return length | (bb.get() & 0xFF);
}