Example usage for java.nio ByteBuffer putInt

List of usage examples for java.nio ByteBuffer putInt

Introduction

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

Prototype

public abstract ByteBuffer putInt(int value);

Source Link

Document

Writes the given int to the current position and increases the position by 4.

Usage

From source file:org.apache.hadoop.hdfs.server.datanode.BlockSender.java

/**
 * Sends upto maxChunks chunks of data.// www .j a v  a2s  . co  m
 * 
 * When blockInPosition is >= 0, assumes 'out' is a 
 * {@link SocketOutputStream} and tries 
 * {@link SocketOutputStream#transferToFully(FileChannel, long, int)} to
 * send data (and updates blockInPosition).
 */
private int sendChunks(ByteBuffer pkt, int maxChunks, OutputStream out) throws IOException {
    // Sends multiple chunks in one packet with a single write().

    int len = Math.min((int) (endOffset - offset), bytesPerChecksum * maxChunks);

    // truncate len so that any partial chunks will be sent as a final packet.
    // this is not necessary for correctness, but partial chunks are 
    // ones that may be recomputed and sent via buffer copy, so try to minimize
    // those bytes
    if (len > bytesPerChecksum && len % bytesPerChecksum != 0) {
        len -= len % bytesPerChecksum;
    }

    if (len == 0) {
        return 0;
    }

    int numChunks = (len + bytesPerChecksum - 1) / bytesPerChecksum;
    int packetLen = len + numChunks * checksumSize + 4;
    pkt.clear();

    // write packet header
    pkt.putInt(packetLen);
    pkt.putLong(offset);
    pkt.putLong(seqno);
    pkt.put((byte) ((offset + len >= endOffset) ? 1 : 0));
    //why no ByteBuf.putBoolean()?
    pkt.putInt(len);

    int checksumOff = pkt.position();
    int checksumLen = numChunks * checksumSize;
    byte[] buf = pkt.array();

    if (checksumSize > 0 && checksumIn != null) {
        try {
            checksumIn.readFully(buf, checksumOff, checksumLen);
        } catch (IOException e) {
            LOG.warn(" Could not read or failed to veirfy checksum for data" + " at offset " + offset
                    + " for block " + block + " got : " + StringUtils.stringifyException(e));
            IOUtils.closeStream(checksumIn);
            checksumIn = null;
            if (corruptChecksumOk) {
                if (checksumOff < checksumLen) {
                    // Just fill the array with zeros.
                    Arrays.fill(buf, checksumOff, checksumLen, (byte) 0);
                }
            } else {
                throw e;
            }
        }
    }

    int dataOff = checksumOff + checksumLen;

    if (blockInPosition < 0) {
        //normal transfer
        IOUtils.readFully(blockIn, buf, dataOff, len);

        if (verifyChecksum) {
            int dOff = dataOff;
            int cOff = checksumOff;
            int dLeft = len;

            for (int i = 0; i < numChunks; i++) {
                checksum.reset();
                int dLen = Math.min(dLeft, bytesPerChecksum);
                checksum.update(buf, dOff, dLen);
                if (!checksum.compare(buf, cOff)) {
                    throw new ChecksumException("Checksum failed at " + (offset + len - dLeft), len);
                }
                dLeft -= dLen;
                dOff += dLen;
                cOff += checksumSize;
            }
        }

        // only recompute checksum if we can't trust the meta data due to 
        // concurrent writes
        if (memoizedBlock.hasBlockChanged(len)) {
            ChecksumUtil.updateChunkChecksum(buf, checksumOff, dataOff, len, checksum);
        }

        try {
            out.write(buf, 0, dataOff + len);
        } catch (IOException e) {
            throw ioeToSocketException(e);
        }
    } else {
        try {
            //use transferTo(). Checks on out and blockIn are already done. 
            SocketOutputStream sockOut = (SocketOutputStream) out;
            FileChannel fileChannel = ((FileInputStream) blockIn).getChannel();

            if (memoizedBlock.hasBlockChanged(len)) {
                fileChannel.position(blockInPosition);
                IOUtils.readFileChannelFully(fileChannel, buf, dataOff, len);

                ChecksumUtil.updateChunkChecksum(buf, checksumOff, dataOff, len, checksum);
                sockOut.write(buf, 0, dataOff + len);
            } else {
                //first write the packet
                sockOut.write(buf, 0, dataOff);
                // no need to flush. since we know out is not a buffered stream.
                sockOut.transferToFully(fileChannel, blockInPosition, len);
            }

            blockInPosition += len;

        } catch (IOException e) {
            /* exception while writing to the client (well, with transferTo(),
             * it could also be while reading from the local file).
             */
            throw ioeToSocketException(e);
        }
    }

    if (throttler != null) { // rebalancing so throttle
        throttler.throttle(packetLen);
    }

    return len;
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionTOSMatchMsg(byte tos) {
    ByteBuffer ipv6ext_tos_msg = ByteBuffer.allocate(5);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_IP_TOS.getValue(), 0, 1);
    ipv6ext_tos_msg.putInt(nxm_header);
    ipv6ext_tos_msg.put(tos);//from   w w w  .  j a  va2 s  .  co m
    return (ipv6ext_tos_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionPortMatchMsg(short port) {
    ByteBuffer ipv6ext_port_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_IN_PORT.getValue(), 0, 2);
    ipv6ext_port_msg.putInt(nxm_header);
    ipv6ext_port_msg.putShort(port);//from   www .  ja  v a 2s.  co m
    return (ipv6ext_port_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionEtherTypeMatchMsg(short EtherType) {
    ByteBuffer ipv6ext_etype_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_ETH_TYPE.getValue(), 0, 2);
    ipv6ext_etype_msg.putInt(nxm_header);
    ipv6ext_etype_msg.putShort(EtherType);
    return (ipv6ext_etype_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionSrcMacMatchMsg(byte[] srcMac) {
    ByteBuffer ipv6ext_srcmac_msg = ByteBuffer.allocate(10);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_ETH_SRC.getValue(), 0, 6);
    ipv6ext_srcmac_msg.putInt(nxm_header);
    ipv6ext_srcmac_msg.put(srcMac);/* w w w . ja v a 2  s  .co m*/
    return (ipv6ext_srcmac_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionVlanIDMatchMsg(short VLAN) {
    ByteBuffer ipv6ext_vlanid_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_VLAN_TCI.getValue(), 0, 2);
    ipv6ext_vlanid_msg.putInt(nxm_header);
    ipv6ext_vlanid_msg.putShort(VLAN);/* ww  w  . java  2  s  .  com*/
    return (ipv6ext_vlanid_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionDestMacMatchMsg(byte[] destMac) {
    ByteBuffer ipv6ext_destmac_msg = ByteBuffer.allocate(10);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_ETH_DST.getValue(), 0, 6);
    ipv6ext_destmac_msg.putInt(nxm_header);
    ipv6ext_destmac_msg.put(destMac);//  w  ww. j a va2  s .c  om
    return (ipv6ext_destmac_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionTCPSrcPortMatchMsg(short src_port) {
    ByteBuffer ipv6ext_tcp_srcport_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_TCP_SRC.getValue(), 0, 2);
    ipv6ext_tcp_srcport_msg.putInt(nxm_header);
    ipv6ext_tcp_srcport_msg.putShort(src_port);
    return (ipv6ext_tcp_srcport_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionTCPDstPortMatchMsg(short dst_port) {
    ByteBuffer ipv6ext_tcp_dstport_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_TCP_DST.getValue(), 0, 2);
    ipv6ext_tcp_dstport_msg.putInt(nxm_header);
    ipv6ext_tcp_dstport_msg.putShort(dst_port);
    return (ipv6ext_tcp_dstport_msg.array());
}

From source file:org.opendaylight.controller.protocol_plugin.openflow.vendorextension.v6extension.V6Match.java

private byte[] getIPv6ExtensionUDPSrcPortMatchMsg(short src_port) {
    ByteBuffer ipv6ext_udp_srcport_msg = ByteBuffer.allocate(6);
    int nxm_header = getIPv6ExtensionMatchHeader(Extension_Types.OF_10,
            OF_Match_Types.MATCH_OF_UDP_SRC.getValue(), 0, 2);
    ipv6ext_udp_srcport_msg.putInt(nxm_header);
    ipv6ext_udp_srcport_msg.putShort(src_port);
    return (ipv6ext_udp_srcport_msg.array());
}