Example usage for java.nio ByteBuffer remaining

List of usage examples for java.nio ByteBuffer remaining

Introduction

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

Prototype

public final int remaining() 

Source Link

Document

Returns the number of remaining elements in this buffer, that is limit - position .

Usage

From source file:com.netflix.astyanax.thrift.AbstractThriftMutationBatchImpl.java

@Override
public void deserialize(ByteBuffer data) throws Exception {
    ByteArrayInputStream in = new ByteArrayInputStream(data.array());
    TIOStreamTransport transport = new TIOStreamTransport(in);
    batch_mutate_args args = new batch_mutate_args();

    try {//from   w ww.j  av  a2s  . co m
        TBinaryProtocol bp = new TBinaryProtocol(transport);
        bp.setReadLength(data.remaining());
        args.read(bp);
        mutationMap = args.getMutation_map();
    } catch (TException e) {
        throw ThriftConverter.ToConnectionPoolException(e);
    }
}

From source file:eu.stratosphere.nephele.services.iomanager.IOManagerPerformanceBenchmark.java

@SuppressWarnings("resource")
private final void speedTestNIO(int bufferSize, boolean direct) throws IOException {
    final Channel.ID tmpChannel = ioManager.createChannel();

    File tempFile = null;//from   w w  w .  j av  a2s. c  o m
    FileChannel fs = null;

    try {
        tempFile = new File(tmpChannel.getPath());

        RandomAccessFile raf = new RandomAccessFile(tempFile, "rw");
        fs = raf.getChannel();

        ByteBuffer buf = direct ? ByteBuffer.allocateDirect(bufferSize) : ByteBuffer.allocate(bufferSize);

        long writeStart = System.currentTimeMillis();

        int valsLeft = NUM_INTS_WRITTEN;
        while (valsLeft-- > 0) {
            if (buf.remaining() < 4) {
                buf.flip();
                fs.write(buf);
                buf.clear();
            }
            buf.putInt(valsLeft);
        }

        if (buf.position() > 0) {
            buf.flip();
            fs.write(buf);
        }

        fs.close();
        raf.close();
        fs = null;

        long writeElapsed = System.currentTimeMillis() - writeStart;

        // ----------------------------------------------------------------

        raf = new RandomAccessFile(tempFile, "r");
        fs = raf.getChannel();
        buf.clear();

        long readStart = System.currentTimeMillis();

        fs.read(buf);
        buf.flip();

        valsLeft = NUM_INTS_WRITTEN;
        while (valsLeft-- > 0) {
            if (buf.remaining() < 4) {
                buf.compact();
                fs.read(buf);
                buf.flip();
            }
            if (buf.getInt() != valsLeft) {
                throw new IOException();
            }
        }

        fs.close();
        raf.close();

        long readElapsed = System.currentTimeMillis() - readStart;

        LOG.info("NIO Channel with buffer " + bufferSize + ": write " + writeElapsed + " msecs, read "
                + readElapsed + " msecs.");
    } finally {
        // close if possible
        if (fs != null) {
            fs.close();
            fs = null;
        }
        // try to delete the file
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}

From source file:org.apache.nutch.store.readable.StoreReadable.java

public Parse getParse(String url, WebPage page) {
    HTMLMetaTags metaTags = new HTMLMetaTags();
    System.out.println("[STORE-READABLE]getParse-------------------------------------------------------------");
    String baseUrl = TableUtil.toString(page.getBaseUrl());
    URL base;/*from  w w w.ja va2 s  . co  m*/
    try {
        base = new URL(baseUrl);
    } catch (MalformedURLException e) {
        return ParseStatusUtils.getEmptyParse(e, getConf());
    }

    String text = "";
    String title = "";
    Outlink[] outlinks = new Outlink[0];

    // parse the content
    DocumentFragment root;
    try {
        ByteBuffer contentInOctets = page.getContent();
        InputSource input = new InputSource(new ByteArrayInputStream(contentInOctets.array(),
                contentInOctets.arrayOffset() + contentInOctets.position(), contentInOctets.remaining()));

        EncodingDetector detector = new EncodingDetector(conf);
        detector.autoDetectClues(page, true);
        detector.addClue(sniffCharacterEncoding(contentInOctets), "sniffed");
        String encoding = detector.guessEncoding(page, defaultCharEncoding);

        page.getMetadata().put(new Utf8(Metadata.ORIGINAL_CHAR_ENCODING),
                ByteBuffer.wrap(Bytes.toBytes(encoding)));
        page.getMetadata().put(new Utf8(Metadata.CHAR_ENCODING_FOR_CONVERSION),
                ByteBuffer.wrap(Bytes.toBytes(encoding)));

        input.setEncoding(encoding);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Parsing...");
        }
        root = parse(input);
    } catch (IOException e) {
        LOG.error("Failed with the following IOException: ", e);
        return ParseStatusUtils.getEmptyParse(e, getConf());
    } catch (DOMException e) {
        LOG.error("Failed with the following DOMException: ", e);
        return ParseStatusUtils.getEmptyParse(e, getConf());
    } catch (SAXException e) {
        LOG.error("Failed with the following SAXException: ", e);
        return ParseStatusUtils.getEmptyParse(e, getConf());
    } catch (Exception e) {
        LOG.error("Failed with the following Exception: ", e);
        return ParseStatusUtils.getEmptyParse(e, getConf());
    }

    // get meta directives
    HTMLMetaProcessor.getMetaTags(metaTags, root, base);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Meta tags for " + base + ": " + metaTags.toString());
    }
    // check meta directives
    if (!metaTags.getNoIndex()) { // okay to index
        StringBuilder sb = new StringBuilder();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Getting text...");
        }
        utils.getText(sb, root); // extract text
        text = sb.toString();
        sb.setLength(0);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Getting title...");
        }
        utils.getTitle(sb, root); // extract title
        title = sb.toString().trim();
    }

    if (!metaTags.getNoFollow()) { // okay to follow links
        ArrayList<Outlink> l = new ArrayList<Outlink>(); // extract outlinks
        URL baseTag = utils.getBase(root);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Getting links...");
        }
        utils.getOutlinks(baseTag != null ? baseTag : base, l, root);
        outlinks = l.toArray(new Outlink[l.size()]);
        if (LOG.isTraceEnabled()) {
            LOG.trace("found " + outlinks.length + " outlinks in " + url);
        }
    }

    ParseStatus status = ParseStatus.newBuilder().build();
    status.setMajorCode((int) ParseStatusCodes.SUCCESS);
    if (metaTags.getRefresh()) {
        status.setMinorCode((int) ParseStatusCodes.SUCCESS_REDIRECT);
        status.getArgs().add(new Utf8(metaTags.getRefreshHref().toString()));
        status.getArgs().add(new Utf8(Integer.toString(metaTags.getRefreshTime())));
    }

    String strJo = addJsonToPage(url, page);

    //        storeJsonToSchema(url, page ,strJo);
    page.setReadable(new Utf8(strJo));

    Parse parse = new Parse(text, title, outlinks, status, strJo);
    parse = htmlParseFilters.filter(url, page, parse, metaTags, root);

    if (metaTags.getNoCache()) { // not okay to cache
        page.getMetadata().put(new Utf8(Nutch.CACHING_FORBIDDEN_KEY),
                ByteBuffer.wrap(Bytes.toBytes(cachingPolicy)));
    }
    parse.setJsonRead(strJo);

    return parse;
}

