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:com.andrewkroh.cicso.rtp.AudioFileStreamer.java

/**
 * Sends a single packet of audio data. It reads from the
 * {@link #outputDataBuffer} and will rewind that buffer when it reaches the
 * end so that it continuously streams the source file in a loop.
 *//*  w w  w. j a v a  2 s .  co m*/
private void sendAudioData() {
    ByteBuffer packetDataBuffer = ByteBuffer.allocate(payloadSizeBytes);

    while (packetDataBuffer.hasRemaining()) {
        if (!outputDataBuffer.hasRemaining()) {
            outputDataBuffer.rewind();
        }

        packetDataBuffer.put(outputDataBuffer.get());
    }

    timestamp += numSamplesPerPacket;

    RtpPacket packet = new RtpPacket();
    packet.setPayloadType(outputEncodingType.getPayloadType());
    packet.setSSRC(ssrc);
    packet.setSequenceNumber(++sequenceNumber);
    packet.setTimestamp(timestamp);
    packet.setRtpPayloadData(packetDataBuffer.array());

    rtpSession.sendData(packet);
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * check for iOS activation//from  ww  w. ja  v a 2s .c  o  m
 * 
 * @throws MessageNotFoundException
 */
public byte[] checkStatusAppleUA(String recipientToken) throws ExchangeException, MessageNotFoundException {

    int capacity = mVersionLen //
            + 4 + recipientToken.length() //
            + 4;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(SafeSlingerConfig.NOTIFY_APPLEUA);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/checkStatus" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:net.sf.jml.message.p2p.MsnP2PMessage.java

/**
 * @see MsnMimeMessage#toOutgoingMsg(MsnProtocol)
 *//*w  w w.  ja  v a2s.  co  m*/
@Override
public OutgoingMSG[] toOutgoingMsg(MsnProtocol protocol) {
    OutgoingMSG message = new OutgoingMSG(protocol);
    message.setMsgType(OutgoingMSG.TYPE_MSNC1);

    byte[] mimeMessageHeader = Charset.encodeAsByteArray(toString());

    byte[] body = bodyToMessage();
    if (body == null) {
        body = new byte[0];
    }

    ByteBuffer msg = ByteBuffer
            .allocate(mimeMessageHeader.length + BINARY_HEADER_LEN + body.length + BINARY_FOOTER_LEN);

    msg.put(mimeMessageHeader);
    msg.put(binaryHeader);
    msg.put(body);
    msg.put(binaryFooter);
    message.setMsg(msg.array());
    return new OutgoingMSG[] { message };
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a file, its meta-data, and retrieval id on the server
 * /*from  w  ww .j  ava2s .co  m*/
 * @throws MessageNotFoundException
 */
@Deprecated
public byte[] postFileAndroidC2DM(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken)
        throws ExchangeException, MessageNotFoundException {
    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postFile1" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:gridool.memcached.gateway.MemcachedProxyHandler.java

@Override
public short handleSet(byte[] key, byte[] value, int flags, int expiry) {
    final ByteBuffer reqPacket = ByteBuffer
            .allocate(HEADER_LENGTH + SET_EXTRA_LENGTH + key.length + value.length);
    // request header
    Header header = new Header(MAGIC_BYTE_REQUEST, OPCODE_SET);
    header.setBodyLength(SET_EXTRA_LENGTH, key.length, value.length);
    header.encode(reqPacket);/*w w  w . j  a v  a 2 s.com*/
    // request body (flags, expiration, key, value)
    reqPacket.putInt(flags);
    reqPacket.putInt(expiry);
    reqPacket.put(key);
    reqPacket.put(value);
    reqPacket.flip();

    final ByteBuffer resPacket = ByteBuffer.allocate(HEADER_LENGTH);
    final SocketAddress sockAddr = getSocket(key);
    final ByteChannel channel = sockPool.borrowObject(sockAddr);
    try {
        NIOUtils.writeFully(channel, reqPacket);
        NIOUtils.readFully(channel, resPacket);
    } catch (IOException e) {
        LOG.error(e);
        return ResponseStatus.UNKNOWN.status;
    } finally {
        sockPool.returnObject(sockAddr, channel);
    }
    resPacket.flip();
    short status = resPacket.getShort(6);
    return status;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a file, its meta-data, and retrieval id on the server
 * /*from   w ww  . j av  a 2  s.  co  m*/
 * @throws MessageNotFoundException
 */
@Deprecated
public byte[] postFileAppleUA(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken)
        throws ExchangeException, MessageNotFoundException {

    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postFile2" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:edu.cmu.cylab.starslinger.transaction.WebEngine.java

/**
 * put a message, file, its meta-data, and retrieval id on the server
 * //w  w w.j av  a 2  s. c  o  m
 * @throws MessageNotFoundException
 */
public byte[] postMessage(byte[] msgHashBytes, byte[] msgData, byte[] fileData, String recipientToken,
        int notifyType) throws ExchangeException, MessageNotFoundException {

    handleSizeRestictions(fileData);

    int capacity = mVersionLen //
            + 4 + msgHashBytes.length //
            + 4 + recipientToken.length() //
            + 4 + msgData.length //
            + 4 + fileData.length //
            + 4;
    ByteBuffer msg = ByteBuffer.allocate(capacity);
    msg.putInt(mVersion);
    msg.putInt(msgHashBytes.length);
    msg.put(msgHashBytes);
    msg.putInt(recipientToken.length());
    msg.put(recipientToken.getBytes());
    msg.putInt(msgData.length);
    msg.put(msgData);
    msg.putInt(fileData.length);
    msg.put(fileData);
    msg.putInt(notifyType);

    mNotRegistered = false;

    byte[] resp = doPost(mUrlPrefix + mHost + "/postMessage" + mUrlSuffix, msg.array());

    mNotRegistered = isNotRegisteredErrorCodes(resp);

    resp = handleResponseExceptions(resp, 0);

    return resp;
}

From source file:edu.tsinghua.lumaqq.qq.packets.Packet.java

/**
 * ?//w w  w .  j  ava 2s. co m
 * 
 * @return
 *       
 */
public byte[] dump() {
    if (bodyDecrypted == null)
        return new byte[0];
    else {
        byte[] debugContent = new byte[getLength(bodyDecrypted.length)];
        ByteBuffer debugBuf = ByteBuffer.wrap(debugContent);
        putHead(debugBuf);
        debugBuf.put(bodyDecrypted);
        putTail(debugBuf);
        debugBuf = null;
        return debugContent;
    }
}

From source file:com.codestation.henkakuserver.HenkakuServer.java

/**
 * Convert the exploit to a shellcode in binary format
 *
 * @param exploit payload compiled code//from w w w . ja  va  2 s . c om
 * @return the shellcode
 * @throws Exception
 */
private byte[] preprocessToBin(byte[] exploit) throws Exception {
    Pair<ArrayList<Integer>, List<Byte>> data = preprocessRop(exploit);

    int size = 4 + data.first.size() * 4 + data.second.size();
    byte[] out = new byte[size + ((-size) & 3)];
    ByteBuffer buf = ByteBuffer.wrap(out).order(ByteOrder.LITTLE_ENDIAN);

    buf.putInt(data.second.size());

    for (Integer val : data.first) {
        buf.putInt(val);
    }

    for (Byte val : data.second) {
        buf.put(val);
    }

    return out;
}

From source file:com.clustercontrol.agent.job.PublicKeyThread.java

/**
 * ?Authorized_key????<BR>//from   ww w  .  j  a v  a  2  s . c  o  m
 * 
 * @param publicKey
 * @return
 */
private synchronized boolean addKey(String publicKey) {
    m_log.debug("add key start");

    if (SKIP_KEYFILE_UPDATE) {
        m_log.info("skipped appending publicKey");
        return true;
    }

    //???
    String fileName = AgentProperties.getProperty(execUser.toLowerCase() + AUTHORIZED_KEY_PATH);

    m_log.debug("faileName" + fileName);
    if (fileName == null || fileName.length() == 0)
        return false;

    //File?
    File fi = new File(fileName);

    RandomAccessFile randomAccessFile = null;
    FileChannel channel = null;
    FileLock lock = null;
    boolean add = false;
    try {
        //RandomAccessFile?
        randomAccessFile = new RandomAccessFile(fi, "rw");
        //FileChannel?
        channel = randomAccessFile.getChannel();

        // 
        for (int i = 0; i < (FILELOCK_TIMEOUT / FILELOCK_WAIT); i++) {
            if (null != (lock = channel.tryLock())) {
                break;
            }
            m_log.info("waiting for locked file... [" + (i + 1) + "/" + (FILELOCK_TIMEOUT / FILELOCK_WAIT)
                    + " : " + fileName + "]");
            Thread.sleep(FILELOCK_WAIT);
        }
        if (null == lock) {
            m_log.warn("file locking timeout.");
            return false;
        }

        // (?)
        synchronized (authKeyLock) {
            //??
            channel.position(channel.size());

            //?
            String writeData = "\n" + publicKey;
            // 
            m_log.debug("add key : " + writeData);

            //?????
            ByteBuffer buffer = ByteBuffer.allocate(512);

            //???
            buffer.clear();
            buffer.put(writeData.getBytes());
            buffer.flip();
            channel.write(buffer);
        }

        add = true;
    } catch (Exception e) {
        m_log.error(e);
    } finally {
        try {
            if (channel != null) {
                channel.close();
            }
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
            if (lock != null) {
                //
                lock.release();
            }
        } catch (Exception e) {
        }
    }

    return add;
}