Example usage for java.io DataOutput writeShort

List of usage examples for java.io DataOutput writeShort

Introduction

In this page you can find the example usage for java.io DataOutput writeShort.

Prototype

void writeShort(int v) throws IOException;

Source Link

Document

Writes two bytes to the output stream to represent the value of the argument.

Usage

From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java

/**
 * Write a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from   w ww  .j  a  va  2  s. co  m*/
 * @param out
 * @param instance
 * @param declaredClass
 * @param conf
 * @throws IOException
 */
@SuppressWarnings("unchecked")
static void writeObject(DataOutput out, Object instance, Class declaredClass, Configuration conf)
        throws IOException {

    Object instanceObj = instance;
    Class declClass = declaredClass;

    if (instanceObj == null) { // null
        instanceObj = new NullInstance(declClass, conf);
        declClass = Writable.class;
    }
    writeClassCode(out, declClass);
    if (declClass.isArray()) { // array
        // If bytearray, just dump it out -- avoid the recursion and
        // byte-at-a-time we were previously doing.
        if (declClass.equals(byte[].class)) {
            Bytes.writeByteArray(out, (byte[]) instanceObj);
        } else {
            //if it is a Generic array, write the element's type
            if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
                Class<?> componentType = declaredClass.getComponentType();
                writeClass(out, componentType);
            }

            int length = Array.getLength(instanceObj);
            out.writeInt(length);
            for (int i = 0; i < length; i++) {
                Object item = Array.get(instanceObj, i);
                writeObject(out, item, item.getClass(), conf);
            }
        }
    } else if (List.class.isAssignableFrom(declClass)) {
        List list = (List) instanceObj;
        int length = list.size();
        out.writeInt(length);
        for (int i = 0; i < length; i++) {
            Object elem = list.get(i);
            writeObject(out, elem, elem == null ? Writable.class : elem.getClass(), conf);
        }
    } else if (declClass == String.class) { // String
        Text.writeString(out, (String) instanceObj);
    } else if (declClass.isPrimitive()) { // primitive type
        if (declClass == Boolean.TYPE) { // boolean
            out.writeBoolean(((Boolean) instanceObj).booleanValue());
        } else if (declClass == Character.TYPE) { // char
            out.writeChar(((Character) instanceObj).charValue());
        } else if (declClass == Byte.TYPE) { // byte
            out.writeByte(((Byte) instanceObj).byteValue());
        } else if (declClass == Short.TYPE) { // short
            out.writeShort(((Short) instanceObj).shortValue());
        } else if (declClass == Integer.TYPE) { // int
            out.writeInt(((Integer) instanceObj).intValue());
        } else if (declClass == Long.TYPE) { // long
            out.writeLong(((Long) instanceObj).longValue());
        } else if (declClass == Float.TYPE) { // float
            out.writeFloat(((Float) instanceObj).floatValue());
        } else if (declClass == Double.TYPE) { // double
            out.writeDouble(((Double) instanceObj).doubleValue());
        } else if (declClass == Void.TYPE) { // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declClass);
        }
    } else if (declClass.isEnum()) { // enum
        Text.writeString(out, ((Enum) instanceObj).name());
    } else if (Message.class.isAssignableFrom(declaredClass)) {
        Text.writeString(out, instanceObj.getClass().getName());
        ((Message) instance).writeDelimitedTo(DataOutputOutputStream.constructOutputStream(out));
    } else if (Writable.class.isAssignableFrom(declClass)) { // Writable
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ((Writable) instanceObj).write(out);
    } else if (Serializable.class.isAssignableFrom(declClass)) {
        Class<?> c = instanceObj.getClass();
        Integer code = CLASS_TO_CODE.get(c);
        if (code == null) {
            out.writeByte(NOT_ENCODED);
            Text.writeString(out, c.getName());
        } else {
            writeClassCode(out, c);
        }
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(instanceObj);
            byte[] value = bos.toByteArray();
            out.writeInt(value.length);
            out.write(value);
        } finally {
            if (bos != null)
                bos.close();
            if (oos != null)
                oos.close();
        }
    } else if (Scan.class.isAssignableFrom(declClass)) {
        Scan scan = (Scan) instanceObj;
        byte[] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
        out.writeInt(scanBytes.length);
        out.write(scanBytes);
    } else {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

/**
 * Serialize a {@link INodeFile} node/* www. j  a  v a2s .c  o m*/
 * @param node The node to write
 * @param out The {@link DataOutputStream} where the fields are written
 * @param writeBlock Whether to write block information
 */
public static void writeINodeFile(INodeFile file, DataOutput out, boolean writeUnderConstruction)
        throws IOException {
    LOG.info("== Inside writeINodeFile() ==");
    LOG.info("Parameters count and cached logged into FSImage");
    LOG.info("ACCESSCNT " + file.getAccessCount());
    LOG.info("CACHED " + file.getIsCached());
    writeLocalName(file, out);
    out.writeLong(file.getId());
    out.writeShort(file.getFileReplication());
    out.writeLong(file.getModificationTime());
    out.writeLong(file.getAccessTime());
    out.writeLong(file.getPreferredBlockSize());

    writeBlocks(file.getBlocks(), out);
    SnapshotFSImageFormat.saveFileDiffList(file, out);

    if (writeUnderConstruction) {
        if (file instanceof INodeFileUnderConstruction) {
            out.writeBoolean(true);
            final INodeFileUnderConstruction uc = (INodeFileUnderConstruction) file;
            writeString(uc.getClientName(), out);
            writeString(uc.getClientMachine(), out);
        } else {
            out.writeBoolean(false);
        }
    }

    writePermissionStatus(file, out);
    out.writeLong(file.getAccessCount());
    out.writeInt(file.getIsCached());
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

/** Serialize an {@link INodeFileAttributes}. */
public static void writeINodeFileAttributes(INodeFileAttributes file, DataOutput out) throws IOException {
    LOG.info("== Inside writeINodeFileAttributes() ==");
    LOG.info("Parameters count and cached");
    LOG.info("ACCESSCNT " + file.getAccessCount());
    LOG.info("CACHED " + file.getIsCached());

    writeLocalName(file, out);/*from www  .  ja  va 2  s  .  c  o  m*/
    writePermissionStatus(file, out);
    out.writeLong(file.getModificationTime());
    out.writeLong(file.getAccessTime());

    out.writeShort(file.getFileReplication());
    out.writeLong(file.getPreferredBlockSize());
    out.writeLong(file.getAccessCount());
    out.writeInt(file.getIsCached());
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

/**
 * Serialize a {@link INodeDirectory}//from  w  w w.  j a  v a  2s  .  c  o m
 * @param node The node to write
 * @param out The {@link DataOutput} where the fields are written 
 */
public static void writeINodeDirectory(INodeDirectory node, DataOutput out) throws IOException {
    writeLocalName(node, out);
    out.writeLong(node.getId());
    out.writeShort(0); // replication
    out.writeLong(node.getModificationTime());
    out.writeLong(0); // access time
    out.writeLong(0); // preferred block size
    out.writeInt(-1); // # of blocks

    out.writeLong(node.getNsQuota());
    out.writeLong(node.getDsQuota());
    if (node instanceof INodeDirectorySnapshottable) {
        out.writeBoolean(true);
    } else {
        out.writeBoolean(false);
        out.writeBoolean(node instanceof INodeDirectoryWithSnapshot);
    }

    writePermissionStatus(node, out);
    /*out.writeLong(0);   access count IDecider
    out.writeInt(0);   is cached flag IDecider*/
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

/**
 * Serialize a {@link INodeSymlink} node
 * @param node The node to write/*from  w  w w.  j a v a2s  .  c  o  m*/
 * @param out The {@link DataOutput} where the fields are written
 */
private static void writeINodeSymlink(INodeSymlink node, DataOutput out) throws IOException {
    writeLocalName(node, out);
    out.writeLong(node.getId());
    out.writeShort(0); // replication
    out.writeLong(0); // modification time
    out.writeLong(0); // access time
    out.writeLong(0); // preferred block size
    out.writeInt(-2); // # of blocks

    Text.writeString(out, node.getSymlinkString());
    writePermissionStatus(node, out);
    out.writeLong(0); // access count IDecider
    out.writeInt(0); // is cached flag IDecider
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

/** Serialize a {@link INodeReference} node */
private static void writeINodeReference(INodeReference ref, DataOutput out, boolean writeUnderConstruction,
        ReferenceMap referenceMap) throws IOException {
    writeLocalName(ref, out);//from   ww  w  .  ja  v  a2 s. c om
    out.writeLong(ref.getId());
    out.writeShort(0); // replication
    out.writeLong(0); // modification time
    out.writeLong(0); // access time
    out.writeLong(0); // preferred block size
    out.writeInt(-3); // # of blocks

    final boolean isWithName = ref instanceof INodeReference.WithName;
    out.writeBoolean(isWithName);

    if (!isWithName) {
        Preconditions.checkState(ref instanceof INodeReference.DstReference);
        // dst snapshot id
        out.writeInt(((INodeReference.DstReference) ref).getDstSnapshotId());
    } else {
        out.writeInt(((INodeReference.WithName) ref).getLastSnapshotId());
    }

    final INodeReference.WithCount withCount = (INodeReference.WithCount) ref.getReferredINode();
    referenceMap.writeINodeReferenceWithCount(withCount, out, writeUnderConstruction);
    out.writeLong(0); // access count IDecider
    out.writeInt(0); // is cached flag IDecider
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

public static void writeBytes(byte[] data, DataOutput out) throws IOException {
    out.writeShort(data.length);
    out.write(data);//from  w w w  .  ja v a 2  s .c  o m
}

From source file:org.apache.hawq.pxf.service.io.GPDBWritable.java

@Override
public void write(DataOutput out) throws IOException {
    int numCol = colType.length;
    boolean[] nullBits = new boolean[numCol];
    int[] colLength = new int[numCol];
    byte[] enumType = new byte[numCol];
    int[] padLength = new int[numCol];
    byte[] padbytes = new byte[8];

    /**//w  w w. j  a va  2  s.c o  m
     * Compute the total payload and header length
     * header = total length (4 byte), Version (2 byte), Error (1 byte), #col (2 byte)
     * col type array = #col * 1 byte
     * null bit array = ceil(#col/8)
     */
    int datlen = 4 + 2 + 1 + 2;
    datlen += numCol;
    datlen += getNullByteArraySize(numCol);

    for (int i = 0; i < numCol; i++) {
        /* Get the enum type */
        DBType coldbtype;
        switch (DataType.get(colType[i])) {
        case BIGINT:
            coldbtype = DBType.BIGINT;
            break;
        case BOOLEAN:
            coldbtype = DBType.BOOLEAN;
            break;
        case FLOAT8:
            coldbtype = DBType.FLOAT8;
            break;
        case INTEGER:
            coldbtype = DBType.INTEGER;
            break;
        case REAL:
            coldbtype = DBType.REAL;
            break;
        case SMALLINT:
            coldbtype = DBType.SMALLINT;
            break;
        case BYTEA:
            coldbtype = DBType.BYTEA;
            break;
        default:
            coldbtype = DBType.TEXT;
        }
        enumType[i] = (byte) (coldbtype.ordinal());

        /* Get the actual value, and set the null bit */
        if (colValue[i] == null) {
            nullBits[i] = true;
            colLength[i] = 0;
        } else {
            nullBits[i] = false;

            /*
                 * For fixed length type, we get the fixed length.
             * For var len binary format, the length is in the col value.
             * For text format, we must convert encoding first.
             */
            if (!coldbtype.isVarLength()) {
                colLength[i] = coldbtype.getTypeLength();
            } else if (!isTextForm(colType[i])) {
                colLength[i] = ((byte[]) colValue[i]).length;
            } else {
                colLength[i] = ((String) colValue[i]).getBytes(CHARSET).length;
            }

            /* calculate and add the type alignment padding */
            padLength[i] = roundUpAlignment(datlen, coldbtype.getAlignment()) - datlen;
            datlen += padLength[i];

            /* for variable length type, we add a 4 byte length header */
            if (coldbtype.isVarLength()) {
                datlen += 4;
            }
        }
        datlen += colLength[i];
    }

    /*
     * Add the final alignment padding for the next record
     */
    int endpadding = roundUpAlignment(datlen, 8) - datlen;
    datlen += endpadding;

    /* Construct the packet header */
    out.writeInt(datlen);
    out.writeShort(VERSION);
    out.writeByte(errorFlag);
    out.writeShort(numCol);

    /* Write col type */
    for (int i = 0; i < numCol; i++) {
        out.writeByte(enumType[i]);
    }

    /* Nullness */
    byte[] nullBytes = boolArrayToByteArray(nullBits);
    out.write(nullBytes);

    /* Column Value */
    for (int i = 0; i < numCol; i++) {
        if (!nullBits[i]) {
            /* Pad the alignment byte first */
            if (padLength[i] > 0) {
                out.write(padbytes, 0, padLength[i]);
            }

            /* Now, write the actual column value */
            switch (DataType.get(colType[i])) {
            case BIGINT:
                out.writeLong(((Long) colValue[i]));
                break;
            case BOOLEAN:
                out.writeBoolean(((Boolean) colValue[i]));
                break;
            case FLOAT8:
                out.writeDouble(((Double) colValue[i]));
                break;
            case INTEGER:
                out.writeInt(((Integer) colValue[i]));
                break;
            case REAL:
                out.writeFloat(((Float) colValue[i]));
                break;
            case SMALLINT:
                out.writeShort(((Short) colValue[i]));
                break;

            /* For BYTEA format, add 4byte length header at the beginning  */
            case BYTEA:
                out.writeInt(colLength[i]);
                out.write((byte[]) colValue[i]);
                break;

            /* For text format, add 4byte length header. string is already '\0' terminated */
            default: {
                out.writeInt(colLength[i]);
                byte[] data = ((String) colValue[i]).getBytes(CHARSET);
                out.write(data);
                break;
            }
            }
        }
    }

    /* End padding */
    out.write(padbytes, 0, endpadding);
}

From source file:org.apache.pig.data.BinInterSedes.java

@Override
@SuppressWarnings("unchecked")
public void writeDatum(DataOutput out, Object val, byte type) throws IOException {
    switch (type) {
    case DataType.TUPLE:
        writeTuple(out, (Tuple) val);
        break;//from w w w  .  j a  v  a  2  s  . com

    case DataType.BAG:
        writeBag(out, (DataBag) val);
        break;

    case DataType.MAP: {
        writeMap(out, (Map<String, Object>) val);
        break;
    }

    case DataType.INTERNALMAP: {
        out.writeByte(INTERNALMAP);
        Map<Object, Object> m = (Map<Object, Object>) val;
        out.writeInt(m.size());
        Iterator<Map.Entry<Object, Object>> i = m.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<Object, Object> entry = i.next();
            writeDatum(out, entry.getKey());
            writeDatum(out, entry.getValue());
        }
        break;
    }

    case DataType.INTEGER:
        int i = (Integer) val;
        if (i == 0) {
            out.writeByte(INTEGER_0);
        } else if (i == 1) {
            out.writeByte(INTEGER_1);
        } else if (Byte.MIN_VALUE <= i && i <= Byte.MAX_VALUE) {
            out.writeByte(INTEGER_INBYTE);
            out.writeByte(i);
        } else if (Short.MIN_VALUE <= i && i <= Short.MAX_VALUE) {
            out.writeByte(INTEGER_INSHORT);
            out.writeShort(i);
        } else {
            out.writeByte(INTEGER);
            out.writeInt(i);
        }
        break;

    case DataType.LONG:
        long lng = (Long) val;
        if (lng == 0) {
            out.writeByte(LONG_0);
        } else if (lng == 1) {
            out.writeByte(LONG_1);
        } else if (Byte.MIN_VALUE <= lng && lng <= Byte.MAX_VALUE) {
            out.writeByte(LONG_INBYTE);
            out.writeByte((int) lng);
        } else if (Short.MIN_VALUE <= lng && lng <= Short.MAX_VALUE) {
            out.writeByte(LONG_INSHORT);
            out.writeShort((int) lng);
        } else if (Integer.MIN_VALUE <= lng && lng <= Integer.MAX_VALUE) {
            out.writeByte(LONG_ININT);
            out.writeInt((int) lng);
        } else {
            out.writeByte(LONG);
            out.writeLong(lng);
        }
        break;

    case DataType.DATETIME:
        out.writeByte(DATETIME);
        out.writeLong(((DateTime) val).getMillis());
        out.writeShort(((DateTime) val).getZone().getOffset((DateTime) val) / ONE_MINUTE);
        break;

    case DataType.FLOAT:
        out.writeByte(FLOAT);
        out.writeFloat((Float) val);
        break;

    case DataType.BIGINTEGER:
        out.writeByte(BIGINTEGER);
        writeBigInteger(out, (BigInteger) val);
        break;

    case DataType.BIGDECIMAL:
        out.writeByte(BIGDECIMAL);
        writeBigDecimal(out, (BigDecimal) val);
        break;

    case DataType.DOUBLE:
        out.writeByte(DOUBLE);
        out.writeDouble((Double) val);
        break;

    case DataType.BOOLEAN:
        if ((Boolean) val)
            out.writeByte(BOOLEAN_TRUE);
        else
            out.writeByte(BOOLEAN_FALSE);
        break;

    case DataType.BYTE:
        out.writeByte(BYTE);
        out.writeByte((Byte) val);
        break;

    case DataType.BYTEARRAY: {
        DataByteArray bytes = (DataByteArray) val;
        SedesHelper.writeBytes(out, bytes.mData);
        break;

    }

    case DataType.CHARARRAY: {
        SedesHelper.writeChararray(out, (String) val);
        break;
    }
    case DataType.GENERIC_WRITABLECOMPARABLE:
        out.writeByte(GENERIC_WRITABLECOMPARABLE);
        // store the class name, so we know the class to create on read
        writeDatum(out, val.getClass().getName());
        Writable writable = (Writable) val;
        writable.write(out);
        break;

    case DataType.NULL:
        out.writeByte(NULL);
        break;

    default:
        throw new RuntimeException("Unexpected data type " + val.getClass().getName() + " found in stream. "
                + "Note only standard Pig type is supported when you output from UDF/LoadFunc");
    }
}

From source file:org.apache.pig.data.BinInterSedes.java

private void writeMap(DataOutput out, Map<String, Object> m) throws IOException {

    final int sz = m.size();
    if (sz < UNSIGNED_BYTE_MAX) {
        out.writeByte(TINYMAP);//  www  . ja va2 s  .  co  m
        out.writeByte(sz);
    } else if (sz < UNSIGNED_SHORT_MAX) {
        out.writeByte(SMALLMAP);
        out.writeShort(sz);
    } else {
        out.writeByte(MAP);
        out.writeInt(sz);
    }
    Iterator<Map.Entry<String, Object>> i = m.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry<String, Object> entry = i.next();
        writeDatum(out, entry.getKey());
        writeDatum(out, entry.getValue());
    }
}