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:android.framework.util.jar.Manifest.java

private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder,
        ByteBuffer bBuf) throws IOException {
    String nameString = name.getName();
    os.write(nameString.getBytes(Charsets.US_ASCII));
    os.write(VALUE_SEPARATOR);/*from   www.ja  v  a  2 s.c  o  m*/

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);

    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}

From source file:de.csdev.ebus.command.EBusCommandUtils.java

/**
 * Builds an escaped master telegram part or if slaveData is used a complete telegram incl. master ACK and SYN
 *
 * @param source//from w  ww  .j  a v  a 2  s . c  om
 * @param target
 * @param command
 * @param masterData
 * @return
 * @throws EBusTypeException
 */
public static ByteBuffer buildPartMasterTelegram(byte source, byte target, byte[] command, byte[] masterData)
        throws EBusTypeException {

    ByteBuffer buf = ByteBuffer.allocate(50);

    buf.put(source); // QQ - Source
    buf.put(target); // ZZ - Target
    buf.put(command); // PB SB - Command
    buf.put((byte) masterData.length); // NN - Length, will be set later

    // add the escaped bytes
    for (byte b : masterData) {
        buf.put(escapeSymbol(b));
    }

    // calculate crc
    byte crc8 = EBusUtils.crc8(buf.array(), buf.position());

    buf.put(escapeSymbol(crc8));

    // set limit and reset position
    buf.limit(buf.position());
    buf.position(0);

    return buf;
}

From source file:de.csdev.ebus.command.EBusCommandUtils.java

/**
 * Build an escaped telegram part for a slave answer
 *
 * @param slaveData/*from   w ww.ja  v  a  2  s.co m*/
 * @return
 * @throws EBusTypeException
 */
public static ByteBuffer buildPartSlave(byte[] slaveData) throws EBusTypeException {

    ByteBuffer buf = ByteBuffer.allocate(50);

    buf.put(EBusConsts.ACK_OK); // ACK

    // if payload available
    if (slaveData != null && slaveData.length > 0) {
        buf.put((byte) slaveData.length); // NN - Length

        // add the escaped bytes
        for (byte b : slaveData) {
            buf.put(escapeSymbol(b));
        }

        // calculate crc
        byte crc8 = EBusUtils.crc8(buf.array(), buf.position());

        // add the crc, maybe escaped
        buf.put(escapeSymbol(crc8));
    } else {
        // only set len = 0
        buf.put((byte) 0x00); // NN - Length
    }

    // set limit and reset position
    buf.limit(buf.position());
    buf.position(0);

    return buf;
}

From source file:com.blm.orc.ReaderImpl.java

/**
 * Ensure this is an ORC file to prevent users from trying to read text
 * files or RC files as ORC files./* w w  w .  jav  a2 s  .c  o m*/
 * @param in the file being read
 * @param path the filename for error messages
 * @param psLen the postscript length
 * @param buffer the tail of the file
 * @throws IOException
 */
static void ensureOrcFooter(FSDataInputStream in, Path path, int psLen, ByteBuffer buffer) throws IOException {
    int len = OrcFile.MAGIC.length();
    if (psLen < len + 1) {
        throw new IOException("Malformed ORC file " + path + ". Invalid postscript length " + psLen);
    }
    int offset = buffer.arrayOffset() + buffer.position() + buffer.limit() - 1 - len;
    byte[] array = buffer.array();
    // now look for the magic string at the end of the postscript.
    if (!Text.decode(array, offset, len).equals(OrcFile.MAGIC)) {
        // If it isn't there, this may be the 0.11.0 version of ORC.
        // Read the first 3 bytes of the file to check for the header
        in.seek(0);
        byte[] header = new byte[len];
        in.readFully(header, 0, len);
        // if it isn't there, this isn't an ORC file
        if (!Text.decode(header, 0, len).equals(OrcFile.MAGIC)) {
            throw new IOException("Malformed ORC file " + path + ". Invalid postscript.");
        }
    }
}

From source file:com.github.seqware.queryengine.plugins.runners.hbasemr.MRHBasePluginRunner.java

