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.openteach.diamond.network.waverider.slave.SlaveState.java

public static SlaveState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {/*from w ww.j ava  2  s . co m*/
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (SlaveState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        if (oin != null) {
            try {
                oin.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }
}

From source file:Main.java

public static byte[] toByteArrayNew(Bitmap source) {
    //int size = source.getRowBytes() * source.getHeight();
    int size = source.getByteCount();
    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    source.copyPixelsToBuffer(byteBuffer);
    byteBuffer.rewind();/*from   w  w w. j av  a 2  s . c  om*/
    byte[] b = byteBuffer.array();
    return b;
}

From source file:com.glaf.core.util.BinaryUtils.java

/**
 * Returns a copy of all the bytes from the given <code>ByteBuffer</code>,
 * from the beginning to the buffer's limit; or null if the input is null.
 * <p>//  ww  w. j av a 2s  .  c o m
 * The internal states of the given byte buffer will be restored when this
 * method completes execution.
 * <p>
 * When handling <code>ByteBuffer</code> from user's input, it's typical to
 * call the {@link #copyBytesFrom(ByteBuffer)} instead of
 * {@link #copyAllBytesFrom(ByteBuffer)} so as to account for the position
 * of the input <code>ByteBuffer</code>. The opposite is typically true,
 * however, when handling <code>ByteBuffer</code> from withint the
 * unmarshallers of the low-level clients.
 */
public static byte[] copyAllBytesFrom(ByteBuffer bb) {
    if (bb == null)
        return null;
    if (bb.hasArray())
        return Arrays.copyOf(bb.array(), bb.limit());
    bb.mark();
    // the default ByteBuffer#mark() and reset() won't work, as the
    // rewind would discard the mark position
    final int marked = bb.position();
    try {
        byte[] dst = new byte[bb.rewind().remaining()];
        bb.get(dst);
        return dst;
    } finally {
        bb.position(marked);
    }
}

From source file:com.glaf.core.util.BinaryUtils.java

/**
 * Returns a copy of the bytes from the given <code>ByteBuffer</code>,
 * ranging from the the buffer's current position to the buffer's limit; or
 * null if the input is null./* w ww. j  a v a 2s  . c o m*/
 * <p>
 * The internal states of the given byte buffer will be restored when this
 * method completes execution.
 * <p>
 * When handling <code>ByteBuffer</code> from user's input, it's typical to
 * call the {@link #copyBytesFrom(ByteBuffer)} instead of
 * {@link #copyAllBytesFrom(ByteBuffer)} so as to account for the position
 * of the input <code>ByteBuffer</code>. The opposite is typically true,
 * however, when handling <code>ByteBuffer</code> from withint the
 * unmarshallers of the low-level clients.
 */
public static byte[] copyBytesFrom(ByteBuffer bb) {
    if (bb == null)
        return null;
    if (bb.hasArray())
        return Arrays.copyOfRange(bb.array(), bb.position(), bb.limit());
    bb.mark();
    try {
        byte[] dst = new byte[bb.remaining()];
        bb.get(dst);
        return dst;
    } finally {
        bb.reset();
    }
}

From source file:com.nestedbird.util.UUIDConverter.java

/**
 * Turns a UUID in string format to a Base64 encoded version
 *
 * @param uuidString String representation of the uuid
 * @return base64 encoded version of the uuid
 * @throws IllegalArgumentException String must be a valid uuid
 * @throws NullPointerException     String cannot be null
 *//* ww  w .  ja  v  a 2 s.  c  o m*/
public static String toBase64(final String uuidString) {
    if (uuidString == null)
        throw new NullPointerException("String cannot be null");
    if (!isUUID(uuidString))
        throw new IllegalArgumentException("string must be a valid uuid");

    final UUID uuid = UUID.fromString(uuidString);
    final ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());
    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:Main.java

public static String readString(String filePath) {
    File file = new File(filePath);
    if (!file.exists())
        return null;

    FileInputStream fileInput = null;
    FileChannel channel = null;//  www .  j av a  2s .  com
    try {
        fileInput = new FileInputStream(filePath);
        channel = fileInput.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
        channel.read(buffer);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer.array());
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
    } finally {

        if (fileInput != null) {
            try {
                fileInput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<ByteBuffer> mergeAdjacentBuffers(List<ByteBuffer> paramList) {
    ArrayList localArrayList = new ArrayList(paramList.size());
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext()) {
        ByteBuffer localByteBuffer1 = (ByteBuffer) localIterator.next();
        int i = -1 + localArrayList.size();
        if ((i >= 0) && (localByteBuffer1.hasArray()) && (((ByteBuffer) localArrayList.get(i)).hasArray())
                && (localByteBuffer1.array() == ((ByteBuffer) localArrayList.get(i)).array())
                && (((ByteBuffer) localArrayList.get(i)).arrayOffset()
                        + ((ByteBuffer) localArrayList.get(i)).limit() == localByteBuffer1.arrayOffset())) {
            ByteBuffer localByteBuffer3 = (ByteBuffer) localArrayList.remove(i);
            localArrayList.add(ByteBuffer.wrap(localByteBuffer1.array(), localByteBuffer3.arrayOffset(),
                    localByteBuffer3.limit() + localByteBuffer1.limit()).slice());
        } else if ((i >= 0) && ((localByteBuffer1 instanceof MappedByteBuffer))
                && ((localArrayList.get(i) instanceof MappedByteBuffer))
                && (((ByteBuffer) localArrayList.get(i))
                        .limit() == ((ByteBuffer) localArrayList.get(i)).capacity()
                                - localByteBuffer1.capacity())) {
            ByteBuffer localByteBuffer2 = (ByteBuffer) localArrayList.get(i);
            localByteBuffer2.limit(localByteBuffer1.limit() + localByteBuffer2.limit());
        } else {//from   ww  w. j a va2  s  .c  o m
            localByteBuffer1.reset();
            localArrayList.add(localByteBuffer1);
        }
    }
    return localArrayList;
}

From source file:com.cnaude.mutemanager.UUIDFetcher.java

public static byte[] toBytes(UUID uuid) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
    byteBuffer.putLong(uuid.getMostSignificantBits());
    byteBuffer.putLong(uuid.getLeastSignificantBits());
    return byteBuffer.array();
}

From source file:cn.ctyun.amazonaws.util.StringUtils.java

/**
 * Base64 encodes the data in the specified byte buffer and returns it as a
 * base64 encoded string./*from   www  .  j  a  v a  2  s.c o m*/
 *
 * @param byteBuffer
 *            The data to base64 encode and return as a string.
 *
 * @return The base64 encoded contents of the specified byte buffer.
 */
public static String fromByteBuffer(ByteBuffer byteBuffer) {
    byte[] encodedBytes = null;
    if (byteBuffer.hasArray()) {
        encodedBytes = Base64.encodeBase64(byteBuffer.array());
    } else {
        byte[] binaryData = new byte[byteBuffer.limit()];
        byteBuffer.get(binaryData);
        encodedBytes = Base64.encodeBase64(binaryData);
    }
    return new String(encodedBytes);
}

From source file:com.willetinc.hadoop.mapreduce.dynamodb.AttributeValueIOUtils.java

public static void write(Types type, AttributeValue value, DataOutput out) throws IOException {
    switch (type) {
    case STRING://  w  w  w. j a  v a2s  .com
        Text.writeString(out, value.getS());
        break;
    case NUMBER:
        Text.writeString(out, value.getN());
        break;
    case BINARY: {
        WritableUtils.writeCompressedByteArray(out, value.getB().array());
        break;
    }
    case STRING_SET: {
        List<String> values = value.getSS();
        out.writeInt(values.size());
        for (String s : values) {
            Text.writeString(out, s);
        }
        break;
    }
    case NUMBER_SET: {
        List<String> values = value.getNS();
        out.writeInt(values.size());
        for (String s : values) {
            Text.writeString(out, s);
        }
        break;
    }
    case BINARY_SET: {
        List<ByteBuffer> values = value.getBS();
        out.writeInt(values.size());
        for (ByteBuffer buf : values) {
            WritableUtils.writeCompressedByteArray(out, buf.array());
        }
    }
    }
}