Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

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

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.streamsets.pipeline.stage.origin.maprjson.MapRJsonOriginSource.java

private Record createRecordFromJSON(String lastSourceOffset, Document document) throws StageException {
    Record record;/*from  w w  w . j  a  va 2 s  . c om*/
    DataParser parser;

    try {
        if (jsonDataTypes.get(MAPR_ID) == Value.Type.BINARY) {
            String str;
            ByteBuffer bb = document.getIdBinary();

            switch (bb.array().length) {
            case 8:
                Long l = Bytes.toLong(bb.array());
                str = l.toString();
                break;
            case 4:
                Integer i = Bytes.toInt(bb.array());
                str = i.toString();
                break;

            case 2:
                Short s = Bytes.toShort(bb.array());
                str = s.toString();
                break;

            default:
                str = Bytes.toString(bb.array());
                break;
            }
            document.set(MAPR_ID, str);
        }

        if ((parser = parserFactory.getParser(lastSourceOffset, document.asJsonString())) == null) {
            LOG.error(Errors.MAPR_JSON_ORIGIN_05.getMessage());
            throw new OnRecordErrorException(Errors.MAPR_JSON_ORIGIN_05);
        }
    } catch (DataParserException ex) {
        LOG.error(Errors.MAPR_JSON_ORIGIN_05.getMessage(), ex);
        throw new OnRecordErrorException(Errors.MAPR_JSON_ORIGIN_05, ex);
    }

    try {
        record = parser.parse();
    } catch (IOException | DataParserException ex) {
        LOG.error(Errors.MAPR_JSON_ORIGIN_04.getMessage(), ex);
        throw new OnRecordErrorException(Errors.MAPR_JSON_ORIGIN_04, ex);
    }
    return record;

}

From source file:com.codeabovelab.dm.common.security.token.SignedTokenServiceBackend.java

private byte[] contentPack(long creationTime, byte[] random, byte[] serverSecret, byte[] payload) {
    ByteBuffer buffer = ByteBuffer
            .allocate(8 + 1 + random.length + 1 + serverSecret.length + 1 + payload.length);
    buffer.putLong(creationTime);//from  ww w . j a v  a  2 s. c  om
    store(buffer, random);
    store(buffer, serverSecret);
    store(buffer, payload);
    return buffer.array();
}

From source file:com.linkedin.pinot.core.segment.index.creator.SegmentGenerationWithBytesTypeTest.java

/**
 * Build Avro file containing serialized TDigest bytes.
 *
 * @param schema Schema of data (one fixed and one variable column)
 * @param _fixedExpected Serialized bytes of fixed length column are populated here
 * @param _varExpected Serialized bytes of variable length column are populated here
 * @throws IOException/*  w  w w .ja  va2  s  . c  o  m*/
 */
private void buildAvro(Schema schema, List<byte[]> _fixedExpected, List<byte[]> _varExpected)
        throws IOException {
    org.apache.avro.Schema avroSchema = AvroUtils.getAvroSchemaFromPinotSchema(schema);

    try (DataFileWriter<GenericData.Record> recordWriter = new DataFileWriter<>(
            new GenericDatumWriter<>(avroSchema))) {

        if (!new File(AVRO_DIR_NAME).mkdir()) {
            throw new RuntimeException("Unable to create test directory: " + AVRO_DIR_NAME);
        }

        recordWriter.create(avroSchema, new File(AVRO_DIR_NAME, AVRO_NAME));
        for (int i = 0; i < NUM_ROWS; i++) {
            GenericData.Record record = new GenericData.Record(avroSchema);

            TDigest tDigest = new TDigest(PercentileTDigestAggregationFunction.DEFAULT_TDIGEST_COMPRESSION);
            tDigest.add(_random.nextDouble());

            ByteBuffer buffer = ByteBuffer.allocate(tDigest.byteSize());
            tDigest.asBytes(buffer);
            _fixedExpected.add(buffer.array());

            buffer.flip();
            record.put(FIXED_BYTES_UNSORTED_COLUMN, buffer);

            if (i % 2 == 0) {
                tDigest.add(_random.nextDouble());
            }

            buffer = ByteBuffer.allocate(tDigest.byteSize());
            tDigest.asBytes(buffer);
            _varExpected.add(buffer.array());

            buffer.flip();
            record.put(VARIABLE_BYTES_COLUMN, buffer);

            recordWriter.append(record);
        }
    }
}

From source file:io.protostuff.JsonOutput.java

/**
 * Writes a ByteBuffer field.//w w w. j  a  va  2  s.c  o m
 */
@Override
public void writeBytes(int fieldNumber, ByteBuffer value, boolean repeated) throws IOException {
    writeByteRange(false, fieldNumber, value.array(), value.arrayOffset() + value.position(), value.remaining(),
            repeated);
}

From source file:de.micromata.genome.logging.spi.ifiles.IndexHeader.java

