Java ByteBuffer Write writeXid(ByteBuffer logBuf, Xid xid)

Here you can find the source of writeXid(ByteBuffer logBuf, Xid xid)

Description

write Xid

License

Open Source License

Declaration

public static void writeXid(ByteBuffer logBuf, Xid xid) 

Method Source Code


//package com.java2s;
/*-//ww  w . j av  a 2 s.c o  m
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002-2010 Oracle.  All rights reserved.
 *
 */

import java.nio.ByteBuffer;

import javax.transaction.xa.Xid;

public class Main {
    public static void writeXid(ByteBuffer logBuf, Xid xid) {
        byte[] gid = xid.getGlobalTransactionId();
        byte[] bqual = xid.getBranchQualifier();

        writeInt(logBuf, xid.getFormatId());

        if (gid == null) {
            logBuf.put((byte) -1);
        } else {
            logBuf.put((byte) (gid.length));
            logBuf.put(gid);
        }

        if (bqual == null) {
            logBuf.put((byte) -1);
        } else {
            logBuf.put((byte) (bqual.length));
            logBuf.put(bqual);
        }
    }

    /**
     * Write an int into the log.
     */
    public static void writeInt(ByteBuffer logBuf, int i) {
        byte b = (byte) ((i >> 0) & 0xff);
        logBuf.put(b);
        b = (byte) ((i >> 8) & 0xff);
        logBuf.put(b);
        b = (byte) ((i >> 16) & 0xff);
        logBuf.put(b);
        b = (byte) ((i >> 24) & 0xff);
        logBuf.put(b);
    }
}

Related

  1. writeVarLong(ByteBuffer buff, long x)
  2. writeVarLong(long n, ByteBuffer buff)
  3. writeVarLong(long value, ByteBuffer buf)
  4. writeVInt(ByteBuffer bb, int i)
  5. writeWithLength(ByteBuffer buffer, byte[] src)