Example usage for java.nio ByteBuffer arrayOffset

List of usage examples for java.nio ByteBuffer arrayOffset

Introduction

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

Prototype

public final int arrayOffset() 

Source Link

Document

Returns the offset of the byte array which this buffer is based on, if there is one.

Usage

From source file:org.apache.http.contrib.logging.Wire.java

public void input(final ByteBuffer b) {
    if (b.hasArray()) {
        input(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {/*w w  w .j a v a 2  s .co m*/
        byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        input(tmp);
    }
}

From source file:org.apache.http.contrib.logging.Wire.java

public void output(final ByteBuffer b) {
    if (b.hasArray()) {
        output(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {//from   w  w w .ja va  2  s  .  c  o  m
        byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        output(tmp);
    }
}

From source file:org.apache.http.HC4.impl.nio.conn.Wire.java

public void input(final ByteBuffer b) {
    if (b.hasArray()) {
        input(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {//from w w  w .j  av  a 2  s .  co m
        final byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        input(tmp);
    }
}

From source file:org.apache.http.HC4.impl.nio.conn.Wire.java

public void output(final ByteBuffer b) {
    if (b.hasArray()) {
        output(b.array(), b.arrayOffset() + b.position(), b.remaining());
    } else {//from   w  ww  .j  av  a  2 s. c  o m
        final byte[] tmp = new byte[b.remaining()];
        b.get(tmp);
        output(tmp);
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * from to /*from w  ww .  j  a  v a  2 s  . c o  m*/
 * 
 * @param fromBuffer
 *            Buffer ? flush
 * @param toBuffer
 *            Buffer ? fill
 * @return number of bytes moved
 */
public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) {
    int put;
    int remaining = fromBuffer.remaining();
    if (remaining > 0) { // 
        if (remaining <= toBuffer.remaining()) {
            toBuffer.put(fromBuffer);
            put = remaining;
            // from 
            fromBuffer.position(fromBuffer.limit());
        }
        // heap buffer
        else if (fromBuffer.hasArray()) {
            put = toBuffer.remaining();
            // ??
            toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put);
            fromBuffer.position(fromBuffer.position() + put);
        }
        // direct buffer
        else {
            // ??
            put = toBuffer.remaining();
            ByteBuffer slice = fromBuffer.slice();
            slice.limit(put);
            toBuffer.put(slice);
            fromBuffer.position(fromBuffer.position() + put);
        }
    } else {
        put = 0;
    }
    return put;
}

From source file:org.apache.avro.file.XZCodec.java

private void writeAndClose(ByteBuffer data, OutputStream to) throws IOException {
    byte[] input = data.array();
    int offset = data.arrayOffset() + data.position();
    int length = data.remaining();
    try {/* w  w w  .j a v a  2 s  .co m*/
        to.write(input, offset, length);
    } finally {
        to.close();
    }
}

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);/*from   ww  w  . j ava  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.redisson.codec.JsonJacksonCodec.java

private Object decode(ByteBuffer bytes) {
    try {/*w  w w. j a  va  2 s . co 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.redisson.codec.JsonJacksonCodec.java

@Override
public Object decodeMapValue(ByteBuffer bytes) {
    try {//from   www. j  ava  2s .c o  m
        return mapObjectMapper.readValue(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.limit(),
                Object.class);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.accumulo.server.security.delegation.AuthenticationKey.java

@Override
public void write(DataOutput out) throws IOException {
    if (null == authKey) {
        WritableUtils.writeVInt(out, 0);
        return;/*from w ww.  java 2 s  .c  om*/
    }
    ThriftMessageUtil util = new ThriftMessageUtil();
    ByteBuffer serialized = util.serialize(authKey);
    WritableUtils.writeVInt(out, serialized.limit() - serialized.arrayOffset());
    ByteBufferUtil.write(out, serialized);
}