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:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
public void onResiveClientData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    long opcode = 0;
    long size = 0;
    byte[] data;//from   w w  w  . j av a  2 s  . c o  m
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;
        opcode = (header & 0x1FFF);
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        // old (opcode == 0x1a72) | (opcode == 0x3f3)
        if ((opcode == 0x1aa1) | (opcode == 0x9f1)) {
            this.log.debug("Init cryptography system for channel {}", this.getChannelId());
            addChannelState(State.AUTHED);
            this.csPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
            this.scPacketBuffer.getCrypt().init(this.woWKeyReader.getKey());
        }
    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x\n", frameNumber, "CMSG", opcode));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.CLIENT, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:com.navercorp.pinpoint.collector.receiver.thrift.udp.SpanStreamUDPPacketHandlerFactory.java

private byte[] getComponentData(ByteBuffer buffer, HeaderTBaseDeserializer deserializer) {
    if (buffer.remaining() < 2) {
        logger.warn("Can't available {} fixed buffer.", 2);
        return null;
    }/*from   w w w  .j  a  v  a2s . c o  m*/

    int componentSize = 0xffff & buffer.getShort();
    if (buffer.remaining() < componentSize) {
        logger.warn("Can't available {} fixed buffer.", buffer.remaining());
        return null;
    }

    byte[] componentData = new byte[componentSize];
    buffer.get(componentData);

    return componentData;
}

From source file:android.syncml.pim.vcard.VCardDataBuilder.java

private String encodeString(String originalString, String targetCharset) {
    if (mSourceCharset.equalsIgnoreCase(targetCharset)) {
        return originalString;
    }//w w  w. jav  a2  s.  c  om
    Charset charset = Charset.forName(mSourceCharset);
    ByteBuffer byteBuffer = charset.encode(originalString);
    // byteBuffer.array() "may" return byte array which is larger than
    // byteBuffer.remaining(). Here, we keep on the safe side.
    byte[] bytes = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytes);
    try {
        return new String(bytes, targetCharset);
    } catch (UnsupportedEncodingException e) {
        Log.e(LOG_TAG, "Failed to encode: charset=" + targetCharset);
        return new String(bytes);
    }
}

From source file:org.jmangos.sniffer.network.model.impl.WOWNetworkChannel.java

@Override
synchronized public void onResiveServerData(final long frameNumber, final byte[] buffer) {
    final ByteBuffer bytebuf = ByteBuffer.wrap(buffer);
    bytebuf.order(ByteOrder.LITTLE_ENDIAN);
    long opcode = 0;
    long size = 0;
    byte[] data = null;
    if (getChannelState().contains(State.AUTHED)) {
        final long header = bytebuf.getInt() & 0xFFFFFFFF;
        size = header >> 13;//w  w w .jav  a 2  s .  c  o  m
        opcode = header & 0x1FFF;
        data = new byte[(int) size];
        bytebuf.get(data);
    } else {
        size = bytebuf.getShort();
        opcode = bytebuf.getInt();
        bytebuf.mark();
        data = new byte[(int) size - 4];
        bytebuf.get(data);
        bytebuf.reset();
        // old 0xc0b
        if ((opcode == 0x221) & !getChannelState().contains(State.NOT_ACCEPT_SEED)) {
            this.log.debug("Get new Seed");
            final byte[] serverSeed = new byte[16];
            final byte[] clientSeed = new byte[16];
            for (int i = 0; i < 16; i++) {
                serverSeed[i] = bytebuf.get();
            }
            for (int i = 0; i < 16; i++) {
                clientSeed[i] = bytebuf.get();
            }
            bytebuf.get();
            this.csPacketBuffer.getCrypt().setEncryptionSeed(clientSeed);
            this.scPacketBuffer.getCrypt().setEncryptionSeed(serverSeed);
        }

    }
    this.log.debug(String.format("Frame: %d; Resive packet %s Opcode: 0x%x OpcodeSize: %d", frameNumber, "SMSG",
            opcode, size));
    for (final PacketLogHandler logger : this.packetLoggers) {
        logger.onDecodePacket(this, Direction.SERVER, (int) size, (int) opcode, data, (int) frameNumber);
    }
}

From source file:cn.iie.haiep.hbase.value.Bytes.java

/**
 * This method will get a sequence of bytes from pos -> limit,
 * but will restore pos after./*from w w  w  .j ava 2 s.co  m*/
 * @param buf
 * @return byte array
 */
public static byte[] getBytes(ByteBuffer buf) {
    int savedPos = buf.position();
    byte[] newBytes = new byte[buf.remaining()];
    buf.get(newBytes);
    buf.position(savedPos);
    return newBytes;
}

From source file:AutoDJ.metaReader.OggIndexer.java

