Example usage for java.nio ByteBuffer limit

List of usage examples for java.nio ByteBuffer limit

Introduction

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

Prototype

public final int limit() 

Source Link

Document

Returns the limit of this buffer.

Usage

From source file:com.cloudera.seismic.crunch.SUPipeline.java

public int run(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("cwproot", true, "The path to CWPROOT on the cluster machines");
    options.addOption("input", true, "SU files in Hadoop");
    options.addOption("output", true, "The path of the SU files to write out to Hadoop");
    options.addOption("command", true, "A pipeline of SU commands to run on the data");

    // Parse the commandline and check for required arguments.
    CommandLine cmdLine = new PosixParser().parse(options, args, false);
    if (!cmdLine.hasOption("input") || !cmdLine.hasOption("command")) {
        System.out.println("Mising required input/command arguments");
        new HelpFormatter().printHelp("SUPipeline", options);
        System.exit(1);//www . j a  v a  2 s  .c om
    }

    String clusterCwproot = null;
    if (cmdLine.hasOption("cwproot")) {
        clusterCwproot = cmdLine.getOptionValue("cwproot");
    }
    if (clusterCwproot == null || clusterCwproot.isEmpty()) {
        System.out.println("Could not determine cluster's CWPROOT value");
        new HelpFormatter().printHelp("SUPipeline", options);
        System.exit(1);
    }

    Pipeline pipeline = new MRPipeline(SUPipeline.class);
    PCollection<ByteBuffer> traces = pipeline
            .read(From.sequenceFile(cmdLine.getOptionValue("input"), Writables.bytes()));
    Pair<List<String>, String> cmd = parse(cmdLine.getOptionValue("command"));
    PCollection<ByteBuffer> result = constructPipeline(traces, clusterCwproot, cmd.first());

    if (cmdLine.hasOption("output")) {
        result.write(To.sequenceFile(cmdLine.getOptionValue("output")));
    }

    if (cmd.second() != null) {
        String localCwproot = System.getenv("CWPROOT");
        if (localCwproot == null) {
            System.out.println("To use local SU commands, the CWPROOT environment variable must be set");
            System.exit(1);
        }
        String[] pieces = cmd.second().split("\\s+");
        SUProcess x = new SUProcess(localCwproot, pieces[0]);
        for (int i = 1; i < pieces.length; i++) {
            x.addArg(pieces[i]);
        }
        x.addEnvironment(ImmutableMap.of("DISPLAY", System.getenv("DISPLAY")));
        Iterator<ByteBuffer> iter = result.materialize().iterator();
        x.start();
        while (iter.hasNext()) {
            ByteBuffer bb = iter.next();
            x.write(bb.array(), bb.arrayOffset(), bb.limit());
        }
        x.closeAndWait();
    }

    if (!cmdLine.hasOption("output") && cmd.second() == null) {
        System.out.println("No output destination specified");
        System.exit(1);
    }

    pipeline.done();
    return 0;
}

From source file:org.apache.tajo.tuple.offheap.OffHeapMemory.java

@VisibleForTesting
protected OffHeapMemory(ByteBuffer buffer, ResizableLimitSpec limitSpec) {
    this.buffer = buffer;
    this.address = ((DirectBuffer) buffer).address();
    this.memorySize = buffer.limit();
    this.limitSpec = limitSpec;
}

From source file:rascal.object.AbstractObjectFactoryTest.java

private void headerReadExpectation(final String header) throws Exception {
    context.checking(new Expectations() {
        {//  www .  j a  v  a 2 s.  c  o m
            oneOf(readableByteChannelMock).read(with(new TypeSafeMatcher<ByteBuffer>() {
                public boolean matchesSafely(ByteBuffer byteBuffer) {
                    return byteBuffer.limit() == HEADER_BUFFER_LENGTH;
                }

                public void describeTo(Description description) {
                    description.appendText(
                            String.format("Buffer for header should be %d bytes length", HEADER_BUFFER_LENGTH));
                }
            }));
            will(new CustomAction("writes header to buffer") {
                public Object invoke(Invocation invocation) throws Throwable {
                    ByteBuffer buffer = (ByteBuffer) invocation.getParameter(0);
                    buffer.put(header.getBytes());
                    buffer.put((byte) 0);
                    return HEADER_BUFFER_LENGTH;
                }
            });

            oneOf(readableByteChannelMock).close();
        }
    });
}

From source file:com.stratio.ingestion.sink.kafka.KafkaSinkTest.java

