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

public void run() {
    try {//ww w . j a v a2s.  com
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(this.howMany);
        buffer.flip();
        while (buffer.hasRemaining())
            out.write(buffer);

        for (int i = 0; i < howMany; i++) {
            byte[] data = new BigInteger(Integer.toString(i)).toByteArray();
            buffer = ByteBuffer.allocate(4 + data.length);

            buffer.putInt(data.length);
            buffer.put(data);
            buffer.flip();

            while (buffer.hasRemaining())
                out.write(buffer);
        }
        out.close();
        System.err.println("Closed");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void transfer(InputStream in, SocketChannel out) throws IOException {
    byte[] buf = new byte[1024];
    ByteBuffer bbuf = ByteBuffer.allocate(1024);
    int len;/*from  www  .j  a va  2  s.c  om*/
    while ((len = in.read(buf)) != -1) {
        bbuf.put(buf, 0, len);
        bbuf.flip();
        while (bbuf.remaining() > 0)
            out.write(bbuf);
        bbuf.clear();
    }
}

From source file:ja.lingo.engine.util.EngineFiles.java

private static void appendFile(String fileName, FileOutputStream fos, boolean prependWithLength)
        throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    FileChannel fic = fis.getChannel();
    FileChannel foc = fos.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);

    // put header: length (1 int = 4 bytes)
    if (prependWithLength) {
        buffer.putInt((int) new File(fileName).length());
    }/* w w w  .j  a  v a 2s .co m*/

    // put body
    do {
        buffer.flip();
        foc.write(buffer);
        buffer.clear();
    } while (fic.read(buffer) != -1);
    fic.close();
    // NOTE: do not close 'foc'

    Files.delete(fileName);
}

From source file:com.datatorrent.contrib.hdht.PurgeTest.java

Slice newSlice(int i) {
    return new Slice(ByteBuffer.allocate(4).putInt(i).array());
}

From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java

@Override
public Double bytesToDouble(byte[] arg0) throws IOException {
    if (arg0 == null || arg0.length == 0) {
        return null;
    }// w  ww  .jav  a2s. c  om
    try {
        byte[] by = hex.decode(arg0);
        ByteBuffer bb = ByteBuffer.allocate(by.length);
        bb.put(by);
        bb.position(0);
        return bb.getDouble();
    } catch (Exception e) {
        LOG.error("failed to convert " + new String(arg0) + " to double");
        return null;
    }
}

From source file:com.pushtechnology.diffusion.examples.runnable.RandomData.java

/**
 * Serialize a {@link RandomData} value as a {@link Binary} value.
 * @param randomData The {@link RandomData} value
 * @return The {@link Binary} value//from   ww  w  .ja  v  a 2 s.c  om
 */
static Binary toBinary(RandomData randomData) {
    final ByteBuffer buffer = ByteBuffer.allocate(16);
    buffer.putInt(randomData.getId());
    buffer.putLong(randomData.getTimestamp());
    buffer.putInt(randomData.getRandomInt());
    return Diffusion.dataTypes().binary().readValue(buffer.array());
}

From source file:Main.java

private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
    int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(length);

    encoder.reset();/*from  w w  w.j av a  2s .c o m*/
    CoderResult flushResult = null;

    while (flushResult != CoderResult.UNDERFLOW) {
        CoderResult encodeResult = encoder.encode(in, out, true);
        if (encodeResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
            continue;
        }

        flushResult = encoder.flush(out);
        if (flushResult == CoderResult.OVERFLOW) {
            out = allocateMore(out);
        }
    }

    out.flip();
    return out;
}

From source file:com.davidcode.code.util.ErrorBuffer.java

/**
 * Creates an ErrorBuffer with a specified buffer size
 * Allocates two bytes for the length of the data for the server
 * to read// ww w .  j  a  va2s  .c  o m
 *
 * @param size
 */
public ErrorBuffer(int size) {
    buffer = ByteBuffer.allocate(size);
    buffer.position(2);
}

From source file:edu.berkeley.sparrow.examples.ProtoFrontendAsync.java

public static List<TTaskSpec> generateJob(int numTasks, int benchmarkId, int benchmarkIterations) {
    // Pack task parameters
    ByteBuffer message = ByteBuffer.allocate(8);
    message.putInt(benchmarkId);//from  w  ww.  jav  a  2s . c om
    message.putInt(benchmarkIterations);

    List<TTaskSpec> out = new ArrayList<TTaskSpec>();
    for (int taskId = 0; taskId < numTasks; taskId++) {
        TTaskSpec spec = new TTaskSpec();
        spec.setTaskId(Integer.toString(taskId));
        spec.setMessage(message.array());
        out.add(spec);
    }
    return out;
}

From source file:Main.java

public static byte[] padLeft(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }/*from   w w  w  . ja va2s. c  o  m*/
    ByteBuffer buffer = ByteBuffer.allocate(len);
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);

    buffer.put(paddings);
    buffer.put(tgt);

    return buffer.array();
}