Example usage for java.nio ByteBuffer clear

List of usage examples for java.nio ByteBuffer clear

Introduction

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

Prototype

public final Buffer clear() 

Source Link

Document

Clears this buffer.

Usage

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowFilterEndToEnd.java

@SuppressWarnings("deprecation")
@Test/*from  w  w w  .j  a  v a 2s  .co m*/
public void testFilterList() throws Exception {
    String cf = "f";
    String table = "TestFuzzyRowFiltersInFilterList";
    Table ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i1 = 0; i1 < 5; i1++) {
        for (int i2 = 0; i2 < 5; i2++) {
            byte[] rk = new byte[10];

            ByteBuffer buf = ByteBuffer.wrap(rk);
            buf.clear();
            buf.putShort((short) 2);
            buf.putInt(i1);
            buf.putInt(i2);

            // Each row contains 5 columns
            for (int c = 0; c < 5; c++) {
                byte[] cq = new byte[4];
                Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                Put p = new Put(rk);
                p.setDurability(Durability.SKIP_WAL);
                p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                ht.put(p);
                LOG.info("Inserting: rk: " + Bytes.toStringBinary(rk) + " cq: " + Bytes.toStringBinary(cq));
            }
        }
    }

    TEST_UTIL.flush();

    // test passes if we get back 5 KV's (1 row)
    runTest(ht, 5);

}

From source file:org.apache.htrace.impl.PackedBufferManager.java

private void readAndValidateResponseFrame(SelectionKey sockKey, ByteBuffer buf, long expectedSeq,
        int expectedMethodId) throws IOException {
    buf.clear();
    buf.limit(PackedBuffer.HRPC_RESP_FRAME_LENGTH);
    doRecv(sockKey, buf);/*from  ww w .j a va 2  s . c  o  m*/
    buf.flip();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    long seq = buf.getLong();
    if (seq != expectedSeq) {
        throw new IOException("Expected sequence number " + expectedSeq + ", but got sequence number " + seq);
    }
    int methodId = buf.getInt();
    if (expectedMethodId != methodId) {
        throw new IOException("Expected method id " + expectedMethodId + ", but got " + methodId);
    }
    int errorLength = buf.getInt();
    buf.getInt();
    if ((errorLength < 0) || (errorLength > PackedBuffer.MAX_HRPC_ERROR_LENGTH)) {
        throw new IOException("Got server error with invalid length " + errorLength);
    } else if (errorLength > 0) {
        buf.clear();
        buf.limit(errorLength);
        doRecv(sockKey, buf);
        buf.flip();
        CharBuffer charBuf = StandardCharsets.UTF_8.decode(buf);
        String serverErrorStr = charBuf.toString();
        throw new IOException("Got server error " + serverErrorStr);
    }
}

From source file:org.apache.kylin.storage.hbase.cube.v1.filter.TestFuzzyRowFilterV2EndToEnd.java

@Test
public void testEndToEnd() throws Exception {
    String cf = "f";

    HTable ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i0 = 0; i0 < firstPartCardinality; i0++) {

        for (int i1 = 0; i1 < secondPartCardinality; i1++) {

            for (int i2 = 0; i2 < thirdPartCardinality; i2++) {
                byte[] rk = new byte[10];

                ByteBuffer buf = ByteBuffer.wrap(rk);
                buf.clear();
                buf.putShort((short) i0);
                buf.putInt(i1);/*from w  w  w . j a v  a2s .  c o m*/
                buf.putInt(i2);
                for (int c = 0; c < colQualifiersTotal; c++) {
                    byte[] cq = new byte[4];
                    Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                    Put p = new Put(rk);
                    p.setDurability(Durability.SKIP_WAL);
                    p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                    ht.put(p);
                }
            }
        }
    }

    TEST_UTIL.flush();

    // test passes
    runTest1(ht);
    runTest2(ht);

}

From source file:org.apache.hadoop.hbase.filter.TestFuzzyRowFilterEndToEnd.java

@Test
public void testEndToEnd() throws Exception {
    String cf = "f";

    Table ht = TEST_UTIL.createTable(TableName.valueOf(table), Bytes.toBytes(cf), Integer.MAX_VALUE);

    // 10 byte row key - (2 bytes 4 bytes 4 bytes)
    // 4 byte qualifier
    // 4 byte value

    for (int i0 = 0; i0 < firstPartCardinality; i0++) {

        for (int i1 = 0; i1 < secondPartCardinality; i1++) {

            for (int i2 = 0; i2 < thirdPartCardinality; i2++) {
                byte[] rk = new byte[10];

                ByteBuffer buf = ByteBuffer.wrap(rk);
                buf.clear();
                buf.putShort((short) i0);
                buf.putInt(i1);//from w  w  w.j  av  a2 s  .  c  om
                buf.putInt(i2);
                for (int c = 0; c < colQualifiersTotal; c++) {
                    byte[] cq = new byte[4];
                    Bytes.putBytes(cq, 0, Bytes.toBytes(c), 0, 4);

                    Put p = new Put(rk);
                    p.setDurability(Durability.SKIP_WAL);
                    p.add(cf.getBytes(), cq, Bytes.toBytes(c));
                    ht.put(p);
                }
            }
        }
    }

    TEST_UTIL.flush();

    // test passes
    runTest1(ht);
    runTest2(ht);

}