/**
 * Extracts the Image from a FLAC picture structure
 * http://flac.sourceforge.net/format.html#metadata_block_picture
 * Expects a base64 encoded string/*from www. ja  v a2 s.  c  o  m*/
 * 
 * @param String pictureBlock (base64)
 * @return BufferedImage
 */
BufferedImage readAlbumImage(String pictureBlock) {
    if (pictureBlock == null || pictureBlock.isEmpty())
        return null;

    byte[] pictureBytes = Base64.decodeBase64(pictureBlock);
    BufferedImage img = null;

    String mimeString = "", description = "";
    ByteBuffer picBuff = ByteBuffer.allocate(pictureBytes.length);
    picBuff.put(pictureBytes);
    picBuff.rewind();

    /*int picType = */picBuff.getInt(); // not interesting, discard

    // read the mime type string
    int mimeStrLength = picBuff.getInt();
    byte[] mimeBytes = new byte[mimeStrLength];
    picBuff.get(mimeBytes);
    mimeString = new String(mimeBytes);

    // read the string describing the image 
    int descStrLength = picBuff.getInt();
    byte[] descBytes = new byte[descStrLength];
    picBuff.get(descBytes);
    try {
        description = new String(descBytes, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // skip over some unnecessary information
    /*int picWidth  = */picBuff.getInt();
    /*int picHeight = */picBuff.getInt();
    /*int colDepth  = */picBuff.getInt();
    /*int idxColors = */picBuff.getInt();

    // read the image data
    int picDataLength = picBuff.getInt();
    byte[] picBytes = new byte[picDataLength];
    picBuff.get(picBytes);
    try {
        img = ImageIO.read(new ByteArrayInputStream(picBytes));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return img;
}

From source file:io.druid.query.aggregation.HyperloglogAggregatorFactory.java

@Override
public Object deserialize(Object object) {
    log.debug("class name: [%s]:value [%s]", object.getClass().getName(), object);

    final String k = (String) object;
    final byte[] ibmapByte = Base64.decodeBase64(k);

    final ByteBuffer buffer = ByteBuffer.wrap(ibmapByte);
    final int keylength = buffer.getInt();
    final int valuelength = buffer.getInt();

    TIntByteHashMap newIbMap;/*from www  .j a  v  a  2s. co  m*/

    if (keylength == 0) {
        newIbMap = new TIntByteHashMap();
    } else {
        final int[] keys = new int[keylength];
        final byte[] values = new byte[valuelength];

        for (int i = 0; i < keylength; i++) {
            keys[i] = buffer.getInt();
        }
        buffer.get(values);

        newIbMap = new TIntByteHashMap(keys, values);
    }

    return newIbMap;
}

From source file:com.talis.storage.s3.S3StoreTest.java

@Override
protected Store getStore() throws Exception {
    bucketname = UUID.randomUUID().toString();
    Provider<S3Service> serviceProvider = initialiseServiceProvider();

    final S3Object stubObject = new S3Object("foo");
    objectFactory = new S3ObjectFactory(bucketname) {
        @Override/*from  w w  w .j  av a  2  s  .c o m*/
        public ExternalizableS3Object newObject(String key, int index, MediaType mediaType, ByteBuffer buffer)
                throws IOException {
            ExternalizableS3Object obj = new ExternalizableS3Object();
            obj.setKey(key + "/" + index);
            obj.setContentType(S3Store.TMB_CHUNK_TYPE.toString());
            obj.addMetadata(S3Store.ACTUAL_CONTENT_TYPE_HEADER, mediaType.toString());
            byte[] bytes = new byte[buffer.position()];
            buffer.rewind();
            buffer.get(bytes);
            obj.setDataInputStream(new ByteArrayInputStream(bytes));
            return obj;
        }
    };
    chunkHandler = new StubChunkHandler();

    return new S3Store(objectFactory, chunkHandler);
}

From source file:edu.uci.ics.hyracks.dataflow.std.sort.util.DeletableFrameTupleAppenderTest.java

@Test
public void testAppend() throws Exception {
    int count = 10;
    ByteBuffer bufferRead = makeAFrame(cap, count, 0);
    DeletableFrameTupleAppender accessor = new DeletableFrameTupleAppender(recordDescriptor);
    accessor.reset(bufferRead);//from www  . j a  v a2 s .c o m
    ByteBuffer bufferWrite = ByteBuffer.allocate(cap);
    appender.clear(bufferWrite);
    for (int i = 0; i < accessor.getTupleCount(); i++) {
        appender.append(accessor, i);
    }
    for (int i = 0; i < bufferRead.capacity(); i++) {
        assertEquals(bufferRead.get(i), bufferWrite.get(i));
    }
}