Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array, int start, int len) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryArrayZigZarByteWriter.java

@Override
public byte[] getPage() throws IOException {
    if (dataOffset == 3 * Bytes.SIZEOF_INT) // no data appended 
        return null;

    /** We do not compressed the page of data */
    if (compressAlgo == null) {
        bb = ByteBuffer.wrap(page, 0, page.length);
        bb.putInt(dataOffset);//w w w  .j ava 2s  . c om
        bb.putInt(numPairs);
        bb.putInt(startPos);
        return page;
    } else { // compress a page using the specified <i>Algorithm</i>
        outputBuffer.reset();
        outputBuffer.writeInt(dataOffset);
        outputBuffer.writeInt(numPairs);
        outputBuffer.writeInt(startPos);
        outputBuffer.writeInt(page.length - 12);
        compressor = compressAlgo.getCompressor();
        // create the compression stream
        OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0);
        os.write(page, 3 * Bytes.SIZEOF_INT, dataOffset - 3 * Bytes.SIZEOF_INT);
        os.flush();
        this.compressAlgo.returnCompressor(compressor);
        this.compressor = null;
        return outputBuffer.getData();
    }
}

From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryBitPackingZigZarIntWriter.java

@Override
public byte[] getPage() throws IOException {
    if (dataOffset == 3 * Bytes.SIZEOF_INT)
        return null;
    /** We do not compressed the page of data */
    if (compressAlgo == null) {
        //  bb = ByteBuffer.wrap(page, 0, dataOffset);
        bb = ByteBuffer.wrap(page, 0, page.length);
        bb.putInt(dataOffset);/*from  w ww  .j a v  a2  s. c o  m*/
        bb.putInt(numPairs);
        bb.putInt(startPos);
        return page;
    } else { // compress a page using the specified <i>Algorithm</i>
        outputBuffer.reset();
        outputBuffer.writeInt(dataOffset);
        outputBuffer.writeInt(numPairs);
        outputBuffer.writeInt(startPos);
        outputBuffer.writeInt(page.length - 12);
        compressor = compressAlgo.getCompressor();
        // create the compression stream
        OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0);
        os.write(page, 3 * Bytes.SIZEOF_INT, page.length - 12);
        os.flush();
        this.compressAlgo.returnCompressor(compressor);
        this.compressor = null;
        return outputBuffer.getData();
    }
}

From source file:eu.stratosphere.pact.common.io.TextInputFormat.java

public boolean readRecord(PactRecord target, byte[] bytes, int offset, int numBytes) {
    PactString str = this.theString;

    if (this.ascii) {
        str.setValueAscii(bytes, offset, numBytes);
    } else {// w  w  w . ja v  a 2  s  . c  om
        ByteBuffer byteWrapper = this.byteWrapper;
        if (bytes != byteWrapper.array()) {
            byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length);
            this.byteWrapper = byteWrapper;
        }
        byteWrapper.position(offset);
        byteWrapper.limit(offset + numBytes);

        try {
            CharBuffer result = this.decoder.decode(byteWrapper);
            str.setValue(result);
        } catch (CharacterCodingException e) {
            byte[] copy = new byte[numBytes];
            System.arraycopy(bytes, offset, copy, 0, numBytes);
            LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e);
            return false;
        }
    }

    target.clear();
    target.setField(this.pos, str);
    return true;
}

From source file:cn.ac.ncic.mastiff.io.coding.DictionaryBitPackingRLEByteWriter.java

@Override
public byte[] getPage() throws IOException {
    if (dataOffset == 3 * Bytes.SIZEOF_INT) // no data appended 
        return null;
    /** We do not compressed the page of data */
    if (compressAlgo == null) {
        //  bb = ByteBuffer.wrap(page, 0, dataOffset);
        bb = ByteBuffer.wrap(page, 0, page.length);
        bb.putInt(dataOffset);//from  w  ww  . ja  va  2s  . c o  m
        bb.putInt(numPairs);
        bb.putInt(startPos);
        return page;
    } else { // compress a page using the specified <i>Algorithm</i>
        outputBuffer.reset();
        outputBuffer.writeInt(dataOffset);
        outputBuffer.writeInt(numPairs);
        outputBuffer.writeInt(startPos);
        outputBuffer.writeInt(page.length - 12);
        compressor = compressAlgo.getCompressor();
        // create the compression stream
        OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0);
        os.write(page, 3 * Bytes.SIZEOF_INT, dataOffset - 3 * Bytes.SIZEOF_INT);
        os.flush();
        this.compressAlgo.returnCompressor(compressor);
        this.compressor = null;
        return outputBuffer.getData();
    }
}

From source file:eu.stratosphere.pact.array.io.StringInputFormat.java

public boolean readRecord(Value[] target, byte[] bytes, int offset, int numBytes) {
    PactString str = this.theString;

    if (this.ascii) {
        str.setValueAscii(bytes, offset, numBytes);
    } else {// www  .jav  a  2s  . c  om
        ByteBuffer byteWrapper = this.byteWrapper;
        if (bytes != byteWrapper.array()) {
            byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length);
            this.byteWrapper = byteWrapper;
        }
        byteWrapper.clear();
        byteWrapper.position(offset);
        byteWrapper.limit(offset + numBytes);

        try {
            CharBuffer result = this.decoder.decode(byteWrapper);
            str.setValue(result);
        } catch (CharacterCodingException e) {
            byte[] copy = new byte[numBytes];
            System.arraycopy(bytes, offset, copy, 0, numBytes);
            LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e);
            return false;
        }
    }

    target[0] = str;
    return true;
}

