Example usage for java.nio ByteBuffer get

List of usage examples for java.nio ByteBuffer get

Introduction

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

Prototype

public abstract byte get(int index);

Source Link

Document

Returns the byte at the specified index and does not change the position.

Usage

From source file:net.fenyo.mail4hotspot.dns.Msg.java

private byte[] process(final AdvancedServices advancedServices, final Inet4Address address)
        throws GeneralException {
    // log.debug("processing message of type " + input_buffer[0]);
    // for (byte b : input_buffer) log.debug("received byte: [" + b + "]");

    if (input_buffer.length == 0)
        throw new GeneralException("invalid size");
    if (input_buffer[0] == 0) {
        // UTF-8 message type

        final ByteBuffer bb = ByteBuffer.allocate(input_buffer.length - 1);
        bb.put(input_buffer, 1, input_buffer.length - 1);
        bb.position(0);// w  w w  .  j  a  v  a  2  s.  c  o m
        final String query = Charset.forName("UTF-8").decode(bb).toString();
        // log.debug("RECEIVED query: [" + query + "]");

        final String reply = advancedServices.processQueryFromClient(query, address);

        // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill output_buffer
        final ByteBuffer ob = Charset.forName("UTF-8").encode(reply);
        ob.get(output_buffer = new byte[ob.limit()]);

        output_size = output_buffer.length;

    } else {
        // binary message type
        // log.debug("processing binary message");

        final ByteBuffer bb = ByteBuffer.allocate(input_buffer[0]);
        bb.put(input_buffer, 1, input_buffer[0]);
        bb.position(0);
        final String query = Charset.forName("UTF-8").decode(bb).toString();
        //      log.debug("RECEIVED query: [" + query + "]");

        final BinaryMessageReply reply = advancedServices.processBinaryQueryFromClient(query,
                Arrays.copyOfRange(input_buffer, input_buffer[0] + 1, input_buffer.length), address);

        // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill string_part
        final ByteBuffer ob = Charset.forName("UTF-8").encode(reply.reply_string);
        final byte[] string_part = new byte[ob.limit()];
        ob.get(string_part);

        if (string_part.length > 255)
            throw new GeneralException("string_part too long");
        output_buffer = new byte[string_part.length + reply.reply_data.length + 1];
        output_buffer[0] = (byte) string_part.length;
        for (int i = 0; i < string_part.length; i++)
            output_buffer[i + 1] = string_part[i];
        for (int i = 0; i < reply.reply_data.length; i++)
            output_buffer[string_part.length + i + 1] = reply.reply_data[i];
        output_size = output_buffer.length;
    }

    synchronized (compressed) {
        // http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#deflate(byte[])
        // log.debug("processing binary message: length before compressing: " + output_buffer.length);
        final Deflater compresser = new Deflater();
        compresser.setInput(output_buffer);
        compresser.finish();
        final int nbytes = compresser.deflate(compressed);
        //         log.debug("RET: " + nbytes);
        //         log.debug("COMPRESSED: " + compressed.length);
        // log.debug("processing binary message: length after compressing: " + nbytes);
        if (compressed.length == nbytes) {
            log.error("compressed buffer too small...");
            throw new GeneralException("compressed buffer too small...");
        }
        output_buffer = Arrays.copyOf(compressed, nbytes);
        output_size = output_buffer.length;
    }

    synchronized (is_processed) {
        is_processed = true;
    }

    return new byte[] { 'E', 0 }; // 'E'rror 0 == OK
}

From source file:experts.net.ip6.ULUA.java

/**
 * Create Interface ID by the Modified EUI-64 format (RFC 4291)
 *
 * @param macAddr// w  w w.  ja v a  2 s  . c  o  m
 *            MAC (NIC) address
 */
public final void createInterfaceIDByEUI64(byte[] macAddr) {
    ByteBuffer buf = ByteBuffer.allocate(INTERFACE_ID_LENGTH);

    buf.put(macAddr, 0, 3).putShort(ADDITIONAL_VALUES).put(macAddr, 3, 3);

    buf.put(0, (byte) (buf.get(0) ^ 0x02));

    interfaceID = toList(buf);
}

From source file:ja.lingo.engine.mergedindex.ChannelMergedIndex.java