public static String[] serializeParametersToString(Object[] parameters, PluginInterface mapReducePlugin,
        byte[][] sSet, byte[] dSet) {
    int num_guaranteed_parameters = 6;
    String[] str_params = new String[num_guaranteed_parameters];
    byte[] ext_serials = SerializationUtils.serialize(parameters);
    byte[] int_serials = SerializationUtils.serialize(mapReducePlugin.getInternalParameters());
    str_params[EXTERNAL_PARAMETERS] = Base64.encodeBase64String(ext_serials);
    str_params[INTERNAL_PARAMETERS] = Base64.encodeBase64String(int_serials);
    ByteBuffer bBuffer = ByteBuffer.allocate(1024 * 1024); // one MB should be enough for now
    bBuffer.putInt(sSet.length);/*w  ww . j a v  a 2s  . c  o  m*/
    for (byte[] arr : sSet) {
        bBuffer.putInt(arr.length);
        bBuffer.put(arr);
    }
    str_params[NUM_AND_SOURCE_FEATURE_SETS] = Base64.encodeBase64String(bBuffer.array());
    str_params[DESTINATION_FEATURE_SET] = Base64.encodeBase64String(dSet);
    str_params[SETTINGS_MAP] = Base64
            .encodeBase64String(SerializationUtils.serialize(new Object[] { Constants.getSETTINGS_MAP() }));
    str_params[PLUGIN_CLASS] = Base64
            .encodeBase64String(SerializationUtils.serialize(mapReducePlugin.getClass()));

    return str_params;
}

From source file:Main.java

/**
 * //from w w  w .ja v  a  2 s.c  om
 * @return Bitmap's RGBA byte array
 */
public static byte[] getImageRGBA(Bitmap inputBitmap) {
    Config config = inputBitmap.getConfig();
    ByteBuffer buffer;

    Bitmap bitmap;
    /**
     * if bitmap size is not 32*32 create scaled bitmap
     */

    if (inputBitmap.getWidth() != 32 || inputBitmap.getHeight() != 32) {
        Log.d(TAG, "bitmap resized to 32x32");
        bitmap = Bitmap.createScaledBitmap(inputBitmap, 32, 32, false);
    } else {
        bitmap = inputBitmap;
    }
    /**
     * if bitmap is not ARGB_8888 format, copy bitmap with ARGB_8888 format
     */
    if (!config.equals(Bitmap.Config.ARGB_8888)) {
        Bitmap bitmapARBG = bitmap.copy(Bitmap.Config.ARGB_8888, false);
        buffer = ByteBuffer.allocate(bitmapARBG.getByteCount());
        bitmapARBG.copyPixelsToBuffer(buffer);
        bitmapARBG.recycle();
    } else {
        buffer = ByteBuffer.allocate(bitmap.getByteCount());
        bitmap.copyPixelsToBuffer(buffer);
    }
    return buffer.array();
}

From source file:net.darkmist.alib.io.BufferUtil.java

public static byte[] asBytes(ByteBuffer buf) {
    buf = buf.duplicate();//from ww w  .  ja  v  a 2  s .c o m
    /* To use buf.array() the buffer must:
     *    be writable as the array will be writable
     *    have arrayOffset() == 0 or the array will not start at the right location
     *    the returned array must be the same length as the buffer's limit or it will be the wrong size.
     */
    if (!buf.isReadOnly() && buf.hasArray() && buf.arrayOffset() == 0) {
        logger.debug("!read-only, hasArray && offset is 0");
        byte[] ret = buf.array();

        if (ret.length == buf.limit())
            return ret;
        logger.debug("length of array !=limit, doing copy...");
    }

    byte[] bytes = new byte[buf.limit()];
    buf.get(bytes, 0, buf.limit());
    return bytes;
}

From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java

@SuppressWarnings("resource")
public static byte[] readFromFile(File file) throws IOException {
    FileChannel fileChannel = new FileInputStream(file).getChannel();
    ByteBuffer bb = ByteBuffer.allocate((int) fileChannel.size());
    fileChannel.read(bb);//ww w. j  ava 2 s.c om
    fileChannel.close();
    bb.flip();
    byte[] bytes;

    if (bb.hasArray()) {
        bytes = bb.array();
    } else {
        bytes = new byte[bb.limit()];
        bb.get(bytes);
    }

    return bytes;
}

From source file:com.ibm.sbt.service.basic.ProxyService.java

private static byte[] decodeBase64(String s) {
    try {//  ww  w . j  a v  a  2  s  .  c  o  m
        Base64.InputStream b64 = new Base64.InputStream(new ReaderInputStream(new StringReader(s)));
        ByteBuffer bb = ByteBuffer.allocate(1024 * 4); // max cookie size
        int byt;
        while ((byt = b64.read()) >= 0) {
            bb.put((byte) (byt & 0xFF));
        }
        return bb.array();
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.kactech.otj.Utils.java

public static byte[] readBytes(File f) throws IOException {
    FileInputStream fis = new FileInputStream(f);
    FileChannel fChannel = fis.getChannel();
    byte[] barray = new byte[(int) f.length()];
    ByteBuffer bb = ByteBuffer.wrap(barray);
    //bb.order(ByteOrder.LITTLE_ENDIAN);
    fChannel.read(bb);//w w  w. j  av  a 2 s.c  o m
    fChannel.close();
    fis.close();
    return bb.array();
}