From source file:cn.ac.ncic.mastiff.io.coding.FixedLenEncoder.java

@Override
public byte[] getPage() throws IOException {
    if (dataOffset == 3 * Bytes.SIZEOF_INT) // no data appended 
        return null;

    /** We do not compressed the page of data */
    if (compressAlgo == null) {
        //  bb = ByteBuffer.wrap(page, 0, dataOffset);
        bb = ByteBuffer.wrap(page, 0, page.length);
        bb.putInt(dataOffset);//from   w  ww.  j  a v  a  2 s  .  c  o  m
        bb.putInt(numPairs);
        bb.putInt(startPos);
        LOG.info("134  page .length " + page.length);
        return page;
    } else { // compress a page using the specified <i>Algorithm</i>
        outputBuffer.reset();
        outputBuffer.writeInt(dataOffset);
        outputBuffer.writeInt(numPairs);
        outputBuffer.writeInt(startPos);
        compressor = compressAlgo.getCompressor();
        // create the compression stream
        OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0);
        os.write(page, 3 * Bytes.SIZEOF_INT, dataOffset - 3 * Bytes.SIZEOF_INT);
        os.flush();
        this.compressAlgo.returnCompressor(compressor);
        this.compressor = null;
        return outputBuffer.getData();
    }
}

From source file:com.mellanox.hadoop.mapred.UdaShuffleHandler.java

public static ByteBuffer serializeServiceData(Token<JobTokenIdentifier> jobToken) throws IOException {
    //TODO these bytes should be versioned
    DataOutputBuffer jobToken_dob = new DataOutputBuffer();
    jobToken.write(jobToken_dob);//from   ww  w .  j a v a  2s . c o  m
    return ByteBuffer.wrap(jobToken_dob.getData(), 0, jobToken_dob.getLength());
}

From source file:hudson.plugins.sonar.utils.MaskPasswordsOutputStream.java

@Override
protected void eol(byte[] bytes, int len) throws IOException {
    String line = charset.decode(ByteBuffer.wrap(bytes, 0, len)).toString();
    if (passwordsAsPattern != null && !line.contains(URL_IN_LOGS)) {
        line = passwordsAsPattern.matcher(line).replaceAll(REPLACEMENT);
    }//from www .  j  a v a  2 s .c o  m
    logger.write(line.getBytes(charset));
}

From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryBitPackingZigZarLongWriter.java

@Override
public byte[] getPage() throws IOException {
    if (dataOffset == 3 * Bytes.SIZEOF_INT) // no data appended 
        return null;
    /** We do not compressed the page of data */
    if (compressAlgo == null) {
        if (valueLen == 4) {
            LOG.info("134 dataOffse  " + dataOffset);
            LOG.info("134 pageLnegth " + page.length);

        }//from   w  ww .ja va  2s .  c  o  m
        //  bb = ByteBuffer.wrap(page, 0, dataOffset);
        bb = ByteBuffer.wrap(page, 0, page.length);
        bb.putInt(dataOffset);
        bb.putInt(numPairs);
        bb.putInt(startPos);
        LOG.info("134  page .length " + page.length);
        System.out.println(
                "154 dataoffset  " + dataOffset + " numPairs  " + numPairs + "  startPos  " + startPos);
        return page;
    } else { // compress a page using the specified <i>Algorithm</i>
        outputBuffer.reset();
        outputBuffer.writeInt(dataOffset);
        outputBuffer.writeInt(numPairs);
        outputBuffer.writeInt(startPos);
        outputBuffer.writeInt(page.length - 12);
        compressor = compressAlgo.getCompressor();
        // create the compression stream
        OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0);
        os.write(page, 3 * Bytes.SIZEOF_INT, dataOffset - 3 * Bytes.SIZEOF_INT);
        os.flush();
        this.compressAlgo.returnCompressor(compressor);
        this.compressor = null;
        return outputBuffer.getData();
    }
}

From source file:edu.umass.cs.reconfiguration.reconfigurationutils.ReconfigurationPacketDemultiplexer.java

@Override
public Integer getPacketType(JSONObject json) {
    if (json instanceof JSONMessenger.JSONObjectWrapper) {
        byte[] bytes = (byte[]) ((JSONMessenger.JSONObjectWrapper) json).getObj();
        if (!JSONPacket.couldBeJSON(bytes, NIOHeader.BYTES)) {
            // first 4 bytes (after 12 bytes of address) must be the type
            return ByteBuffer.wrap(bytes, NIOHeader.BYTES, Integer.BYTES).getInt();
        } else {// ww w.  j  av  a 2  s  .co  m
            // return any valid type (assuming no more chained demultiplexers)
            return PacketType.ECHO_REQUEST.getInt();
        }
    } else
        try {
            //            assert(ReconfigurationPacket.isReconfigurationPacket(json)) : json;
            return JSONPacket.getPacketType(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    return null;
}