public ChannelMergedIndex(String fileName, Map<String, IDictionaryIndex> indexFileNameToReaderMap)
        throws IOException {
    Arguments.assertNotNull("fileName", fileName);
    Arguments.assertNotNull("indexFileNameToReaderMap", indexFileNameToReaderMap);

    this.fileName = fileName;

    fisMappedByteBufferWrapper = new MappedByteBufferWrapper(fileName);

    ISliceReader sliceReader = new ByteBufferSliceReader(fisMappedByteBufferWrapper.getMappedByteBuffer());

    // read position + fill readerIntIdToReaderMap
    {/*  w  ww.ja  v a  2  s  .  co  m*/
        ByteBuffer buffer = sliceReader.getSlice(SLICE_READER_INT_ID_TO_READER_ID_MAP);
        byte[] bytes = new byte[buffer.limit()];
        buffer.position(0);
        buffer.get(bytes);

        try {
            readerIntIdToReaderMap = _deserializeReaderIdToReaderMap(
                    new DataInputStream(new ByteArrayInputStream(bytes)), indexFileNameToReaderMap);
        } catch (IOException e) {
            Files.close(fisMappedByteBufferWrapper);
            throw e;
        }
    }

    groupsIndexLengthIntReader = new IntBufferIntReader(
            sliceReader.getSlice(SLICE_GROUPS_INDEX_LENGTH_LIST).asIntBuffer());
    groupsIntReader = new IntBufferIntReader(sliceReader.getSlice(SLICE_GROUPS_LIST).asIntBuffer());
}

From source file:com.cloudera.sqoop.TestAvroImport.java

/**
 * Helper method that runs an import using Avro with optional command line
 * arguments and checks that the created file matches the expectations.
 * <p/>//from   w w  w. j  av  a2  s .  c  o m
 * This can be used to test various extra options that are implemented for
 * the Avro input.
 *
 * @param extraArgs extra command line arguments to pass to Sqoop in addition
 *                  to those that {@link #getOutputArgv(boolean, String[])}
 *                  returns
 */
private void avroImportTestHelper(String[] extraArgs, String codec) throws IOException {
    String[] types = { "BIT", "INTEGER", "BIGINT", "REAL", "DOUBLE", "VARCHAR(6)", "VARBINARY(2)", "DATE",
            "TIMESTAMP", "TIME" };
    DateMidnight testDate = new LocalDate().toDateMidnight();
    LocalTime testTime = new LocalTime().withMillisOfSecond(0);
    // this example checks for wrong week-year conversion
    DateTime testTimestamp = testTime.toDateTime(new DateTime(2012, 12, 31, 0, 0, 0, 0));
    String timestampPattern = "YYYY-MM-dd HH:mm:ss.SSS";
    String datePattern = "YYYY-MM-dd";
    String[] vals = { "true", "100", "200", "1.0", "2.0", "'s'", "'0102'",
            singleQuote(DateTimeFormat.forPattern(datePattern).print(testDate)),
            singleQuote(DateTimeFormat.forPattern(timestampPattern).print(testTimestamp)),
            singleQuote(DateTimeFormat.forPattern("HH:mm:ss").print(testTime)) };
    createTableWithColTypes(types, vals);

    runImport(getOutputArgv(true, extraArgs));

    Path outputFile = new Path(getTablePath(), "part-m-00000.avro");
    DataFileReader<GenericRecord> reader = read(outputFile);
    Schema schema = reader.getSchema();
    assertEquals(Schema.Type.RECORD, schema.getType());
    List<Field> fields = schema.getFields();
    assertEquals(types.length, fields.size());

    checkField(fields.get(0), "DATA_COL0", Schema.Type.BOOLEAN);
    checkField(fields.get(1), "DATA_COL1", Schema.Type.INT);
    checkField(fields.get(2), "DATA_COL2", Schema.Type.LONG);
    checkField(fields.get(3), "DATA_COL3", Schema.Type.FLOAT);
    checkField(fields.get(4), "DATA_COL4", Schema.Type.DOUBLE);
    checkField(fields.get(5), "DATA_COL5", Schema.Type.STRING);
    checkField(fields.get(6), "DATA_COL6", Schema.Type.BYTES);
    checkField(fields.get(7), "DATA_COL7", Type.STRING);
    checkField(fields.get(8), "DATA_COL8", Type.STRING);
    checkField(fields.get(9), "DATA_COL9", Type.STRING);

    GenericRecord record1 = reader.next();
    assertEquals("DATA_COL0", true, record1.get("DATA_COL0"));
    assertEquals("DATA_COL1", 100, record1.get("DATA_COL1"));
    assertEquals("DATA_COL2", 200L, record1.get("DATA_COL2"));
    assertEquals("DATA_COL3", 1.0f, record1.get("DATA_COL3"));
    assertEquals("DATA_COL4", 2.0, record1.get("DATA_COL4"));
    assertEquals("DATA_COL5", new Utf8("s"), record1.get("DATA_COL5"));
    Object object = record1.get("DATA_COL6");
    assertTrue(object instanceof ByteBuffer);
    ByteBuffer b = ((ByteBuffer) object);
    assertEquals((byte) 1, b.get(0));
    assertEquals((byte) 2, b.get(1));
    assertEquals("DATA_COL7", testDate.toDateTime(),
            DateTimeFormat.forPattern(datePattern).parseDateTime(record1.get("DATA_COL7").toString()));
    assertEquals("DATA_COL8", testTimestamp,
            DateTimeFormat.forPattern(timestampPattern).parseDateTime(record1.get("DATA_COL8").toString()));
    assertEquals("DATA_COL9", testTime, DateTimeFormat.forPattern("HH:mm:ss.SSS")
            .parseDateTime(record1.get("DATA_COL9").toString()).toLocalTime());
    if (codec != null) {
        assertEquals(codec, reader.getMetaString(DataFileConstants.CODEC));
    }
}

