Example usage for java.nio ByteBuffer duplicate

List of usage examples for java.nio ByteBuffer duplicate

Introduction

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

Prototype

public abstract ByteBuffer duplicate();

Source Link

Document

Returns a duplicated buffer that shares its content with this buffer.

Usage

From source file:org.grouplens.lenskit.data.dao.packed.BinaryRatingDAO.java

static BinaryRatingDAO fromBuffer(ByteBuffer buffer) {
    BinaryHeader header = BinaryHeader.fromHeader(buffer);
    assert buffer.position() >= BinaryHeader.HEADER_SIZE;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(header.getRatingDataSize());

    ByteBuffer tableBuffer = buffer.duplicate();
    tableBuffer.position(tableBuffer.position() + header.getRatingDataSize());
    BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer);
    BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer);

    return new BinaryRatingDAO(null, header, dup.slice(), utbl, itbl);
}

From source file:org.lealone.cluster.utils.ByteBufferUtil.java

public static InputStream inputStream(ByteBuffer bytes) {
    final ByteBuffer copy = bytes.duplicate();

    return new InputStream() {
        @Override//from w w w . j a va2  s. c o m
        public int read() {
            if (!copy.hasRemaining())
                return -1;

            return copy.get() & 0xFF;
        }

        @Override
        public int read(byte[] bytes, int off, int len) {
            if (!copy.hasRemaining())
                return -1;

            len = Math.min(len, copy.remaining());
            copy.get(bytes, off, len);
            return len;
        }

        @Override
        public int available() {
            return copy.remaining();
        }
    };
}

From source file:org.lealone.cluster.utils.ByteBufferUtil.java

public static boolean isPrefix(ByteBuffer prefix, ByteBuffer value) {
    if (prefix.remaining() > value.remaining())
        return false;

    int diff = value.remaining() - prefix.remaining();
    return prefix.equals(value.duplicate().limit(value.remaining() - diff));
}

From source file:org.lenskit.data.packed.BinaryIndexTable.java

/**
 * Create a binary index table./*from   w ww  .  j av  a 2 s.  c  o m*/
 * @param nentries The number of entries in the table.
 * @param buffer The table buffer.  Its position will be advanced to the end of the table.
 * @return The index table.
 */
public static BinaryIndexTable fromBuffer(int nentries, ByteBuffer buffer) {
    logger.debug("reading table of {} entries", nentries);
    long[] keys = new long[nentries];
    int[] offsets = new int[nentries];
    int[] sizes = new int[nentries];
    // Read the index table's header (IDs, offsets, and counts/sizes).
    int nextExpectedOffset = 0;
    for (int i = 0; i < nentries; i++) {
        keys[i] = buffer.getLong();
        if (i > 0 && keys[i - 1] >= keys[i]) {
            logger.error("key {} is not greater than previous key {}", keys[i], keys[i - 1]);
            throw new IllegalArgumentException("corrupted index table");
        }
        offsets[i] = buffer.getInt();
        sizes[i] = buffer.getInt();
        if (offsets[i] != nextExpectedOffset) {
            logger.error("expected offset {}, got {}", nextExpectedOffset, offsets[i]);
            throw new IllegalArgumentException("corrupted index table");
        }
        nextExpectedOffset += sizes[i];
    }

    // Set up the integer store
    if (buffer.remaining() < nextExpectedOffset * 4) {
        throw new IllegalArgumentException("buffer not large enough");
    }
    int end = buffer.position() + nextExpectedOffset * 4;
    ByteBuffer dup = buffer.duplicate();
    dup.limit(end);
    // update input indexStore's position
    buffer.position(end);
    // create index table object
    LongKeyDomain dom = LongKeyDomain.wrap(keys, keys.length, true);
    return new BinaryIndexTable(dom, offsets, sizes, dup.asIntBuffer());
}

From source file:org.lenskit.data.packed.BinaryRatingDAO.java

