Example usage for java.nio ByteBuffer allocate

List of usage examples for java.nio ByteBuffer allocate

Introduction

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

Prototype

public static ByteBuffer allocate(int capacity) 

Source Link

Document

Creates a byte buffer based on a newly allocated byte array.

Usage

From source file:com.linkedin.databus.client.pub.TestUnifiedClientStats.java

private DbusEvent createEvent(long timestampNs) {
    DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 6004L, // SCN
            (short) 1, // physical partition ID
            (short) 1, // logical partition ID
            timestampNs, (short) 1, // srcId
            SOURCE1_SCHEMAID, // payloadSchemaMd5
            SOURCE1_PAYLOAD, // payload
            false, // enableTracing
            true); // autocommit
    DbusEventKey key = new DbusEventKey("myKey".getBytes(Charset.forName("UTF-8")));
    ByteBuffer buf = ByteBuffer.allocate(1000).order(ByteOrder.BIG_ENDIAN);
    try {//  ww w .  j av a 2s .c  om
        DbusEventFactory.serializeEvent(key, buf, eventInfo);
    } catch (KeyTypeNotImplementedException ex) {
        fail("string key type not supported by DbusEventV2Factory?!? " + ex.getLocalizedMessage());
    }
    return _eventFactory.createReadOnlyDbusEventFromBuffer(buf, 0);
}

From source file:com.ebay.pulsar.analytics.metricstore.druid.query.model.GroupByQuery.java

@Override
public byte[] cacheKey() {
    BaseFilter filter = this.getFilter();
    byte[] filterBytes = filter == null ? new byte[] {} : filter.cacheKey();
    byte[] aggregatorBytes = QueryCacheHelper.computeAggregatorBytes(this.getAggregations());
    byte[] granularityBytes = null;
    if (this.getGranularity() instanceof BaseGranularity) {
        granularityBytes = ((BaseGranularity) this.getGranularity()).cacheKey();
    } else {//from   ww w.  ja  va 2 s.co m
        granularityBytes = new byte[0];
    }
    byte[] intervalsBytes = QueryCacheHelper.computeIntervalsBytes(this.getIntervals());
    final byte[][] dimensionsBytes = new byte[this.getDimensions().size()][];
    int dimensionsBytesSize = 0;
    int index = 0;
    for (String dimension : this.getDimensions()) {
        dimensionsBytes[index] = dimension.getBytes();
        dimensionsBytesSize += dimensionsBytes[index].length;
        ++index;
    }
    final byte[] limitBytes = this.getLimitSpec().cacheKey();

    BaseHaving having = this.getHaving();
    byte[] havingBytes = having == null ? new byte[] {} : having.cacheKey();

    List<BasePostAggregator> postaggregators = this.getPostAggregations();
    byte[] postaggregatorBytes = postaggregators == null ? new byte[] {}
            : QueryCacheHelper.computePostAggregatorBytes(postaggregators);

    ByteBuffer buffer = ByteBuffer
            .allocate(1 + granularityBytes.length + filterBytes.length + aggregatorBytes.length
                    + dimensionsBytesSize + limitBytes.length + havingBytes.length + intervalsBytes.length
                    + postaggregatorBytes.length)
            .put(GROUPBY_QUERY).put(granularityBytes).put(filterBytes).put(aggregatorBytes)
            .put(postaggregatorBytes);

    for (byte[] dimensionsByte : dimensionsBytes) {
        buffer.put(dimensionsByte);
    }
    return buffer.put(limitBytes).put(havingBytes).put(intervalsBytes).array();
}

From source file:com.stainlesscode.mediapipeline.audioout.DefaultAudioPlayer.java

private void allocateBuffers() {
    if (bytesPerFrame > 0) {
        if (LogUtil.isDebugEnabled()) {
            LogUtil.debug("bytesPerFrame is " + bytesPerFrame);
            LogUtil.debug("Allocating audio buffers " + bufferSizeInFrames + "*" + bytesPerFrame);
        }/*from   w  w  w . j a va2s .  c  o m*/
        bufferSizeInBytes = bufferSizeInFrames * bytesPerFrame;
        smoothingBuffer = ByteBuffer.allocate(bufferSizeInBytes);
        playBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes);
    }
}

From source file:com.smartitengineering.util.simple.io.BufferedInputStream.java

protected int initializeNewBuffer() {
    int remaining;
    final int capacity;
    if (bufferSegmentSize > 100) {
        capacity = bufferSegmentSize;//  ww  w.  j a  va2  s  . c  om
    } else {
        capacity = bufferSegmentSize / 10;
    }
    currentBuffer = ByteBuffer.allocate(capacity);
    currentBuffer.limit(bufferSegmentSize);
    remaining = currentBuffer.remaining();
    buffers.put(currentBuffer, new MutableInt(0));
    return remaining;
}

From source file:de.csdev.ebus.command.EBusCommandUtils.java