From source file:org.opennms.netmgt.syslogd.SyslogdImplementationsIT.java

@Test
@Transactional/*from  ww  w .ja v a 2  s  . c om*/
public void testDefaultSyslogd() throws Exception {
    Thread listener = new Thread(m_nio);
    listener.start();
    Thread.sleep(3000);

    final int eventCount = 100;

    List<Integer> foos = new ArrayList<Integer>();

    for (int i = 0; i < eventCount; i++) {
        int eventNum = Double.valueOf(Math.random() * 10000).intValue();
        foos.add(eventNum);
    }

    m_eventCounter.setAnticipated(eventCount);

    String testPduFormat = "2010-08-19 localhost foo%d: load test %d on tty1";
    SyslogClient sc = new SyslogClient(null, 10, SyslogClient.LOG_USER, addr("127.0.0.1"));

    /*
    // Test by directly invoking the SyslogConnection task
    System.err.println("Starting to send packets");
    final long start = System.currentTimeMillis();
    for (int i = 0; i < eventCount; i++) {
    int foo = foos.get(i);
    DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, String.format(testPduFormat, foo, foo));
    WaterfallExecutor.waterfall(m_executorService, new SyslogConnection(pkt, m_config));
    }
            
    // Test by sending over a java.net socket
    final DatagramSocket socket = new DatagramSocket();
    System.err.println("Starting to send packets");
    final long start = System.currentTimeMillis();
    for (int i = 0; i < eventCount; i++) {
    int foo = foos.get(i);
    DatagramPacket pkt = sc.getPacket(SyslogClient.LOG_DEBUG, String.format(testPduFormat, foo, foo));
    socket.send(pkt);
    }
    socket.close();
    */

    // Test by sending over an NIO channel
    SocketAddress address = new InetSocketAddress(InetAddressUtils.getLocalHostAddress(), SyslogClient.PORT);
    final DatagramChannel channel = DatagramChannel.open();
    final ByteBuffer buffer = ByteBuffer.allocate(SyslogReceiverNioThreadPoolImpl.MAX_PACKET_SIZE);
    buffer.clear();
    System.err.println("Starting to send packets");
    final long start = System.currentTimeMillis();
    for (int i = 0; i < eventCount; i++) {
        int foo = foos.get(i);
        buffer.put(SyslogClient.getPacketPayload(SyslogClient.LOG_USER, null, SyslogClient.LOG_DEBUG,
                String.format(testPduFormat, foo, foo)));
        buffer.flip();
        channel.send(buffer, address);
        buffer.clear();
    }
    channel.close();
    /*
    */

    long mid = System.currentTimeMillis();
    System.err.println(String.format("Sent %d packets in %d milliseconds", eventCount, mid - start));

    m_eventCounter.waitForFinish(120000);
    long end = System.currentTimeMillis();

    System.err.println(
            String.format("Events expected: %d, events received: %d", eventCount, m_eventCounter.getCount()));
    final long total = (end - start);
    final double eventsPerSecond = (eventCount * 1000.0 / total);
    System.err.println(String.format("total time: %d, wait time: %d, events per second: %8.4f", total,
            (end - mid), eventsPerSecond));

    listener.interrupt();
    listener.join();
}

From source file:org.silverpeas.core.web.http.FileResponse.java

/**
 * Fills partially the output response./*  w w w  .  j a va2 s.c  o  m*/
 * @param path the path of the file.
 * @param partialData the partial data.
 * @param output the output stream to write into.
 */
void partialOutputStream(final Path path, final ContentRangeData partialData, final OutputStream output)
        throws IOException {
    SilverLogger.getLogger(this).debug("{0} - start at {1} - end at {2} - partLength {3}",
            StringUtil.abbreviate(path.toString(), path.toString().length(), MAX_PATH_LENGTH_IN_LOGS),
            partialData.start, partialData.end, partialData.partContentLength);
    try (SeekableByteChannel input = Files.newByteChannel(path, READ)) {
        input.position(partialData.start);
        int bytesRead;
        int bytesLeft = partialData.partContentLength;
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH);
        while ((bytesRead = input.read(buffer)) != -1 && bytesLeft > 0) {
            buffer.clear();
            output.write(buffer.array(), 0, bytesLeft < bytesRead ? bytesLeft : bytesRead);
            bytesLeft -= bytesRead;
        }
        SilverLogger.getLogger(this).debug("{0} - all part content bytes sent",
                StringUtil.abbreviate(path.toString(), path.toString().length(), MAX_PATH_LENGTH_IN_LOGS));
    } catch (IOException ioe) {
        SilverLogger.getLogger(this).debug(
                "client stopping the streaming HTTP Request of file content represented by "
                        + "''{0}'' identifier (original message ''{1}'')",
                StringUtil.abbreviate(path.toString(), path.toString().length(), MAX_PATH_LENGTH_IN_LOGS),
                ioe.getMessage(), ioe);
    }
}

