Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.monitor.baseservice.utils.XCodeUtil.java

public static byte[] longToByteArray(long value) {
    ByteBuffer bb = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putLong(value);/*from w  w w. j a  va 2s.com*/
    return bb.array();
}

From source file:com.kentdisplays.synccardboarddemo.Page.java

/**
 * Decodes an input stream from a file into Path objects that can be used to draw the page.
 *///from   ww w  .j  a v  a2 s.  com
private static ArrayList<Path> pathsFromSamplePageInputStream(InputStream inputStream) {
    ArrayList<Path> paths = new ArrayList<Path>();
    try {
        // Retrieve a byte array from the sample page.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) > -1) {
            baos.write(buffer, 0, len);
        }
        baos.flush();
        byte[] byteArray = baos.toByteArray();

        // Decode the path data from the sample page.
        String rawString = EncodingUtils.getAsciiString(byteArray);
        int startIndex = rawString.indexOf("<</Length 13 0 R/Filter /FlateDecode>>") + 46;
        int endIndex = rawString.indexOf("endstream", startIndex) - 1;
        byte[] flateEncodedByteArray = Arrays.copyOfRange(byteArray, startIndex, endIndex);
        net.sf.andpdf.nio.ByteBuffer flateEncodedBuffer = net.sf.andpdf.nio.ByteBuffer
                .NEW(flateEncodedByteArray);
        net.sf.andpdf.nio.ByteBuffer decodedBuffer = FlateDecode.decode(null, flateEncodedBuffer, null);
        String decodedString = new String(decodedBuffer.array(), "UTF-8");

        // Break every line of the decoded string into usable objects.
        String[] stringArray = decodedString.split("\\r?\\n");
        for (int i = 0; i < stringArray.length; i++) {
            String[] components = stringArray[i].split(" ");
            Path path = new Path();
            path.y1 = Float.valueOf(components[2]);
            path.x1 = Float.valueOf(components[3]);
            path.y2 = Float.valueOf(components[5]);
            path.x2 = Float.valueOf(components[6]);
            path.width = Float.valueOf(components[0]);
            paths.add(path);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return paths;
}

From source file:com.bittorrent.mpetazzoni.bencode.BDecoder.java

/**
 * Decode a B-encoded byte buffer./*from w  w w.j  av a 2 s .c  om*/
 *
 * <p>
 * Automatically instantiates a new BDecoder for the provided buffer and
 * decodes its root member.
 * </p>
 *
 * @param data The {@link ByteBuffer} to read from.
 */
public static BEValue bdecode(ByteBuffer data) throws IOException {
    return BDecoder.bdecode(new AutoCloseInputStream(new ByteArrayInputStream(data.array())));
}

From source file:com.datatorrent.contrib.hdht.HDHTDynamicPartitioningTest.java

static Slice getLongByteArray(long key) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.putLong(key);/*from w  w  w . j  a va 2s  .  c  o  m*/
    return new Slice(bb.array());
}

From source file:com.scf.utils.UUIDUtilies.java

protected static String base64Uuid(UUID uuid) {

    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());

    return Base64.encodeBase64URLSafeString(bb.array());
}

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.");
    }// ww w . j  a  v  a2 s . c  om
    return compareBytesUnsigned(a.array(), a.arrayOffset() + a.position(), b.array(),
            b.arrayOffset() + b.position(), a.remaining());
}

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;/* w  ww . j av a 2s.c om*/
}

From source file:kilim.http.HttpRequest.java

public static int parseChunkSize(ByteBuffer buffer, int start, int end) throws IOException {
    byte[] bufa = buffer.array();
    int size = 0;
    for (int i = start; i < end; i++) {
        byte b = bufa[i];
        if (b >= b0 && b <= b9) {
            size = size * 16 + (b - b0);
        } else if (b >= ba && b <= bf) {
            size = size * 16 + ((b - ba) + 10);
        } else if (b >= bA && b <= bF) {
            size = size * 16 + ((b - bA) + 10);
        } else if (b == CR || b == SEMI) {
            // SEMI-colon starts a chunk extension. We ignore extensions currently.
            break;
        } else {/*w w  w  . j a v a2s . c om*/
            throw new IOException("Error parsing chunk size; unexpected char " + b + " at offset " + i);
        }
    }
    return size;
}

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

public static ByteBuffer readByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length < 0) {
        throw new IOException("Corrupt (negative) value length encountered");
    }//ww  w .  j av  a 2 s  . co  m

    ByteBuffer bb = ByteBuffer.allocate(length);
    if (length > 0) {
        in.readFully(bb.array(), bb.position(), bb.remaining());
    }
    return bb;
}

From source file:ec.edu.chyc.manejopersonal.util.ServerUtils.java

/***
 * Devuelve un UUID generado y codificado en Base64
 *
 * @return Uuid codificado en Base64/* w  w  w. j  a  v a 2 s. c  om*/
 */
public static String generateB64Uuid() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer uuidBytes = ByteBuffer.wrap(new byte[16]);
    uuidBytes.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    String asB64 = Base64.getEncoder().encodeToString(uuidBytes.array());
    return asB64;
}