From source file:com.l2jfree.gameserver.network.L2ClientSelectorThread.java

public void printDebug(ByteBuffer buf, L2Client client, int... opcodes) {
    report(ErrorMode.INVALID_OPCODE, client, null, null);

    if (!Config.PACKET_HANDLER_DEBUG)
        return;/*  w ww  . ja va2  s. c  o  m*/

    L2TextBuilder sb = L2TextBuilder.newInstance();
    sb.append("Unknown Packet: ");

    for (int i = 0; i < opcodes.length; i++) {
        if (i != 0)
            sb.append(" : ");

        sb.append("0x").append(Integer.toHexString(opcodes[i]));
    }
    sb.append(", Client: ").append(client);
    _log.info(sb.moveToString());

    byte[] array = new byte[buf.remaining()];
    buf.get(array);
    for (String line : StringUtils.split(HexUtil.printData(array), "\n"))
        _log.info(line);
}

From source file:org.apache.hadoop.hbase.client.Put.java

/**
 * @param row row key; we make a copy of what we are passed to keep local.
 * @param ts  timestamp//w w  w.  j  ava2  s . c  om
 */
public Put(ByteBuffer row, long ts) {
    if (ts < 0) {
        throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts);
    }
    checkRow(row);
    this.row = new byte[row.remaining()];
    row.get(this.row);
    this.ts = ts;
}

