Example usage for java.nio ByteBuffer put

List of usage examples for java.nio ByteBuffer put

Introduction

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

Prototype

public ByteBuffer put(ByteBuffer src) 

Source Link

Document

Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.

Usage

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer./*w w w . j av  a  2 s .  c  o  m*/
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:Main.java

/**
 * Discards data from the buffer up to the first SPS, where {@code data.position()} is interpreted
 * as the length of the buffer.//from   w ww .j ava 2 s.  co m
 * <p>
 * When the method returns, {@code data.position()} will contain the new length of the buffer. If
 * the buffer is not empty it is guaranteed to start with an SPS.
 *
 * @param data Buffer containing start code delimited NAL units.
 */
public static void discardToSps(ByteBuffer data) {
    int length = data.position();
    int consecutiveZeros = 0;
    int offset = 0;
    while (offset + 1 < length) {
        int value = data.get(offset) & 0xFF;
        if (consecutiveZeros == 3) {
            if (value == 1 && (data.get(offset + 1) & 0x1F) == H264_NAL_UNIT_TYPE_SPS) {
                // Copy from this NAL unit onwards to the start of the buffer.
                ByteBuffer offsetData = data.duplicate();
                offsetData.position(offset - 3);
                offsetData.limit(length);
                data.position(0);
                data.put(offsetData);
                return;
            }
        } else if (value == 0) {
            consecutiveZeros++;
        }
        if (value != 0) {
            consecutiveZeros = 0;
        }
        offset++;
    }
    // Empty the buffer if the SPS NAL unit was not found.
    data.clear();
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Get the likely uncompressed size of a Gziped file
 * @param filename/*from ww  w.  j av  a 2  s. c o m*/
 * @return
 * @throws IOException 
 * @see http://stackoverflow.com/questions/27825927/get-gzipped-file-attributes-like-gzip-l-basically-compression-ratio
 * @throws FileNotFoundException 
 */
public static long getUncompressedFileSize(String filename) throws FileNotFoundException, IOException {

    File f = new File(filename);
    try (RandomAccessFile ra = new RandomAccessFile(f, "r"); FileChannel channel = ra.getChannel()) {

        MappedByteBuffer fileBuffer = channel.map(MapMode.READ_ONLY, f.length() - 4, 4);
        fileBuffer.load();

        ByteBuffer buf = ByteBuffer.allocate(4);
        buf.order(ByteOrder.LITTLE_ENDIAN);

        buf.put(fileBuffer);
        buf.flip();
        //will print the uncompressed size
        //getInt() reads the 4 bytes as a int
        // if the file is between 2GB and 4GB
        // then this will return a negative value
        //and you'll have to do your own converting to an unsigned int
        int size = buf.getInt();

        if (size < 0) {
            return FileUtils.ONE_GB + size;
        } else {
            return size;
        }
    }
}

From source file:com.alibaba.napoli.metamorphosis.client.producer.SimpleMessageProducer.java

/**
 * ??payload</br></br> 01attribute + payload
 * //  ww  w  .  jav  a 2 s  .  c om
 * @param message
 * @return
 */
public static byte[] encodeData(final Message message) {
    final byte[] payload = message.getData();
    final String attribute = message.getAttribute();
    byte[] attrData = null;
    if (attribute != null) {
        attrData = ByteUtils.getBytes(attribute);
    } else {
        return payload;
    }
    // attributenull
    final int attrLen = attrData == null ? 0 : attrData.length;
    final ByteBuffer buffer = ByteBuffer.allocate(4 + attrLen + payload.length);
    if (attribute != null) {
        buffer.putInt(attrLen);
        if (attrData != null) {
            buffer.put(attrData);
        }
    }

    buffer.put(payload);
    return buffer.array();
}

From source file:edu.umass.cs.utils.Util.java

/**
 * Transfer from src to dst without throwing exception if src.remaining() >
 * dst.remaining() but copying dst.remaining() bytes from src instead.
 *//*from  ww w .  j av  a2 s.c o  m*/
public static ByteBuffer put(ByteBuffer dst, ByteBuffer src) {
    if (src.remaining() < dst.remaining())
        return dst.put(src);
    int oldLimit = src.limit();
    src.limit(src.position() + dst.remaining());
    dst.put(src);
    src.limit(oldLimit);
    return dst;
    // byte[] buf = new byte[dst.remaining()];
    // src.get(buf);
    // return dst.put(buf);
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

/**
 * @param bb//from   w  w w.  j ava 2s. c  om
 * @param bytes
 * @param len
 * @return
 */
public static ByteBuffer appendToByteBuffer(ByteBuffer bb, byte[] bytes, int len) {
    if (len > bytes.length) {
        int pos = bb.position();
        bb.put(bytes);
        bb.position(pos + len);
    } else {
        bb.put(bytes, 0, len);
    }
    return bb;
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * from to /*ww  w .j a v a2 s  . c  o m*/
 * 
 * @param fromBuffer
 *            Buffer ? flush
 * @param toBuffer
 *            Buffer ? fill
 * @return number of bytes moved
 */
public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) {
    int put;
    int remaining = fromBuffer.remaining();
    if (remaining > 0) { // 
        if (remaining <= toBuffer.remaining()) {
            toBuffer.put(fromBuffer);
            put = remaining;
            // from 
            fromBuffer.position(fromBuffer.limit());
        }
        // heap buffer
        else if (fromBuffer.hasArray()) {
            put = toBuffer.remaining();
            // ??
            toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put);
            fromBuffer.position(fromBuffer.position() + put);
        }
        // direct buffer
        else {
            // ??
            put = toBuffer.remaining();
            ByteBuffer slice = fromBuffer.slice();
            slice.limit(put);
            toBuffer.put(slice);
            fromBuffer.position(fromBuffer.position() + put);
        }
    } else {
        put = 0;
    }
    return put;
}

From source file:net.filterlogic.util.imaging.ToTIFF.java

private static String bigEndian2LittleEndian(String fileName) throws FileNotFoundException, IOException {

    //import java.nio.ByteBuffer;
    //import java.nio.ByteOrder;
    //import java.nio.channels.FileChannel;
    //import java.io.FileOutputStream;

    byte[] imgData = loadFileToByteArray(fileName);
    String newFileName = fileName + ".be2le.tif";

    ByteBuffer buffer = ByteBuffer.allocate(imgData.length);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(imgData);
    FileChannel out = new FileOutputStream(newFileName).getChannel();
    out.write(buffer);/*  w  w  w.j a  v a2  s . c om*/
    out.close();

    return newFileName;
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

private static byte[] writePackageStreamFooter(OleBlob.Builder oleBuilder) {

    // note, these are _not_ zero terminated
    byte[] fileNameBytes = oleBuilder.getFileName().getBytes(OLE_UTF_CHARSET);
    byte[] filePathBytes = oleBuilder.getFilePath().getBytes(OLE_UTF_CHARSET);

    int footerLen = 12 + (filePathBytes.length * 2) + fileNameBytes.length;

    byte[] footerBytes = new byte[footerLen];
    ByteBuffer bb = PageChannel.wrap(footerBytes);

    bb.putInt(filePathBytes.length / 2);
    bb.put(filePathBytes);
    bb.putInt(fileNameBytes.length / 2);
    bb.put(fileNameBytes);/* ww  w .j av a 2 s. c om*/
    bb.putInt(filePathBytes.length / 2);
    bb.put(filePathBytes);

    return footerBytes;
}

From source file:net.servicestack.client.Utils.java

public static byte[] toGuidBytes(UUID theUuid) {
    ByteBuffer first8 = ByteBuffer.allocate(8);
    first8.putLong(theUuid.getMostSignificantBits());
    first8.rewind();//from w w  w .j a  v a 2 s . c om

    byte[] first4 = new byte[4];
    first8.get(first4);
    reverse(first4);

    byte[] second2 = new byte[2];
    first8.get(second2);
    reverse(second2);

    byte[] third2 = new byte[2];
    first8.get(third2);
    reverse(third2);

    ByteBuffer converted16 = ByteBuffer.allocate(16).put(first4).put(second2).put(third2);

    ByteBuffer last8 = ByteBuffer.allocate(8);
    last8.putLong(theUuid.getLeastSignificantBits());
    last8.rewind();

    converted16.put(last8);

    return converted16.array();
}