Example usage for java.nio ByteBuffer flip

List of usage examples for java.nio ByteBuffer flip

Introduction

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

Prototype

public final Buffer flip() 

Source Link

Document

Flips this buffer.

Usage

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

@Override
public byte[] ensureDecompressed() throws IOException {
    //  byte[]  bytes=inBuf.getData() ;
    FlexibleEncoding.ORC.DynamicByteArray dynamicBuffer = new FlexibleEncoding.ORC.DynamicByteArray();
    dynamicBuffer.add(inBuf.getData(), 12, inBuf.getLength() - 12);
    ByteBuffer byteBuf = ByteBuffer.allocate(dynamicBuffer.size());
    dynamicBuffer.setByteBuffer(byteBuf, 0, dynamicBuffer.size());
    byteBuf.flip();
    FlexibleEncoding.ORC.InStream instream = FlexibleEncoding.ORC.InStream.create("test", byteBuf, null,
            dynamicBuffer.size());/*from w w  w .  j  a  v  a 2s . co m*/
    RunLengthByteReader rlein = new RunLengthByteReader(instream);
    DataOutputBuffer decoding = new DataOutputBuffer();
    decoding.writeInt(decompressedSize);
    decoding.writeInt(numPairs);
    decoding.writeInt(startPos);
    for (int i = 0; i < numPairs; i++) {
        byte tmp = rlein.next();
        decoding.writeByte(tmp);
    }
    byteBuf.clear();
    inBuf.close();
    return decoding.getData();
}

From source file:com.google.cloud.bigtable.hbase.TestIncrement.java

/**
 * Requirement 6.6/*ww  w  .j  a v a 2 s  . com*/
 */
@Test
public void testIncrementEightBytes() throws IOException {
    // Initialize
    Table table = getConnection().getTable(TABLE_NAME);
    byte[] rowKey = dataHelper.randomData("testrow-");
    byte[] qual = dataHelper.randomData("qual-");
    byte[] value = new byte[8];
    new Random().nextBytes(value);
    ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE);
    buffer.put(value);
    buffer.flip();
    long equivalentValue = buffer.getLong();

    // Put the bytes in
    Put put = new Put(rowKey).addColumn(COLUMN_FAMILY, qual, value);
    table.put(put);

    // Increment
    Increment increment = new Increment(rowKey).addColumn(COLUMN_FAMILY, qual, 1L);
    Result result = table.increment(increment);
    Assert.assertEquals("Should have incremented the bytes like a long", equivalentValue + 1L,
            Bytes.toLong(CellUtil.cloneValue(result.getColumnLatestCell(COLUMN_FAMILY, qual))));
}

From source file:com.alibaba.jstorm.utils.JStormUtils.java

public static long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE);
    buffer.put(bytes);//from   w  ww .j  a v  a  2 s. com
    buffer.flip();// need flip
    return buffer.getLong();
}

From source file:com.digium.respokesdk.RespokeDirectConnection.java

/**
 *  Send a message to the remote client through the direct connection.
 *
 *  @param message             The message to send
 *  @param completionListener  A listener to receive a notification on the success of the asynchronous operation
 */// ww  w  .j a  v  a  2s .c o m
public void sendMessage(String message, final Respoke.TaskCompletionListener completionListener) {
    if (isActive()) {
        JSONObject jsonMessage = new JSONObject();
        try {
            jsonMessage.put("message", message);
            byte[] rawMessage = jsonMessage.toString().getBytes(Charset.forName("UTF-8"));
            ByteBuffer directData = ByteBuffer.allocateDirect(rawMessage.length);
            directData.put(rawMessage);
            directData.flip();
            DataChannel.Buffer data = new DataChannel.Buffer(directData, false);

            if (dataChannel.send(data)) {
                Respoke.postTaskSuccess(completionListener);
            } else {
                Respoke.postTaskError(completionListener, "Error sending message");
            }
        } catch (JSONException e) {
            Respoke.postTaskError(completionListener, "Unable to encode message to JSON");
        }
    } else {
        Respoke.postTaskError(completionListener, "DataChannel not in an open state");
    }
}

From source file:edu.usc.pgroup.floe.client.FloeClient.java

/**
 * Uploads the file to the coordinator.//from w ww  . j a v a2  s  .c  o m
 * The file is uploaded relative to the coordinator's scratch folder.
 *
 * @param fileName name of the file to be stored on the coordinator.
 * @return the base fileName which may be used for downloading the file
 * later.
 */
public final String uploadFileSync(final String fileName) {
    String baseFile = FilenameUtils.getName(fileName);
    try {
        int fid = getClient().beginFileUpload(baseFile);
        ReadableByteChannel inChannel = Channels.newChannel(new FileInputStream(fileName));
        ByteBuffer buffer = ByteBuffer.allocate(Utils.Constants.BUFFER_SIZE);
        while (inChannel.read(buffer) > 0) {
            buffer.flip();
            getClient().uploadChunk(fid, buffer);
            buffer.clear();
        }
        inChannel.close();
        getClient().finishUpload(fid);
    } catch (TException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    }
    return baseFile;
}

From source file:oz.hadoop.yarn.api.net.ApplicationContainerServerImpl.java

/**
 * /*from   w  w w. java  2 s  .  com*/
 */
void doWrite(SelectionKey selectionKey, ByteBuffer buffer) {
    ByteBuffer message = ByteBufferUtils.merge(ByteBuffer.allocate(4).putInt(buffer.limit() + 4), buffer);
    message.flip();

    selectionKey.attach(message);
    selectionKey.interestOps(SelectionKey.OP_WRITE);
}

