Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

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

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

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 .  jav a  2 s. 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:io.alicorn.server.http.LoginEndpoint.java

public static String hash(char[] chars) {
    //Parse chars into bytes for hashing.
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = charset.encode(charBuffer);
    byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());

    //Clear temporary arrays of any data.
    Arrays.fill(charBuffer.array(), '\u0000');
    Arrays.fill(byteBuffer.array(), (byte) 0);

    //Generate the SHA-256 hash.
    String hash = hash(bytes);//from w  w w  .j  a va  2  s .c om

    //Clear remaining arrays of any data.
    Arrays.fill(bytes, (byte) 0);

    return hash;
}

From source file:de.csdev.ebus.utils.EBusUtils.java

/**
 * Generates a string hex dump from a ByteBuffer
 *
 * @param data The source/*from ww  w  . ja  v a  2s  .co  m*/
 * @return The StringBuilder with hex dump
 */
static public StringBuilder toHexDumpString(ByteBuffer data) {

    int size = 0;
    if (data.position() == 0) {
        size = data.limit();
    } else {
        size = data.position();
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < size; i++) {
        byte c = data.get(i);
        if (i > 0) {
            sb.append(' ');
        }
        sb.append(toHexDumpString(c));
    }
    return sb;
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static ByteBuffer clone(ByteBuffer o) {
    assert o != null;

    if (o.remaining() == 0)
        return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY);

    ByteBuffer clone = ByteBuffer.allocate(o.remaining());

    if (o.isDirect()) {
        for (int i = o.position(); i < o.limit(); i++) {
            clone.put(o.get(i));/*  w  w  w . j a  va  2  s.co m*/
        }
        clone.flip();
    } else {
        System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining());
    }

    return clone;
}

From source file:gobblin.util.io.StreamUtils.java

/**
 * Reads the full contents of a ByteBuffer and writes them to an OutputStream. The ByteBuffer is
 * consumed by this operation; eg in.remaining() will be 0 after it completes successfully.
 * @param in  ByteBuffer to write into the OutputStream
 * @param out Destination stream//  ww  w. j  ava 2 s  .  c  o m
 * @throws IOException If there is an error writing into the OutputStream
 */
public static void byteBufferToOutputStream(ByteBuffer in, OutputStream out) throws IOException {
    final int BUF_SIZE = 8192;

    if (in.hasArray()) {
        out.write(in.array(), in.arrayOffset() + in.position(), in.remaining());
    } else {
        final byte[] b = new byte[Math.min(in.remaining(), BUF_SIZE)];
        while (in.remaining() > 0) {
            int bytesToRead = Math.min(in.remaining(), BUF_SIZE);
            in.get(b, 0, bytesToRead);

            out.write(b, 0, bytesToRead);
        }
    }
}

From source file:android.wulongdao.thirdparty.mime.HttpMultipart.java

private static ByteArrayBuffer encode(final Charset charset, final String string) {
    ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
    ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;/*  ww  w .j  av  a 2 s . c om*/
}

From source file:com.googlecode.jcimd.TextMessageUserDataFactory.java

private static byte[] encodeAs(Charset charset, String textMessage) {
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer byteBuffer = ByteBuffer
            .allocate(textMessage.length() * (int) Math.ceil(encoder.maxBytesPerChar()));
    encoder.encode(CharBuffer.wrap(textMessage), byteBuffer, true);
    byte[] bytes = new byte[byteBuffer.position()];
    byteBuffer.flip();/* ww  w .  jav a 2 s  .com*/
    byteBuffer.get(bytes);
    return bytes;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static ByteBuffer readShortByteArray(DataInput in) throws IOException {
    int length = readShortLength(in);
    ByteBuffer bb = ByteBuffer.allocate(length);
    in.readFully(bb.array(), bb.position(), bb.remaining());
    return bb;/*from   w  ww  .  j  a v a 2  s .c  om*/
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static String bytesToHex(ByteBuffer bytes) {
    StringBuilder sb = new StringBuilder();
    for (int i = bytes.position() + bytes.arrayOffset(); i < bytes.limit() + bytes.arrayOffset(); i++) {
        int bint = bytes.array()[i] & 0xff;
        if (bint <= 0xF)
            // toHexString does not 0 pad its results.
            sb.append("0");
        sb.append(Integer.toHexString(bint));
    }// w w  w  .  j a  v a  2  s . c om
    return sb.toString();
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static int compareBytesUnsigned(ByteBuffer a, ByteBuffer b) {
    if (a.remaining() != b.remaining()) {
        throw new RuntimeException(
                "Cannot compare ByteBuffers that have a different number of remaining elements.");
    }/*from   ww  w .  j  av a 2 s  .co m*/
    return compareBytesUnsigned(a.array(), a.arrayOffset() + a.position(), b.array(),
            b.arrayOffset() + b.position(), a.remaining());
}