From source file:com.homeadvisor.kafdrop.service.MessageInspector.java

private byte[] readBytes(ByteBuffer buffer, int offset, int size) {
    byte[] dest = new byte[size];
    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, size);
    } else {/*from  w w w.java 2 s  .  c o m*/
        buffer.mark();
        buffer.get(dest);
        buffer.reset();
    }
    return dest;
}

From source file:com.taobao.common.tfs.comm.TfsClient.java

public void invokeAsync(final BasePacket packet, final long timeout, ResponseListener listener) {
    if (isDebugEnabled) {
        log.debug("send request [" + packet.getChid() + "] async,time is:" + System.currentTimeMillis());
    }//  ww w  . j  a  v a 2s .co  m

    if (minTimeout > timeout) {
        minTimeout = timeout;
    }

    final ResponseCallbackTask callbackTask = new ResponseCallbackTask(packet.getSeqId(), listener, timeout);
    callbackTasks.put(packet.getChid(), callbackTask);

    ByteBuffer bb = packet.getByteBuffer();
    bb.flip();
    byte[] data = new byte[bb.remaining()];
    bb.get(data);
    WriteFuture writeFuture = session.write(data);
    writeFuture.addListener(new IoFutureListener() {

        public void operationComplete(IoFuture future) {
            WriteFuture wfuture = (WriteFuture) future;
            if (wfuture.isWritten()) {
                return;
            }
            String error = "send message to tfs server error [" + packet.getChid() + "], tfs server: "
                    + session.getRemoteAddress() + ", maybe because this connection closed: "
                    + !session.isConnected();
            callbackTask.setResponse(new TfsException(error));

            // close this session
            if (session.isConnected()) {
                session.close();
            } else {
                TfsClientFactory.getInstance().removeClient(key);
            }
        }

    });
}

From source file:com.offbynull.portmapper.pcp.PeerPcpResponse.java

/**
 * Constructs a {@link PeerPcpResponse} object by parsing a buffer.
 * @param buffer buffer containing PCP response data
 * @throws NullPointerException if any argument is {@code null}
 * @throws BufferUnderflowException if not enough data is available in {@code buffer}
 * @throws IllegalArgumentException if there's not enough or too much data remaining in the buffer, or if the version doesn't match the
 * expected version (must always be {@code 2}), or if the r-flag isn't set, or if there's an unsuccessful/unrecognized result code,
 * or if the op code doesn't match the PEER opcode, or if the response has a {@code 0} for its {@code internalPort} or
 * {@code assignedExternalPort} or {@code remotePeerPort} or {@code protocol} field, or if there were problems parsing options
 *//*from   www.  j  a va  2  s  . c o  m*/