public void writeFileHeader(OutputStream os, File indexFile, IndexDirectory indexDirectory) throws IOException {
    indexDirectoryIdx = indexDirectory.createNewLogIdxFile(indexFile);
    ByteBuffer lbb = ByteBuffer.wrap(new byte[Long.BYTES]);
    ByteBuffer ibb = ByteBuffer.wrap(new byte[Integer.BYTES]);
    os.write(INDEX_FILE_TYPE);//  w  w  w.j a  v  a  2s. co m
    os.write(INDEX_FILE_VERSION);
    lbb.putLong(0, System.currentTimeMillis());
    os.write(lbb.array());
    ibb.putInt(0, indexDirectoryIdx);
    os.write(ibb.array());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    for (Pair<String, Integer> headerp : headerOrder) {
        String hn = StringUtils.rightPad(headerp.getFirst(), HEADER_NAME_LENGTH);
        bout.write(hn.getBytes());
        ibb.putInt(0, headerp.getSecond());
        bout.write(ibb.array());
    }
    byte[] headerar = bout.toByteArray();
    int idxOffset = FILE_TYPE_LENGTH + FILE_VERSION_LENGTH + Long.BYTES /* timestamp */
            + Integer.BYTES /** indexDirectory */
            + Integer.BYTES /* indexOfset */
            + headerar.length;
    ibb.putInt(0, idxOffset);
    os.write(ibb.array());
    os.write(headerar);
    os.flush();
}

From source file:org.muckebox.android.net.DownloadRunnable.java

private void handleReceivedData(ByteBuffer data) throws IOException {
    if (!BufferUtils.isEmpty(data)) {
        mBytesTotal += data.position();/*from w  ww.j av  a  2s. co m*/

        if (mOutputStream != null) {
            mOutputStream.write(data.array(), 0, data.position());
        }

        Chunk chunk = new Chunk();

        chunk.bytesTotal = mBytesTotal;
        chunk.buffer = data;

        if (mHandler != null)
            mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DATA_RECEIVED, (int) mTrackId, 0, chunk));
    }

}

From source file:inti.util.Base64OutputByteBufferTest.java

public void writeSmallDataSmallBuffer(String str) throws Exception {
    byte[] data = new String(str).getBytes(UTF_8);
    int bufferSize = 32;
    ByteBuffer buffer;

    base64OutputByteBuffer = new Base64OutputByteBuffer(Base64OutputByteBuffer.DEFAULT_ALPHABET,
            Base64OutputByteBuffer.DEFAULT_PADDING, bufferSize);

    base64OutputByteBuffer.write(data);//from  ww w.ja  v a  2s. co  m
    base64OutputByteBuffer.flush();
    buffer = base64OutputByteBuffer.getByteBuffer();

    assertEquals(new Base64().encodeAsString(data), new String(buffer.array()).trim());

    base64OutputByteBuffer = new Base64OutputByteBuffer(Base64OutputByteBuffer.DEFAULT_ALPHABET,
            Base64OutputByteBuffer.DEFAULT_PADDING, bufferSize);

    for (int i = 0; i < data.length; i++) {
        base64OutputByteBuffer.write(data[i]);
    }
    base64OutputByteBuffer.flush();
    buffer = base64OutputByteBuffer.getByteBuffer();

    assertEquals(new Base64().encodeAsString(data), new String(buffer.array()).trim());

    if (data.length % 2 == 0) {
        base64OutputByteBuffer = new Base64OutputByteBuffer(Base64OutputByteBuffer.DEFAULT_ALPHABET,
                Base64OutputByteBuffer.DEFAULT_PADDING, bufferSize);

        for (int i = 0; i < data.length; i += 2) {
            base64OutputByteBuffer.write(new byte[] { data[i], data[i + 1] });
        }
        base64OutputByteBuffer.flush();
        buffer = base64OutputByteBuffer.getByteBuffer();

        assertEquals(new Base64().encodeAsString(data), new String(buffer.array()).trim());
    }
}

From source file:edu.umn.cs.spatialHadoop.core.ZCurvePartitioner.java

@Override
public void write(DataOutput out) throws IOException {
    mbr.write(out);//from  ww  w .  j  av  a 2 s  .  c om
    out.writeInt(zSplits.length);
    ByteBuffer bbuffer = ByteBuffer.allocate(zSplits.length * 8);
    for (long zSplit : zSplits)
        bbuffer.putLong(zSplit);
    if (bbuffer.hasRemaining())
        throw new RuntimeException("Did not calculate buffer size correctly");
    out.write(bbuffer.array(), bbuffer.arrayOffset(), bbuffer.position());
}

From source file:com.all.networking.AbstractNetworkingService.java

private boolean write(IoSession session, NetworkingMessage networkingMessage) throws InterruptedException {
    String json = JsonConverter.toJson(networkingMessage);
    String encodedMessage = null;
    try {//from w  w  w . j  a v a 2  s.  co  m
        ByteBuffer byteBuffer = UTF_ENCODER.encode(CharBuffer.wrap(json));
        encodedMessage = new String(Base64.encode(byteBuffer.array()));
    } catch (CharacterCodingException e) {
        LOG.warn("Could not encode message with UTF-8.");
        encodedMessage = new String(Base64.encode(json.getBytes()));
    }
    WriteFuture future = session.write(encodedMessage);
    future.await();
    if (!future.isWritten()) {
        LOG.error("Could not send message through the network.", future.getException());
    }
    return future.isWritten();
}

From source file:com.cellngine.crypto.RSACipher.java

private byte[] encode(final BigInteger modulus, final BigInteger exponent) {
    final byte[] modulusEnc = modulus.toByteArray();
    final byte[] exponentEnc = exponent.toByteArray();
    final ByteBuffer buffer = ByteBuffer.allocate(2 * 4 + modulusEnc.length + exponentEnc.length);
    buffer.putInt(modulusEnc.length);//ww  w  .ja  v  a2 s . c  om
    buffer.put(modulusEnc);
    buffer.putInt(exponentEnc.length);
    buffer.put(exponentEnc);
    return buffer.array();
}