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:io.warp10.quasar.encoder.QuasarTokenEncoder.java

public String cypherToken(TBase<?, ?> token, KeyStore keyStore) throws TException {
    byte[] tokenAesKey = keyStore.getKey(KeyStore.AES_TOKEN);
    byte[] tokenSipHashkey = keyStore.getKey(KeyStore.SIPHASH_TOKEN);

    // Serialize the  thrift token into byte array
    byte[] serialized = serializer.serialize(token);

    // Calculate the SIP
    long sip = SipHashInline.hash24_palindromic(tokenSipHashkey, serialized);

    //Create the token byte buffer
    ByteBuffer buffer = ByteBuffer.allocate(8 + serialized.length);
    // adds the sip
    buffer.putLong(sip);/*w  w w  .  j  av a 2 s  .c  o m*/
    // adds the thrift token
    buffer.put(serialized);

    // Wrap the TOKEN
    byte[] wrappedData = CryptoUtils.wrap(tokenAesKey, buffer.array());

    String accessToken = new String(OrderPreservingBase64.encode(wrappedData));

    return accessToken;
}

From source file:acromusashi.kafka.log.producer.WinApacheLogProducer.java

/**
 * ????/*  w w w .j  a  va  2 s . com*/
 *
 * @param random 
 * @return ?????
 * @throws IOException 
 */
private byte[] readToEnd(RandomAccessFile random) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_ALLOCATE_SIZE);

    while ((random.getChannel().read(buffer)) != -1) {
        out.write(buffer.array());
        buffer.clear();
    }

    return out.toByteArray();
}

From source file:org.lispmob.noroot.IPC.java