From source file:byps.test.TestRemoteStreams.java

/**
 * Send and receive a stream with content length information.
 * /*from  ww  w.  j  a  v  a2 s . com*/
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testRemoteStreamsOneStreamContentLength() throws InterruptedException, IOException {
    log.info("testRemoteStreamsOneStreamContentLength(");

    String str = "hello";
    InputStream istrm = new ByteArrayInputStream(str.getBytes());
    remote.setImage(istrm);

    InputStream istrmR = remote.getImage();
    ByteBuffer buf = BWire.bufferFromStream(istrmR);
    String strR = new String(buf.array(), buf.position(), buf.remaining());
    TestUtils.assertEquals(log, "stream", str, strR);

    remote.setImage(null);
    TestUtils.checkTempDirEmpty(client);

    log.info(")testRemoteStreamsOneStreamContentLength");
}

From source file:byps.test.TestRemoteStreams.java

/**
 * Send and receive a stream without content length information
 * //  w  w  w.j av  a 2  s  . co  m
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testRemoteStreamsOneStreamChunked() throws InterruptedException, IOException {
    log.info("testRemoteStreamsOneStreamChunked(");

    String str = "hello";
    final ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
    InputStream istrm = new BContentStream() {

        @Override
        public long getContentLength() {
            return -1L;
        }

        @Override
        public int read() throws IOException {
            return bis.read();
        }

    };

    remote.setImage(istrm);

    InputStream istrmR = remote.getImage();
    ByteBuffer buf = BWire.bufferFromStream(istrmR);
    String strR = new String(buf.array(), buf.position(), buf.remaining());
    TestUtils.assertEquals(log, "stream", str, strR);

    remote.setImage(null);
    TestUtils.checkTempDirEmpty(client);

    log.info(")testRemoteStreamsOneStreamChunked");
}

From source file:org.apache.tajo.master.exec.QueryExecutor.java

public void execExplain(Session session, String query, LogicalPlan plan, QueryContext queryContext,
        boolean isGlobal, SubmitQueryResponse.Builder response) throws Exception {

    String explainStr;// w ww  .  java2s  .  c  o  m
    boolean isTest = queryContext.getBool(SessionVars.TEST_PLAN_SHAPE_FIX_ENABLED);
    if (isTest) {
        ExplainPlanPreprocessorForTest preprocessorForTest = new ExplainPlanPreprocessorForTest();
        preprocessorForTest.prepareTest(plan);
    }

    if (isGlobal) {
        GlobalPlanner planner = new GlobalPlanner(context.getConf(), context.getCatalog());
        MasterPlan masterPlan = compileMasterPlan(plan, queryContext, planner);
        if (isTest) {
            ExplainGlobalPlanPreprocessorForTest globalPlanPreprocessorForTest = new ExplainGlobalPlanPreprocessorForTest();
            globalPlanPreprocessorForTest.prepareTest(masterPlan);
        }
        explainStr = masterPlan.toString();
    } else {
        explainStr = PlannerUtil.buildExplainString(plan.getRootBlock().getRoot());
    }

    Schema schema = SchemaBuilder.builder().add("explain", TajoDataTypes.Type.TEXT).build();

    SerializedResultSet.Builder serializedResBuilder = SerializedResultSet.newBuilder();
    MemoryRowBlock rowBlock = new MemoryRowBlock(SchemaUtil.toDataTypes(schema));
    String[] lines = explainStr.split("\n");
    try {
        for (String line : lines) {
            rowBlock.getWriter().startRow();
            rowBlock.getWriter().putText(line);
            rowBlock.getWriter().endRow();
        }
        MemoryBlock memoryBlock = rowBlock.getMemory();
        ByteBuffer uncompressed = memoryBlock.getBuffer().nioBuffer(0, memoryBlock.readableBytes());
        int uncompressedLength = uncompressed.remaining();

        serializedResBuilder.setDecompressedLength(uncompressedLength);
        serializedResBuilder.setSerializedTuples(ByteString.copyFrom(uncompressed));
        serializedResBuilder.setSchema(schema.getProto());
        serializedResBuilder.setRows(rowBlock.rows());
    } finally {
        rowBlock.release();
    }

    QueryInfo queryInfo = context.getQueryJobManager().createNewSimpleQuery(queryContext, session, query,
            (LogicalRootNode) plan.getRootBlock().getRoot());

    response.setState(OK);
    response.setQueryId(queryInfo.getQueryId().getProto());
    response.setResultType(ResultType.ENCLOSED);
    response.setResultSet(serializedResBuilder.build());
    response.setMaxRowNum(lines.length);
}

From source file:byps.test.TestRemoteStreams.java

/**
 * Send file stream./*from  w w w . ja  va 2s .c  om*/
 * A file stream has the fileName property set.
 * @throws InterruptedException
 * @throws IOException
 */
