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.pig.data.BinInterSedes.java

private void writeBag(DataOutput out, DataBag bag) throws IOException {
    // We don't care whether this bag was sorted or distinct because
    // using the iterator to write it will guarantee those things come
    // correctly. And on the other end there'll be no reason to waste
    // time re-sorting or re-applying distinct.
    final long sz = bag.size();
    if (sz < UNSIGNED_BYTE_MAX) {
        out.writeByte(TINYBAG);/*from ww  w. j a v  a 2s. co  m*/
        out.writeByte((int) sz);
    } else if (sz < UNSIGNED_SHORT_MAX) {
        out.writeByte(SMALLBAG);
        out.writeShort((int) sz);
    } else {
        out.writeByte(BAG);
        out.writeLong(sz);
    }

    Iterator<Tuple> it = bag.iterator();
    while (it.hasNext()) {
        writeTuple(out, it.next());
    }

}

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

@SuppressWarnings("unchecked")
public static void writeDatum(DataOutput out, Object val) throws IOException {
    // Read the data type
    byte type = DataType.findType(val);
    switch (type) {
    case DataType.TUPLE:
        // Because tuples are written directly by hadoop, the
        // tuple's write method needs to write the indicator byte.
        // So don't write the indicator byte here as it is for
        // everyone else.
        ((Tuple) val).write(out);
        break;/* ww  w  .  j  av  a2 s .  c o m*/

    case DataType.BAG:
        out.writeByte(DataType.BAG);
        ((DataBag) val).write(out);
        break;

    case DataType.MAP: {
        out.writeByte(DataType.MAP);
        Map<String, Object> m = (Map<String, Object>) val;
        out.writeInt(m.size());
        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());
        }
        break;
    }

    case DataType.INTERNALMAP: {
        out.writeByte(DataType.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:
        out.writeByte(DataType.INTEGER);
        out.writeInt((Integer) val);
        break;

    case DataType.LONG:
        out.writeByte(DataType.LONG);
        out.writeLong((Long) val);
        break;

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

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

    case DataType.BOOLEAN:
        out.writeByte(DataType.BOOLEAN);
        out.writeBoolean((Boolean) val);
        break;

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

    case DataType.BYTEARRAY: {
        out.writeByte(DataType.BYTEARRAY);
        DataByteArray bytes = (DataByteArray) val;
        out.writeInt(bytes.size());
        out.write(bytes.mData);
        break;
    }

    case DataType.CHARARRAY: {
        String s = (String) val;
        byte[] utfBytes = s.getBytes(DataReaderWriter.UTF8);
        int length = utfBytes.length;

        if (length < DataReaderWriter.UNSIGNED_SHORT_MAX) {
            out.writeByte(DataType.CHARARRAY);
            out.writeShort(length);
            out.write(utfBytes);
        } else {
            out.writeByte(DataType.BIGCHARARRAY);
            out.writeInt(length);
            out.write(utfBytes);
        }
        break;
    }
    case DataType.GENERIC_WRITABLECOMPARABLE:
        out.writeByte(DataType.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(DataType.NULL);
        break;

    default:
        throw new RuntimeException("Unexpected data type " + type + " found in stream.");
    }
}

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

protected void write(DataOutput out, boolean writeIdentifiers) throws IOException {
    if (writeIdentifiers) {
        int id = getSchemaTupleIdentifier();
        if (id < BinInterSedes.UNSIGNED_BYTE_MAX) {
            out.writeByte(BinInterSedes.SCHEMA_TUPLE_BYTE_INDEX);
            out.writeByte(id);/*from   w  w  w .  ja  v  a  2s . com*/
        } else if (id < BinInterSedes.UNSIGNED_SHORT_MAX) {
            out.writeByte(BinInterSedes.SCHEMA_TUPLE_SHORT_INDEX);
            out.writeShort(id);
        } else {
            out.writeByte(BinInterSedes.SCHEMA_TUPLE);
            out.writeInt(id);
        }
    }
    writeElements(out);
}

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

protected static void write(DataOutput out, DateTime v) throws IOException {
    out.writeLong(v.getMillis());//w  w  w  .j a  va  2s.c  o  m
    out.writeShort(v.getZone().getOffset(v) / ONE_MINUTE);
}

From source file:org.cloudata.core.common.io.CObjectWritable.java

/** Write a {@link CWritable}, {@link String}, primitive type, or an array of
 * the preceding. *//*from   w  ww. j  av  a2s  . co m*/
public static void writeObject(DataOutput out, Object instance, Class declaredClass, CloudataConf conf,
        boolean arrayComponent) throws IOException {

    if (instance == null) { // null
        instance = new NullInstance(declaredClass, conf);
        declaredClass = CWritable.class;
        arrayComponent = false;
    }

    if (!arrayComponent) {
        CUTF8.writeString(out, declaredClass.getName()); // always write declared
        //System.out.println("Write:declaredClass.getName():" + declaredClass.getName());
    }

    if (declaredClass.isArray()) { // array
        int length = Array.getLength(instance);
        out.writeInt(length);
        //System.out.println("Write:length:" + length);

        if (declaredClass.getComponentType() == Byte.TYPE) {
            out.write((byte[]) instance);
        } else if (declaredClass.getComponentType() == ColumnValue.class) {
            //ColumnValue?  Deserialize? ?? ?   ?? ?  .
            writeColumnValue(out, instance, declaredClass, conf, length);
        } else {
            for (int i = 0; i < length; i++) {
                writeObject(out, Array.get(instance, i), declaredClass.getComponentType(), conf,
                        !declaredClass.getComponentType().isArray());
            }
        }
    } else if (declaredClass == String.class) { // String
        CUTF8.writeString(out, (String) instance);

    } else if (declaredClass.isPrimitive()) { // primitive type

        if (declaredClass == Boolean.TYPE) { // boolean
            out.writeBoolean(((Boolean) instance).booleanValue());
        } else if (declaredClass == Character.TYPE) { // char
            out.writeChar(((Character) instance).charValue());
        } else if (declaredClass == Byte.TYPE) { // byte
            out.writeByte(((Byte) instance).byteValue());
        } else if (declaredClass == Short.TYPE) { // short
            out.writeShort(((Short) instance).shortValue());
        } else if (declaredClass == Integer.TYPE) { // int
            out.writeInt(((Integer) instance).intValue());
        } else if (declaredClass == Long.TYPE) { // long
            out.writeLong(((Long) instance).longValue());
        } else if (declaredClass == Float.TYPE) { // float
            out.writeFloat(((Float) instance).floatValue());
        } else if (declaredClass == Double.TYPE) { // double
            out.writeDouble(((Double) instance).doubleValue());
        } else if (declaredClass == Void.TYPE) { // void
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }
    } else if (declaredClass.isEnum()) { // enum
        CUTF8.writeString(out, ((Enum) instance).name());
    } else if (CWritable.class.isAssignableFrom(declaredClass)) { // Writable
        if (instance.getClass() == declaredClass) {
            out.writeShort(TYPE_SAME); // ? ?? ? ?? 
            //System.out.println("Write:TYPE_SAME:" + TYPE_SAME);

        } else {
            out.writeShort(TYPE_DIFF);
            //System.out.println("Write:TYPE_DIFF:" + TYPE_DIFF);
            CUTF8.writeString(out, instance.getClass().getName());
            //System.out.println("Write:instance.getClass().getName():" + instance.getClass().getName());
        }
        ((CWritable) instance).write(out);
        //System.out.println("Write:instance value");

    } else {
        throw new IOException("Can't write: " + instance + " as " + declaredClass);
    }
}

From source file:org.gradoop.model.impl.properties.PropertyValueList.java

@Override
public void write(DataOutput dataOutput) throws IOException {
    dataOutput.writeShort(bytes.length);
    dataOutput.write(bytes);//from  w  w  w.  j  a va 2 s  .  c o  m
}

From source file:org.lealone.cluster.utils.ByteBufferUtil.java

public static void writeWithShortLength(byte[] buffer, DataOutput out) throws IOException {
    int length = buffer.length;
    assert 0 <= length && length <= Utils.MAX_UNSIGNED_SHORT : length;
    out.writeShort(length);
    out.write(buffer);/*from www .j  a  va2s.  co m*/
}

From source file:org.mule.transformer.simple.ModbusRequestToByteArray.java

public Object doTransform(Object src, String outputEncoding) throws TransformerException {
    ModbusRequest request = (ModbusRequest) src;

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutput dout = new DataOutputStream(os);
    try {//w w w. j av  a2s  .co m

        if (!request.isHeadless()) {
            dout.writeShort(request.getTransactionID());
            dout.writeShort(request.getProtocolID());
            dout.writeShort(request.getDataLength());
        }
        dout.writeByte(request.getUnitID());
        dout.writeByte(request.getFunctionCode());
        request.writeData(dout);

        return os.toByteArray();
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }

}

From source file:StorageEngineClient.MultiFormatStorageSplit.java

@Override
public void write(DataOutput out) throws IOException {
    if (path == null || path.length == 0) {
        out.writeInt(0);//from ww  w. j  av  a  2  s  .c o m
    } else {
        out.writeInt(path.length);
        for (int i = 0; i < path.length; i++) {
            int len = path[i].toString().length();
            if (len == 0) {
                out.writeShort((short) 0);
            } else {
                out.writeShort((short) len);
                out.write(path[i].toString().getBytes());
            }
        }
    }
}

From source file:TVA.Hadoop.MapReduce.Historian.File.StandardPointFile.java

/**
 * Serializes the point to disk./*from   w  w w.  ja  va2 s .  co  m*/
 * 
 * @param    out         A DataOutput object to write data to.
 * @see       DataOutput   
 * @see org.apache.hadoop.io.Writable#write(java.io.DataOutput)
 * 
 */
public void write(DataOutput out) throws IOException {

    out.writeInt(iTimeTag);
    out.writeShort(this.Flags);
    out.writeFloat(this.Value);
    out.writeInt(this.iPointID);

}