public void run() {
    int len = 0;// ww w.  j a v  a2  s .  c o m
    ByteBuffer buf = ByteBuffer.allocate(9000);
    while (!ipc_thread.isInterrupted()) {
        buf.clear();
        try {
            len = ipc_channel.read(buf);
            if (len == 0) {
                continue;
            }
            buf.flip();
            String json_str = EncodingUtils.getString(buf.array(), "utf8");
            JSONObject jObj = new JSONObject(json_str);
            int ipc_type = jObj.getInt("type");
            System.out.println("LISPmob: Received IPC message: " + ipc_type);
            switch (ipc_type) {
            case IPC_LOG_MSG:
                LISPmobVPNService.err_msg_code = jObj.getInt("err_msg_code");
                Thread.sleep(1000);
                if (LISPmobVPNService.err_msg_code != 0) {
                    /* If LISPmob is not the active windows, the error msg code is not clean
                     * and we send a notification of the error */
                    Resources res = vpn_service.getResources();
                    String[] err_msg = res.getStringArray(R.array.ErrMsgArray);
                    String msg = err_msg[LISPmobVPNService.err_msg_code];
                    //notifications.notify_msg( msg);
                }
                break;
            case IPC_PROTECT_SOCKS:
                int socket = jObj.getInt("socket");
                if (socket != -1) {
                    boolean sock_protect = false;
                    int retry = 0;
                    while (!sock_protect && retry < 30) {
                        if (!vpn_service.protect(socket)) {
                            retry++;
                            Thread.sleep(200);
                        } else {
                            sock_protect = true;
                            System.out.println(
                                    "LISPmob: The socket " + socket + " has been protected (VPN Service)");
                        }
                    }
                }
                break;
            default:
                System.out.println("***** Unknown IPC message: " + ipc_type);
                break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.github.neoio.net.message.staging.file.TestFileMessageStaging.java

@Test
public void test_primaryStage() throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(1024);

    buffer.put("Hello World".getBytes());
    buffer.flip();//from  w w  w.  j  a va 2  s. c o  m
    staging.writePrimaryStaging(buffer, "Hello World".getBytes().length);

    buffer.clear();
    staging.getPrimaryStage().read(buffer);
    Assert.assertEquals("Hello World".getBytes().length, staging.getPrimaryStageSize());
    Assert.assertEquals("Hello World", new String(buffer.array(), 0, buffer.position()));

    staging.resetPrimaryStage();
    Assert.assertEquals(0, staging.getPrimaryStageSize());
}

From source file:com.example.android.toyvpn.ToyVpnService.java

private void UDPtoProxy(ByteBuffer packet) throws IOException {
    //TODO FARE un THREAD??
    final DatagramSocket clientSocket = new DatagramSocket();
    InetAddress IPAddress = InetAddress.getByName("localhost");
    //send Pkt to Proxy
    DatagramPacket sendPacket = new DatagramPacket(packet.array(), packet.array().length, IPAddress,
            mServerPort);//from  w w  w  .  ja v  a 2  s.c o m
    clientSocket.send(sendPacket);
}

From source file:com.mycompany.monroelabsm.Seed.java

public byte[] getSeed() {
    byte[] seed = new byte[32];
    ByteBuffer bb = ByteBuffer.wrap(seed);
    bb.put(serial);//from w w  w.  j  a v a  2 s. c o m
    bb.put(operator);
    bb.put(heading);
    bb.put(gpsx);
    bb.put(gpsy);
    bb.put(crypto);
    bb.put(fiat);
    bb.put(denomination);
    bb.put(time);
    return bb.array();
}

From source file:indexer.DocVector.java

byte[] getVecBytes(float[] x) {
    ByteBuffer buff = ByteBuffer.allocate(Float.SIZE * x.length);
    for (float xval : x)
        buff.putFloat(xval);//from  w w  w  .j av  a2 s .  c  om

    byte[] bytes = buff.array();
    return bytes;
}

From source file:net.pictulog.otgdb.task.BackupTask.java

private boolean copyFile(FsFile srcFile, File destFile) {
    publishProgress(currentFile++);//from ww w  .java 2  s  .  c o  m
    if (srcFile.isValid()) {
        ByteBuffer buffer = ByteBuffer.allocate((int) srcFile.getLength());
        if (destFile.exists() && !overwrite) {
            return true;
        }
        OutputStream fos = null;
        try {
            fos = new FileOutputStream(destFile);
            srcFile.read(0, buffer);
            fos.write(buffer.array());
            fos.flush();
            return true;
        } catch (IOException e) {
            Log.e("BackupTask", e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(fos);
        }
    }
    return false;
}

From source file:au.org.ala.delta.io.BinFile.java

public String sread(int size) {
    ByteBuffer bb = readByteBuffer(size);
    return BinFileEncoding.decode(bb.array());
}

From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteReader.java

public byte[] CompressensureDecompressed() throws IOException {
    FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray();
    dynamicBuffer.add(inBuf.getData(), 0, inBuf.getLength());
    ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size());
    dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size());
    byteBuf.flip();/* w  w w  .jav a2s. c  o m*/
    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(byteBuf.array(), 0, byteBuf.array().length);
    int dictionarySize = dib.readInt();
    int OnlydictionarySize = dib.readInt();
    dib.reset(byteBuf.array(), 8, dictionarySize - 4);
    byte[] dictionaryBuffer = dib.getData();
    dib.reset(byteBuf.array(), 4 + dictionarySize, (byteBuf.array().length - dictionarySize - 4));
    byte[] dictionaryId = dib.getData();
    dib.close();
    DictionaryValuesReader cr = initDicReader(OnlydictionarySize, dictionaryBuffer,
            PrimitiveType.PrimitiveTypeName.BINARY);
    cr.initFromPage(numPairs, dictionaryId, 0);
    DataOutputBuffer decoding = new DataOutputBuffer();
    decoding.writeInt(decompressedSize);
    decoding.writeInt(numPairs);
    decoding.writeInt(startPos);
    for (int i = 0; i < numPairs; i++) {
        byte tmp = Byte.parseByte(cr.readBytes().toStringUsingUTF8());
        decoding.writeInt(tmp);
    }
    byteBuf.clear();
    inBuf.close();
    return decoding.getData();
}