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:eu.stratosphere.pact.common.io.TextInputFormat.java

public boolean readRecord(PactRecord target, byte[] bytes, int offset, int numBytes) {
    PactString str = this.theString;

    if (this.ascii) {
        str.setValueAscii(bytes, offset, numBytes);
    } else {/*from  w w w.  j  ava 2s  .  c o m*/
        ByteBuffer byteWrapper = this.byteWrapper;
        if (bytes != byteWrapper.array()) {
            byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length);
            this.byteWrapper = byteWrapper;
        }
        byteWrapper.position(offset);
        byteWrapper.limit(offset + numBytes);

        try {
            CharBuffer result = this.decoder.decode(byteWrapper);
            str.setValue(result);
        } catch (CharacterCodingException e) {
            byte[] copy = new byte[numBytes];
            System.arraycopy(bytes, offset, copy, 0, numBytes);
            LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e);
            return false;
        }
    }

    target.clear();
    target.setField(this.pos, str);
    return true;
}

From source file:Main.java

public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {/* w w w  . j a v  a2  s .c om*/

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] ivp = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {
            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.sample.MyEndpoint.java

@OnMessage
public void echoBinary(ByteBuffer data, Session session) {
    System.out.println("echoBinary: " + data);
    for (byte b : data.array()) {
        System.out.print(b);/* w w  w .  ja va2  s  .  com*/
    }
    try {
        session.getBasicRemote().sendBinary(data);
    } catch (IOException ex) {
        Logger.getLogger(MyEndpoint.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.rwhq.serializer.FixedStringSerializer.java

@Override
public byte[] serialize(final String o) {
    final byte[] bytes = o.getBytes();

    if (bytes.length > (length - 1)) {
        throw new IllegalArgumentException("String is too long to be serialized");
    }//from  www .  j  a va 2s. c  om

    final ByteBuffer buf = ByteBuffer.allocate(length);
    buf.putShort((short) bytes.length);
    buf.put(bytes);
    return buf.array();
}

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

@Override
public void writePrimaryStaging(ByteBuffer buffer, int length) throws NetIOException {
    primaryStage.write(buffer.array(), buffer.position(), length);
    buffer.position(buffer.limit());//  w w  w.  j  ava  2s .  c  o  m
}

From source file:com.esri.geoevent.test.performance.ClockSync.java

@Override
public void run() {
    DatagramSocket socket = null;
    try {//ww  w .jav  a 2  s .  c  om
        byte[] incomingBuffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(incomingBuffer, incomingBuffer.length);

        ByteBuffer bb = ByteBuffer.allocate(8);
        DatagramPacket outgoingPacket = new DatagramPacket(bb.array(), 0, 8, null, port);
        socket = new DatagramSocket(port);
        socket.setSoTimeout(100);
        while (isRunning.get()) {
            try {
                socket.receive(packet);
                long now = System.currentTimeMillis();
                bb.putLong(now);
                outgoingPacket.setAddress(packet.getAddress());
                outgoingPacket.setPort(packet.getPort());
                socket.send(outgoingPacket);
                bb.clear();
                //System.out.println("Sent the time " + now);
            } catch (SocketTimeoutException ex) {
                // Do nothing if nothing was sent.
            }
        }
    } catch (BindException e) {
        // port is in use - increment and try again
        port++;
        this.run();
    } catch (SocketException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
}

From source file:mil.nga.giat.geowave.datastore.accumulo.AccumuloDataStore.java

protected static byte[] getRowIdBytes(final GeowaveRowId rowElements) {
    final ByteBuffer buf = ByteBuffer.allocate(12 + rowElements.getDataId().length
            + rowElements.getAdapterId().length + rowElements.getInsertionId().length);
    buf.put(rowElements.getInsertionId());
    buf.put(rowElements.getAdapterId());
    buf.put(rowElements.getDataId());/*from   w  ww  . j  a  v a  2  s .  com*/
    buf.putInt(rowElements.getAdapterId().length);
    buf.putInt(rowElements.getDataId().length);
    buf.putInt(rowElements.getNumberOfDuplicates());
    return buf.array();
}

From source file:ch.cyberduck.core.cryptomator.CryptoInputStream.java

private int readNextChunk() throws IOException {
    final ByteBuffer ciphertextBuf = ByteBuffer.allocate(chunkSize);
    final int read = IOUtils.read(proxy, ciphertextBuf.array());
    if (read == 0) {
        return IOUtils.EOF;
    }// ww  w  . j  av a 2s  . c om
    ciphertextBuf.position(read);
    ciphertextBuf.flip();
    try {
        buffer = cryptor.fileContentCryptor().decryptChunk(ciphertextBuf, chunkIndexOffset++, header, true);
    } catch (CryptoException e) {
        throw new IOException(e.getMessage(), new CryptoAuthenticationException(e.getMessage(), e));
    }
    return read;
}

From source file:eu.stratosphere.api.java.record.io.TextInputFormat.java

public Record readRecord(Record reuse, byte[] bytes, int offset, int numBytes) {
    StringValue str = this.theString;

    //Check if \n is used as delimiter and the end of this line is a \r, then remove \r from the line
    if (this.getDelimiter() != null && this.getDelimiter().length == 1 && this.getDelimiter()[0] == NEW_LINE
            && offset + numBytes >= 1 && bytes[offset + numBytes - 1] == CARRIAGE_RETURN) {
        numBytes -= 1;//from   w w w.  j a va  2 s . co  m
    }

    if (this.ascii) {
        str.setValueAscii(bytes, offset, numBytes);
    } else {
        ByteBuffer byteWrapper = this.byteWrapper;
        if (bytes != byteWrapper.array()) {
            byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length);
            this.byteWrapper = byteWrapper;
        }
        byteWrapper.limit(offset + numBytes);
        byteWrapper.position(offset);

        try {
            CharBuffer result = this.decoder.decode(byteWrapper);
            str.setValue(result);
        } catch (CharacterCodingException e) {
            byte[] copy = new byte[numBytes];
            System.arraycopy(bytes, offset, copy, 0, numBytes);
            LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e);
            return null;
        }
    }

    reuse.clear();
    reuse.setField(this.pos, str);
    return reuse;
}

From source file:RegexProperties.java

public void load(FileInputStream inStream) throws IOException, PatternSyntaxException {
    FileChannel fc = inStream.getChannel();

    ByteBuffer bb = ByteBuffer.allocate((int) fc.size());
    fc.read(bb);/*from   ww  w  .jav a2  s .co m*/
    bb.flip();
    String fileContent = new String(bb.array());

    Pattern pattern = Pattern.compile("^(.*)$", Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(fileContent);

    while (matcher.find()) {
        String line = matcher.group(1);
        if (line != null && !"".equals(line.trim()) && !line.startsWith("#") && !line.startsWith("!")) {
            String keyValue[] = null;
            if (line.indexOf("=") > 0)
                keyValue = line.split("=", 2);
            else
                keyValue = line.split(":", 2);

            if (keyValue != null) {
                super.put(keyValue[0].trim(), keyValue[1]);
            }
        }
    }
    fc = null;
    bb = null;
}