static BinaryRatingDAO fromBuffer(ByteBuffer buffer) {
    // start by reading header from the buffer
    BinaryHeader header = BinaryHeader.fromHeader(buffer);
    assert buffer.position() >= BinaryHeader.HEADER_SIZE;
    // the header read advanced the buffer position past the header; prepare a data slice
    // first slice to remove the header
    ByteBuffer data = buffer.slice();
    // then limit to the rating data size
    data.limit(header.getRatingDataSize());
    assert data.remaining() == header.getRatingDataSize();

    // prepare to read tables
    ByteBuffer tableBuffer = buffer.duplicate();
    // skip the header and the rating data
    tableBuffer.position(tableBuffer.position() + header.getRatingDataSize());
    // each of the following reads advances the buffer by the amount read
    BinaryIndexTable utbl = BinaryIndexTable.fromBuffer(header.getUserCount(), tableBuffer);
    BinaryIndexTable itbl = BinaryIndexTable.fromBuffer(header.getItemCount(), tableBuffer);

    return new BinaryRatingDAO(null, header, data, utbl, itbl, header.getRatingCount(), Long.MAX_VALUE);
}

From source file:org.paxle.crawler.proxy.impl.ProxyForwarder.java

@Execution(Execution.MULTITHREADED)
public boolean onData(NonBlockingBodyDataSource bodyDataSource) throws BufferUnderflowException {
    try {//  w  w  w .  j  a  v a2  s. c o  m
        int available = bodyDataSource.available();
        if (available > 0) {
            ByteBuffer[] data = bodyDataSource.readByteBufferByLength(available);
            if (this.fileDataSink != null) {
                for (ByteBuffer buf : data) {
                    ByteBuffer copy = buf.duplicate();
                    while (copy.hasRemaining()) {
                        this.fileDataSink.write(copy.get());
                    }

                    //                  Charset charset = Charset.forName("UTF-8");
                    //                  System.out.print(charset.decode(buf.duplicate()).toString());

                }
            }
            this.clientDataSink.write(data);
        } else if (available == -1) {
            this.clientDataSink.close();
            if (this.fileDataSink != null)
                this.fileDataSink.close();
        }
    } catch (Throwable ioe) {
        if (this.fileDataSink != null)
            try {
                this.fileDataSink.close();
            } catch (IOException e) {
                /* ignore this */ }
        this.clientDataSink.destroy();
        this.logger.error(String.format("Unexpected '%s': %s", ioe.getClass().getName(), ioe.getMessage()),
                ioe);
    }

    return true;
}

From source file:org.usergrid.utils.JsonUtils.java

public static boolean isSmile(ByteBuffer buffer) {
    buffer = buffer.duplicate();
    if (buffer.get() != 0x3A) {
        return false;
    }//from   ww w  .ja  va  2  s . c  om
    if (buffer.get() != 0x29) {
        return false;
    }
    if (buffer.get() != 0x0A) {
        return false;
    }
    return true;
}

From source file:org.wso2.andes.amqp.QpidAMQPBridge.java

/**
 * message content chunk received to the server
 *
 * @param messageID       id of message to which content belongs
 * @param offsetInMessage chunk offset//w  ww  .ja  va 2 s . c  om
 * @param src             Bytebuffer with content bytes
 */
public AndesMessagePart messageContentChunkReceived(long messageID, int offsetInMessage, ByteBuffer src) {

    if (log.isDebugEnabled()) {
        log.debug("Content Part Received id " + messageID + ", offset " + offsetInMessage);
    }
    AndesMessagePart part = new AndesMessagePart();
    src = src.slice();
    final byte[] chunkData = new byte[src.limit()];
    src.duplicate().get(chunkData);

    part.setData(chunkData);
    part.setMessageID(messageID);
    part.setOffSet(offsetInMessage);
    part.setDataLength(chunkData.length);

    return part;
}

From source file:org.wso2.andes.amqp.QpidAndesBridge.java

/**
 * message content chunk received to the server
 *
 * @param messageID       id of message to which content belongs
 * @param offsetInMessage chunk offset//from   ww  w .j  av  a 2  s.  c  o  m
 * @param src             Bytebuffer with content bytes
 */
public static AndesMessagePart messageContentChunkReceived(long messageID, int offsetInMessage,
        ByteBuffer src) {

    if (log.isDebugEnabled()) {
        log.debug("Content Part Received id " + messageID + ", offset " + offsetInMessage);
    }
    AndesMessagePart part = new AndesMessagePart();
    src = src.slice();
    final byte[] chunkData = new byte[src.limit()];
    src.duplicate().get(chunkData);

    part.setData(chunkData);
    part.setMessageID(messageID);
    part.setOffSet(offsetInMessage);

    return part;
}