@Test
public void test() throws EventDeliveryException, UnsupportedEncodingException {
    Transaction tx = channel.getTransaction();
    tx.begin();/*  w ww.ja  va  2  s.c o  m*/

    ObjectNode jsonBody = new ObjectNode(JsonNodeFactory.instance);
    jsonBody.put("myString", "foo");
    jsonBody.put("myInt32", 32);

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("myString", "bar");
    headers.put("myInt64", "64");
    headers.put("myBoolean", "true");
    headers.put("myDouble", "1.0");
    headers.put("myNull", "foobar");

    Event event = EventBuilder.withBody(jsonBody.toString().getBytes(Charsets.UTF_8), headers);
    channel.put(event);

    tx.commit();
    tx.close();

    kafkaSink.process();

    kafka.api.FetchRequest req = new FetchRequestBuilder().clientId(CLIENT_ID).addFetch("test", 0, 0L, 100)
            .build();
    FetchResponse fetchResponse = simpleConsumer.fetch(req);
    ByteBufferMessageSet messageSet = fetchResponse.messageSet("test", 0);

    Assert.assertTrue(messageSet.sizeInBytes() > 0);
    for (MessageAndOffset messageAndOffset : messageSet) {
        ByteBuffer payload = messageAndOffset.message().payload();
        byte[] bytes = new byte[payload.limit()];
        payload.get(bytes);
        String message = new String(bytes, "UTF-8");
        Assert.assertNotNull(message);
        Assert.assertEquals(message, "{\"myString\":\"foo\",\"myInt32\":32}");
    }
}

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());
}

From source file:com.stratio.ingestion.sink.kafka.KafkaSinkTestIT.java

@Test
public void test() throws EventDeliveryException, UnsupportedEncodingException {
    Transaction tx = channel.getTransaction();
    tx.begin();/*from w ww  .  j a va 2 s .co  m*/

    ObjectNode jsonBody = new ObjectNode(JsonNodeFactory.instance);
    jsonBody.put("myString", "foo");
    jsonBody.put("myInt32", 32);

    Map<String, String> headers = new HashMap<String, String>();
    headers.put("myString", "bar");
    headers.put("myInt64", "64");
    headers.put("myBoolean", "true");
    headers.put("myDouble", "1.0");
    headers.put("myNull", "foobar");

    Event event = EventBuilder.withBody(jsonBody.toString().getBytes(Charsets.UTF_8), headers);
    channel.put(event);

    tx.commit();
    tx.close();

    kafkaSink.process();

    kafka.api.FetchRequest req = new FetchRequestBuilder().clientId(CLIENT_ID).addFetch("test", 0, 0L, 100)
            .build();
    FetchResponse fetchResponse = simpleConsumer.fetch(req);
    ByteBufferMessageSet messageSet = fetchResponse.messageSet("test", 0);

    for (MessageAndOffset messageAndOffset : messageSet) {
        ByteBuffer payload = messageAndOffset.message().payload();
        byte[] bytes = new byte[payload.limit()];
        payload.get(bytes);
        String message = new String(bytes, "UTF-8");
        Assert.assertNotNull(message);
        Assert.assertEquals(message, "{\"myString\":\"foo\",\"myInt32\":32}");
    }
}

From source file:org.redisson.codec.JsonJacksonCodec.java

private Object decode(ByteBuffer bytes) {
    try {/* w w  w  . j a v a  2s. c  o m*/
        return objectMapper.readValue(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(),
                Object.class);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.accumulo.core.security.AuthenticationTokenIdentifier.java

@Override
public void write(DataOutput out) throws IOException {
    if (null != impl) {
        ThriftMessageUtil msgUtil = new ThriftMessageUtil();
        ByteBuffer serialized = msgUtil.serialize(impl);
        out.writeInt(serialized.limit());
        out.write(serialized.array(), serialized.arrayOffset(), serialized.limit());
    } else {/*from   www  . j  a  v  a  2  s  .c o m*/
        out.writeInt(0);
    }
}

From source file:org.apache.accumulo.core.client.impl.AuthenticationTokenIdentifier.java

@Override
public void write(DataOutput out) throws IOException {
    if (null != impl) {
        ThriftMessageUtil msgUtil = new ThriftMessageUtil();
        ByteBuffer serialized = msgUtil.serialize(impl);
        out.writeInt(serialized.limit());
        ByteBufferUtil.write(out, serialized);
    } else {//from  w ww  .j a v a  2  s  . c  o m
        out.writeInt(0);
    }
}

From source file:org.apache.accumulo.core.clientImpl.AuthenticationTokenIdentifier.java

@Override
public void write(DataOutput out) throws IOException {
    if (impl != null) {
        ThriftMessageUtil msgUtil = new ThriftMessageUtil();
        ByteBuffer serialized = msgUtil.serialize(impl);
        out.writeInt(serialized.limit());
        ByteBufferUtil.write(out, serialized);
    } else {/*ww w  .ja va 2s.co  m*/
        out.writeInt(0);
    }
}