From source file:com.serenegiant.media.TLMediaEncoder.java

/**
 * read raw bit stream from specific intermediate file
 * @param in//from  w w  w. ja  v a2  s.c  o  m
 * @param header
 * @param buffer
 * @param readBuffer
 * @throws IOException
 * @throws BufferOverflowException
 */
/*package*/static ByteBuffer readStream(final DataInputStream in, final TLMediaFrameHeader header,
        ByteBuffer buffer, final byte[] readBuffer) throws IOException {

    readHeader(in, header);
    if ((buffer == null) || header.size > buffer.capacity()) {
        buffer = ByteBuffer.allocateDirect(header.size);
    }
    buffer.clear();
    final int max_bytes = Math.min(readBuffer.length, header.size);
    int read_bytes;
    for (int i = header.size; i > 0; i -= read_bytes) {
        read_bytes = in.read(readBuffer, 0, Math.min(i, max_bytes));
        if (read_bytes <= 0)
            break;
        buffer.put(readBuffer, 0, read_bytes);
    }
    buffer.flip();
    return buffer;
}

From source file:com.act.lcms.v2.fullindex.Searcher.java

/**
 * Extracts the value bytes from the index corresponding to a list of keys of fixed primitive type.
 * @param cf The column family from which to read.
 * @param keys A list of keys whose values to extract.
 * @param keyBytes The exact number of bytes required by a key; should be uniform for primitive-typed keys
 * @param put A function that writes a key to a ByteBuffer.
 * @param <K> The type of the key.
 * @return An array of arrays of bytes, one per key, containing the values of the key at that position.
 * @throws RocksDBException/*from  w w  w  . j  av  a2 s. c om*/
 */
private <K> byte[][] extractValueBytes(ColumnFamilies cf, List<K> keys, int keyBytes,
        BiFunction<ByteBuffer, K, ByteBuffer> put) throws RocksDBException {
    byte[][] valBytes = new byte[keys.size()][];
    ByteBuffer keyBuffer = ByteBuffer.allocate(keyBytes);
    for (int i = 0; i < keys.size(); i++) {
        K k = keys.get(i);
        keyBuffer.clear();
        put.apply(keyBuffer, k).flip();
        // TODO: try compacting the keyBuffer array to be safe?
        valBytes[i] = dbAndHandles.get(cf, keyBuffer.array());
        assert (valBytes[i] != null);
    }
    return valBytes;
}

From source file:org.energy_home.jemma.osgi.ah.io.flexgateway.FlexGatewayButtons.java

public void run() {
    long tv_sec;//  w w w.  j av  a 2s. com
    long tv_usec;
    short type;
    short code;
    int value;
    while (true) {
        buttonFile = new File(flexGatewayButtonsDevice);
        ByteBuffer buffer = ByteBuffer.allocate(100);
        buffer.order(ByteOrder.nativeOrder());
        try {
            fis = new FileInputStream(buttonFile);
            channel = fis.getChannel();
            while (true) {
                buffer.clear();
                int size = channel.read(buffer);
                buffer.rewind();

                while (size > 0) {
                    tv_sec = buffer.getInt();
                    tv_usec = buffer.getInt();
                    long tv = tv_sec * 1000000 + tv_usec;
                    type = buffer.getShort();
                    code = buffer.getShort();
                    value = buffer.getInt();
                    size -= 16;

                    if (type == 0 && code == 0 && value == 0)
                        continue;

                    // Code 3 -> front button
                    // Code 2 -> back button
                    // Value > 0 -> button pressed
                    // Value > 0 -> button released

                    // send event
                    log.debug("Button: ms " + tv + " type " + (type & 0xffff) + " code " + (code & 0xffff)
                            + " value " + (value & 0xffffffff));

                    Hashtable props = new Hashtable();
                    props.put("timestamp", new Long(tv));
                    if (value > 0)
                        this.postEvent("button/" + code + "/UP", props);
                    else
                        this.postEvent("button/" + code + "/DOWN", props);
                }
            }
        } catch (ClosedByInterruptException e) {
            break;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("exception", e);
            break;
        } finally {
            try {
                if (channel != null)
                    channel.close();
            } catch (IOException e) {
                log.error("exception", e);
                break;
            }
        }
    }
    log.debug("exiting");
}

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

/**
 * Will return the first available {@link ByteBuffer} from the pool
 * or new one/*from   www .ja  va2  s . c om*/
 */
public ByteBuffer poll() {
    ByteBuffer buffer = this.bufferQueue.poll();
    if (buffer == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating new buffer with capacity: " + this.initialBufferSize);
        }
        buffer = ByteBuffer.allocate(this.initialBufferSize);
    } else {
        logger.trace("Polled buffer");
    }
    buffer.clear();
    return buffer;
}