@Test
public void testRemoteStreamsFileStream() throws InterruptedException, IOException {
    log.info("testRemoteStreamsFileStream(");

    File file = File.createTempFile("byps", ".txt");
    String str = "hello";

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        fos.write(str.getBytes());
    } finally {
        if (fos != null)
            fos.close();
    }

    InputStream istrm = new BContentStreamWrapper(file);
    remote.setImage(istrm);

    BContentStream istrmR = (BContentStream) remote.getImage();

    TestUtils.assertEquals(log, "Content-Type", "text/plain", istrmR.getContentType());
    TestUtils.assertEquals(log, "Content-Length", file.length(), istrmR.getContentLength());
    TestUtils.assertEquals(log, "FileName", file.getName(), istrmR.getFileName());

    ByteBuffer buf = BWire.bufferFromStream(istrmR);
    String strR = new String(buf.array(), buf.position(), buf.remaining());
    TestUtils.assertEquals(log, "stream", str, strR);

    TestUtils.assertEquals(log, "Content-Type", "text/plain", istrmR.getContentType());
    TestUtils.assertEquals(log, "Content-Length", file.length(), istrmR.getContentLength());
    TestUtils.assertEquals(log, "FileName", file.getName(), istrmR.getFileName());

    remote.setImage(null);
    TestUtils.checkTempDirEmpty(client);

    log.info(")testRemoteStreamsFileStream");
}

From source file:com.zotoh.maedr.device.apache.StreamingNHttpEntity.java

private void sockItDown(ContentDecoder decoder) throws IOException {

    tlog().debug("StreamingNHttpEntity: sockItDown()");

    ByteBuffer buffer;
    int cnt;/*from   w  w w.j av a  2s . c  o  m*/

    buffer = _alloctor.allocate(4096);
    do {

        buffer.clear();

        if ((cnt = decoder.read(buffer)) == -1)
            break;

        if (cnt == 0) {

            if (buffer.hasRemaining())
                break;
            else
                continue;
        }

        // 
        buffer.flip();
        byte[] bits = new byte[4096];
        int len;

        while (buffer.hasRemaining()) {
            len = Math.min(4096, buffer.remaining());
            buffer.get(bits, 0, len);
            storeBytes(bits, len);
        }
    } while (true);

}