From source file:org.wso2.andes.tools.messagestore.commands.Dump.java

protected List<List> createMessageData(java.util.List<Long> msgids, List<QueueEntry> messages,
        boolean showHeaders, boolean showRouting, boolean showMessageHeaders) {

    List<List> display = new LinkedList<List>();

    List<String> hex = new LinkedList<String>();
    List<String> ascii = new LinkedList<String>();
    display.add(hex);//from www.  jav a2s  .c om
    display.add(ascii);

    for (QueueEntry entry : messages) {
        ServerMessage msg = entry.getMessage();
        if (!includeMsg(msg, msgids)) {
            continue;
        }

        //Add divider between messages
        hex.add(Console.ROW_DIVIDER);
        ascii.add(Console.ROW_DIVIDER);

        // Show general message information
        hex.add(Show.Columns.ID.name());
        ascii.add(msg.getMessageNumber().toString());

        hex.add(Console.ROW_DIVIDER);
        ascii.add(Console.ROW_DIVIDER);

        if (showRouting) {
            addShowInformation(hex, ascii, msg, "Routing Details", true, false, false);
        }
        if (showHeaders) {
            addShowInformation(hex, ascii, msg, "Headers", false, true, false);
        }
        if (showMessageHeaders) {
            addShowInformation(hex, ascii, msg, null, false, false, true);
        }

        // Add Content Body section
        hex.add("Content Body");
        ascii.add("");
        hex.add(Console.ROW_DIVIDER);
        ascii.add(Console.ROW_DIVIDER);

        final int messageSize = (int) msg.getSize();
        if (messageSize != 0) {
            hex.add("Hex");
            hex.add(Console.ROW_DIVIDER);

            ascii.add("ASCII");
            ascii.add(Console.ROW_DIVIDER);

            java.nio.ByteBuffer buf = java.nio.ByteBuffer.allocate(64 * 1024);

            int position = 0;

            while (position < messageSize) {

                position += msg.getContent(buf, position);
                buf.flip();
                //Duplicate so we don't destroy original data :)
                java.nio.ByteBuffer hexBuffer = buf;

                java.nio.ByteBuffer charBuffer = hexBuffer.duplicate();

                Hex hexencoder = new Hex();

                while (hexBuffer.hasRemaining()) {
                    byte[] line = new byte[LINE_SIZE];

                    int bufsize = hexBuffer.remaining();
                    if (bufsize < LINE_SIZE) {
                        hexBuffer.get(line, 0, bufsize);
                    } else {
                        bufsize = line.length;
                        hexBuffer.get(line);
                    }

                    byte[] encoded = hexencoder.encode(line);

                    try {
                        String encStr = new String(encoded, 0, bufsize * 2, DEFAULT_ENCODING);
                        String hexLine = "";

                        int strLength = encStr.length();
                        for (int c = 0; c < strLength; c++) {
                            hexLine += encStr.charAt(c);

                            if ((c & 1) == 1 && SPACE_BYTES) {
                                hexLine += BYTE_SPACER;
                            }
                        }

                        hex.add(hexLine);
                    } catch (UnsupportedEncodingException e) {
                        _console.println(e.getMessage());
                        return null;
                    }
                }

                while (charBuffer.hasRemaining()) {
                    String asciiLine = "";

                    for (int pos = 0; pos < LINE_SIZE; pos++) {
                        if (charBuffer.hasRemaining()) {
                            byte ch = charBuffer.get();

                            if (isPrintable(ch)) {
                                asciiLine += (char) ch;
                            } else {
                                asciiLine += NON_PRINTING_ASCII_CHAR;
                            }

                            if (SPACE_BYTES) {
                                asciiLine += BYTE_SPACER;
                            }
                        } else {
                            break;
                        }
                    }

                    ascii.add(asciiLine);
                }
                buf.clear();
            }
        } else {
            List<String> result = new LinkedList<String>();

            display.add(result);
            result.add("No ContentBodies");
        }
    }

    // if hex is empty then we have no data to display
    if (hex.size() == 0) {
        return null;
    }

    return display;
}