public PeerPcpResponse(ByteBuffer buffer) {
    super(buffer);

    Validate.isTrue(super.getOp() == 2);

    mappingNonce = ByteBuffer.allocate(12);
    buffer.get(mappingNonce.array());
    mappingNonce = mappingNonce.asReadOnlyBuffer();
    this.protocol = buffer.get() & 0xFF;

    for (int i = 0; i < 3; i++) { // reserved block
        buffer.get();
    }

    this.internalPort = buffer.getShort() & 0xFFFF;
    this.assignedExternalPort = buffer.getShort() & 0xFFFF;
    byte[] addrArr = new byte[16];
    buffer.get(addrArr);
    try {
        this.assignedExternalIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                            // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }
    this.remotePeerPort = buffer.getShort() & 0xFFFF;

    for (int i = 0; i < 2; i++) { // reserved block
        buffer.get();
    }

    buffer.get(addrArr);
    try {
        this.remotePeerIpAddress = InetAddress.getByAddress(addrArr); // should automatically shift down to ipv4 if ipv4-to-ipv6
                                                                      // mapped address
    } catch (UnknownHostException uhe) {
        throw new IllegalArgumentException(uhe); // should never happen, will always be 16 bytes
    }

    Validate.inclusiveBetween(1, 255, protocol);
    Validate.inclusiveBetween(1, 65535, internalPort);
    Validate.inclusiveBetween(1, 65535, assignedExternalPort);
    Validate.inclusiveBetween(1, 65535, remotePeerPort);

    parseOptions(buffer);
}

From source file:com.taobao.tair.comm.TairClient.java

public void invokeAsync(final BasePacket packet, final long timeout, ResponseListener listener) {
    if (isDebugEnabled) {
        LOGGER.debug("send request [" + packet.getChid() + "] async,time is:" + System.currentTimeMillis());
    }//from w  ww  .  j  a  va 2 s .  c  o  m
    if (minTimeout > timeout) {
        minTimeout = timeout;
    }
    final ResponseCallbackTask callbackTask = new ResponseCallbackTask(packet.getChid(), listener, this.session,
            timeout);
    callbackTasks.put(packet.getChid(), callbackTask);

    ByteBuffer bb = packet.getByteBuffer();
    bb.flip();
    byte[] data = new byte[bb.remaining()];
    bb.get(data);
    WriteFuture writeFuture = session.write(data);
    writeFuture.addListener(new IoFutureListener() {

        public void operationComplete(IoFuture future) {
            WriteFuture wfuture = (WriteFuture) future;
            if (wfuture.isWritten()) {
                return;
            }
            String error = "send message to tair server error [" + packet.getChid() + "], tair server: "
                    + session.getRemoteAddress() + ", maybe because this connection closed :"
                    + !session.isConnected();
            LOGGER.warn(error);
            callbackTask.setResponse(new TairClientException(error));

            // close this session
            if (session.isConnected())
                session.close();
            else
                clientFactory.removeClient(key);
        }

    });
}

From source file:com.l2jfree.security.NewCipher.java

/**
 * Verifies a packet's checksum./*www .  j a  v  a 2  s.  c  o  m*/
 * 
 * @param buf byte buffer
 * @param offset offset to a packet's body
 * @param size packet's body size
 * @param experimental undocumented experimental features
 * @param report whether to report checksum validation failures
 * @return whether packet integrity is OK or not
 */
public static boolean verifyChecksum(ByteBuffer buf, final int offset, final int size, boolean experimental,
        boolean report) {
    // FIXME: this whole method is most likely a big hoax
    // there is no checksum and definitely no validation!

    // check if size is multiple of 4 (and > 0)
    if ((size & 3) != 0 || size <= 4) {
        if (report)
            reportSoCalledChecksum(null, offset, size, 0, 0);
        return false;
    }

    long calculated = 0;
    int end = offset + size - 4; // ignore embedded checksum

    int pos;
    for (pos = offset; pos < end; pos += 4) {
        final long i = buf.getInt(pos);
        calculated ^= (i & 0xffffffff);
    }

    long real = buf.getInt(pos);
    real &= 0xffffffff;
    if (experimental && calculated != real) // someone knows a better scheme?
        _checks.put(buf.get(offset), (int) real); // let them have it

    reportSoCalledChecksum(buf, offset, size, calculated, real);

    return (calculated == real);
}

From source file:org.wso2.carbon.http2.transport.util.http2Encoder.java

@Override
public int write(ByteBuffer src) throws IOException {
    int l = 0;// w w w.j  a va2 s.c  o  m
    //channel.newPromise();

    while (src.hasRemaining()) {
        byte[] b;//= new byte[chContext.channel().alloc().buffer().capacity()];
        //  if(src.remaining()<b.length){
        b = new byte[src.remaining()];
        src.get(b);
        // request.replace(Unpooled.wrappedBuffer(b));
        if (src.hasRemaining())
            encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, false, promise);
        else {
            encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, true, promise);
            isComplete = true;
        }
    }

    return src.position();
}