From source file:io.stallion.dataAccess.file.FilePersisterBase.java

/**
 * Derives a Long id by hashing the file path and then taking the first 8 bytes
 * of the path.//from   w  ww .ja  v  a 2  s .  c om
 *
 * This is used if the model object doesn't have a defined id field.
 *
 * @param path
 * @return
 */
public Long makeIdFromFilePath(String path) {
    path = path.toLowerCase();
    path = path.replace(getBucketFolderPath().toLowerCase(), "");
    path = StringUtils.stripStart(path, "/");
    path = getBucket() + "-----" + path;
    // Derive a long id by hashing the file path
    byte[] bs = Arrays.copyOfRange(DigestUtils.md5(path), 0, 6);
    bs = ArrayUtils.addAll(new byte[] { 0, 0 }, bs);
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.put(bs);
    buffer.flip();//need flip
    Long l = buffer.getLong();
    if (l < 0) {
        l = -l;
    }
    Log.finest("calculated id is {0}", l);
    return l;
}

From source file:org.apache.bookkeeper.bookie.EntryLogTest.java

private ByteBuffer generateEntry(long ledger, long entry) {
    byte[] data = ("ledger-" + ledger + "-" + entry).getBytes();
    ByteBuffer bb = ByteBuffer.wrap(new byte[8 + 8 + data.length]);
    bb.putLong(ledger);/*w  ww. j ava  2 s  . c o m*/
    bb.putLong(entry);
    bb.put(data);
    bb.flip();
    return bb;
}

From source file:com.doplgangr.secrecy.FileSystem.File.java

public java.io.File readFile(CryptStateListener listener) {
    decrypting = true;/* w ww.j a v  a2 s  . c  o  m*/
    InputStream is = null;
    OutputStream out = null;
    java.io.File outputFile = null;
    try {
        outputFile = java.io.File.createTempFile("tmp" + name, "." + FileType, storage.getTempFolder());
        outputFile.mkdirs();
        outputFile.createNewFile();
        AES_Encryptor enc = new AES_Encryptor(key);
        is = new CipherInputStream(new FileInputStream(file), enc.decryptstream());
        listener.setMax((int) file.length());
        ReadableByteChannel inChannel = Channels.newChannel(is);
        FileChannel outChannel = new FileOutputStream(outputFile).getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(Config.bufferSize);
        while (inChannel.read(byteBuffer) >= 0 || byteBuffer.position() > 0) {
            byteBuffer.flip();
            outChannel.write(byteBuffer);
            byteBuffer.compact();
            listener.updateProgress((int) outChannel.size());
        }
        inChannel.close();
        outChannel.close();
        Util.log(outputFile.getName(), outputFile.length());
        return outputFile;
    } catch (FileNotFoundException e) {
        listener.onFailed(2);
        Util.log("Encrypted File is missing", e.getMessage());
    } catch (IOException e) {
        Util.log("IO Exception while decrypting", e.getMessage());
        if (e.getMessage().contains("pad block corrupted"))
            listener.onFailed(1);
        else
            e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        listener.Finished();
        decrypting = false;
        try {
            if (is != null) {
                is.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // An error occured. Too Bad
    if (outputFile != null)
        storage.purgeFile(outputFile);
    return null;
}

From source file:org.apache.nifi.processor.util.listen.handler.socket.StandardSocketChannelHandler.java

@Override
public void run() {
    boolean eof = false;
    SocketChannel socketChannel = null;

    try {//from w w w . ja v  a  2 s  .  co  m
        int bytesRead;
        socketChannel = (SocketChannel) key.channel();

        final SocketChannelAttachment attachment = (SocketChannelAttachment) key.attachment();
        final ByteBuffer socketBuffer = attachment.getByteBuffer();

        // read until the buffer is full
        while ((bytesRead = socketChannel.read(socketBuffer)) > 0) {
            // prepare byte buffer for reading
            socketBuffer.flip();
            // mark the current position as start, in case of partial message read
            socketBuffer.mark();
            // process the contents that have been read into the buffer
            processBuffer(socketChannel, socketBuffer);

            // Preserve bytes in buffer for next call to run
            // NOTE: This code could benefit from the  two ByteBuffer read calls to avoid
            // this compact for higher throughput
            socketBuffer.reset();
            socketBuffer.compact();
            logger.debug("bytes read {}", new Object[] { bytesRead });
        }

        // Check for closed socket
        if (bytesRead < 0) {
            eof = true;
            logger.debug("Reached EOF, closing connection");
        } else {
            logger.debug("No more data available, returning for selection");
        }
    } catch (ClosedByInterruptException | InterruptedException e) {
        logger.debug("read loop interrupted, closing connection");
        // Treat same as closed socket
        eof = true;
    } catch (ClosedChannelException e) {
        // ClosedChannelException doesn't have a message so handle it separately from IOException
        logger.error("Error reading from channel due to channel being closed", e);
        // Treat same as closed socket
        eof = true;
    } catch (IOException e) {
        logger.error("Error reading from channel due to {}", new Object[] { e.getMessage() }, e);
        // Treat same as closed socket
        eof = true;
    } finally {
        if (eof == true) {
            IOUtils.closeQuietly(socketChannel);
            dispatcher.completeConnection(key);
        } else {
            dispatcher.addBackForSelection(key);
        }
    }
}