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:Main.java

public final static ByteBuffer borrowByteBufferNormal() {
    if (byteBuffersPoolNormal.isEmpty()) {
        return ByteBuffer.allocate(MAX_LINE_BYTES_NORMAL);
    } else {/*w  w w  . jav a 2 s  .c  o  m*/
        return byteBuffersPoolNormal.remove(0);
    }
}

From source file:com.icloud.framework.core.nio.ByteBufferUtil.java

public static ByteBuffer clone(ByteBuffer o) {
    assert o != null;

    if (o.remaining() == 0)
        return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY);

    ByteBuffer clone = ByteBuffer.allocate(o.remaining());

    if (o.isDirect()) {
        for (int i = o.position(); i < o.limit(); i++) {
            clone.put(o.get(i));/*from w ww.j a  v  a2 s.c o  m*/
        }
        clone.flip();
    } else {
        System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining());
    }

    return clone;
}

From source file:ConversionUtil.java

public static byte[] convertToByteArray(short value) {

    byte[] bytes = new byte[2];
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.putShort(value);/*from www  . j a  v  a 2 s  .  c  om*/
    return buffer.array();
    // buffer.get(bytes);
    /*
    for (int i =0;i<bytes.length; ++i) {
    int offset = (bytes.length -i-1) *8;
    bytes[i] = (byte)((value & (0xff << offset)) >>> offset);
    }
     */
    // return bytes;

}

From source file:com.alvermont.terraj.fracplanet.util.ByteBufferUtils.java

/**
 * The sizes of Java types are fixed and defined by the language spec so
 * this shouldn't be necessary but I'm going to do it anyway - so
 * there!//  ww  w  .j a  v a 2s .  c o  m
 *
 * @return The number of bytes occupied by an int value
 */
public static int sizeofInt() {
    final ByteBuffer buffer = ByteBuffer.allocate(10);

    buffer.putInt(0);

    return buffer.position();
}

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

ByteBuffer makeAFrame(int capacity, int count, int deletedBytes) throws HyracksDataException {
    ByteBuffer buffer = ByteBuffer.allocate(capacity);
    int metaOffset = capacity - 4;
    buffer.putInt(metaOffset, deletedBytes);
    metaOffset -= 4;//from  w  w w  . j  a  v a 2 s.com
    buffer.putInt(metaOffset, count);
    metaOffset -= 4;
    for (int i = 0; i < count; i++, metaOffset -= 4) {
        makeARecord(builder, i);
        for (int x = 0; x < builder.getFieldEndOffsets().length; x++) {
            buffer.putInt(builder.getFieldEndOffsets()[x]);
        }
        buffer.put(builder.getByteArray(), 0, builder.getSize());
        assert (metaOffset > buffer.position());
        buffer.putInt(metaOffset, buffer.position());

    }
    return buffer;
}

From source file:com.arpnetworking.tsdcore.sinks.KairosDbSink.java

/**
 * {@inheritDoc}//from www.jav  a 2 s . c  om
 */
@Override
protected Collection<byte[]> serialize(final PeriodicData periodicData) {
    // Initialize serialization structures
    final List<byte[]> completeChunks = Lists.newArrayList();
    final ByteBuffer currentChunk = ByteBuffer.allocate(_maxRequestSize);
    final ByteArrayOutputStream chunkStream = new ByteArrayOutputStream();

    // Extract and transform shared data
    final long timestamp = periodicData.getStart().plus(periodicData.getPeriod()).getMillis();
    final String serializedPeriod = periodicData.getPeriod().toString(ISOPeriodFormat.standard());
    final ImmutableMap<String, String> dimensions = periodicData.getDimensions();
    final Serializer serializer = new Serializer(timestamp, serializedPeriod, dimensions);

    // Initialize the chunk buffer
    currentChunk.put(HEADER);

    // Add aggregated data
    for (final AggregatedData datum : periodicData.getData()) {
        if (!datum.isSpecified()) {
            LOGGER.trace().setMessage("Skipping unspecified datum").addData("datum", datum).log();
            continue;
        }

        serializer.serializeDatum(completeChunks, currentChunk, chunkStream, datum);
    }

    // Add conditions
    for (final Condition condition : periodicData.getConditions()) {
        serializer.serializeCondition(completeChunks, currentChunk, chunkStream, condition);
    }

    // Add the current chunk (if any) to the completed chunks
    if (currentChunk.position() > HEADER_BYTE_LENGTH) {
        currentChunk.put(currentChunk.position() - 1, FOOTER);
        completeChunks.add(Arrays.copyOf(currentChunk.array(), currentChunk.position()));
    }

    return completeChunks;
}

From source file:com.rom.jmultipatcher.Utils.java

public static byte[] byteArrayfromInt(final int dataSize) {

    ByteBuffer intBB = ByteBuffer.allocate(4);
    return intBB.putInt(dataSize).array();
}

From source file:com.bah.culvert.util.Bytes.java

/**
 * Convert the integer to a byte []//from  ww  w.  j a  va  2 s . co m
 * @param i
 * @return
 */
public static byte[] toBytes(int i) {
    int bytes = Integer.SIZE / 8;

    return ByteBuffer.allocate(bytes).putInt(i).array();
}

From source file:Main.java

private static ByteBuffer allocateMore(ByteBuffer output) {
    if (output.capacity() == 0) {
        return ByteBuffer.allocate(1);
    }/* ww  w  .j a v  a2 s  .c  o m*/
    ByteBuffer result = ByteBuffer.allocate(output.capacity() * 2);
    output.flip();
    result.put(output);
    return result;
}

From source file:eu.stratosphere.arraymodel.io.StringInputFormat.java

@Override
public void configure(Configuration parameters) {
    super.configure(parameters);

    // get the charset for the decoding
    String charsetName = parameters.getString(CHARSET_NAME, DEFAULT_CHARSET_NAME);
    if (charsetName == null || !Charset.isSupported(charsetName)) {
        throw new RuntimeException("Unsupported charset: " + charsetName);
    }/*from  ww  w. java2 s  . c o  m*/

    if (charsetName.equals("ISO-8859-1") || charsetName.equalsIgnoreCase("ASCII")) {
        this.ascii = true;
    } else {
        this.decoder = Charset.forName(charsetName).newDecoder();
        this.byteWrapper = ByteBuffer.allocate(1);
    }
}