/**
 * Builds an escaped master telegram part or if slaveData is used a complete telegram incl. master ACK and SYN
 *
 * @param source/*from  ww w.  j a  va  2 s .c  om*/
 * @param target
 * @param command
 * @param masterData
 * @return
 * @throws EBusTypeException
 */
public static ByteBuffer buildPartMasterTelegram(byte source, byte target, byte[] command, byte[] masterData)
        throws EBusTypeException {

    ByteBuffer buf = ByteBuffer.allocate(50);

    buf.put(source); // QQ - Source
    buf.put(target); // ZZ - Target
    buf.put(command); // PB SB - Command
    buf.put((byte) masterData.length); // NN - Length, will be set later

    // add the escaped bytes
    for (byte b : masterData) {
        buf.put(escapeSymbol(b));
    }

    // calculate crc
    byte crc8 = EBusUtils.crc8(buf.array(), buf.position());

    buf.put(escapeSymbol(crc8));

    // set limit and reset position
    buf.limit(buf.position());
    buf.position(0);

    return buf;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

private ByteBuffer fuse(final int length) {
    //fuses the buffers into a single array of the target length
    final ByteBuffer bb = ByteBuffer.allocate(length);

    for (byte[] buffer : fBuffers) {
        if (buffer.length > length - bb.position()) {
            bb.put(buffer, 0, length - bb.position());
        } else {//from   w  ww.  j a v a  2s .c o m
            bb.put(buffer);
        }
    }

    //important
    bb.flip();
    fBuffers.clear();

    return bb;
}

From source file:gobblin.writer.SimpleDataWriter.java

/**
 * Write a source record to the staging file
 *
 * @param record data record to write//from w w  w. j a  v a2 s .  c o m
 * @throws java.io.IOException if there is anything wrong writing the record
 */
@Override
public void write(byte[] record) throws IOException {
    Preconditions.checkNotNull(record);

    byte[] toWrite = record;
    if (this.recordDelimiter.isPresent()) {
        toWrite = Arrays.copyOf(record, record.length + 1);
        toWrite[toWrite.length - 1] = this.recordDelimiter.get();
    }
    if (this.prependSize) {
        long recordSize = toWrite.length;
        ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES);
        buf.putLong(recordSize);
        toWrite = ArrayUtils.addAll(buf.array(), toWrite);
    }
    this.stagingFileOutputStream.write(toWrite);
    this.bytesWritten += toWrite.length;
    this.recordsWritten++;
}

From source file:com.reactive.hzdfs.utils.Digestor.java

/**
 * // ww w . j  a v a2  s  . c  o  m
 * @param l
 * @return
 */
public Digestor addLong(long l) {
    if (md != null)
        md.update(ByteBuffer.allocate(8).putLong(l));
    else
        updateBytes(ByteBuffer.allocate(8).putLong(l));
    return this;
}

From source file:net.sf.jml.message.p2p.MsnP2PMessage.java

/**
 * @see MsnMimeMessage#toOutgoingMsg(MsnProtocol)
 */// ww  w .  j av a2s . co m
@Override
public OutgoingMSG[] toOutgoingMsg(MsnProtocol protocol) {
    OutgoingMSG message = new OutgoingMSG(protocol);
    message.setMsgType(OutgoingMSG.TYPE_MSNC1);

    byte[] mimeMessageHeader = Charset.encodeAsByteArray(toString());

    byte[] body = bodyToMessage();
    if (body == null) {
        body = new byte[0];
    }

    ByteBuffer msg = ByteBuffer
            .allocate(mimeMessageHeader.length + BINARY_HEADER_LEN + body.length + BINARY_FOOTER_LEN);

    msg.put(mimeMessageHeader);
    msg.put(binaryHeader);
    msg.put(body);
    msg.put(binaryFooter);
    message.setMsg(msg.array());
    return new OutgoingMSG[] { message };
}

From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java

/**
 * Load Stored data/*from w  w w  . j  a  v  a  2s  . co  m*/
 *
 * @param input  input stream to read the double array trie from
 * @return double array trie, not null
 * @throws IOException if an IO error occured during reading the double array trie
 */
public static DoubleArrayTrie read(InputStream input) throws IOException {
    DoubleArrayTrie trie = new DoubleArrayTrie();
    DataInputStream dis = new DataInputStream(new BufferedInputStream(input));

    trie.compact = dis.readBoolean();
    int baseCheckSize = dis.readInt(); // Read size of baseArr and checkArr
    int tailSize = dis.readInt(); // Read size of tailArr
    ReadableByteChannel channel = Channels.newChannel(dis);

    ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpBaseBuffer);
    tmpBaseBuffer.rewind();
    trie.baseBuffer = tmpBaseBuffer.asIntBuffer();

    ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4);
    channel.read(tmpCheckBuffer);
    tmpCheckBuffer.rewind();
    trie.checkBuffer = tmpCheckBuffer.asIntBuffer();

    ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2);
    channel.read(tmpTailBuffer);
    tmpTailBuffer.rewind();
    trie.tailBuffer = tmpTailBuffer.asCharBuffer();

    input.close();
    return trie;
}