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.carbondata.core.metadata.blocklet.BlockletInfo.java

@Override
public void write(DataOutput output) throws IOException {
    output.writeLong(dimensionOffset);/*w  ww.  ja v a 2  s.  c om*/
    output.writeLong(measureOffsets);
    int dsize = dimensionChunkOffsets != null ? dimensionChunkOffsets.size() : 0;
    output.writeShort(dsize);
    for (int i = 0; i < dsize; i++) {
        output.writeLong(dimensionChunkOffsets.get(i));
    }
    for (int i = 0; i < dsize; i++) {
        output.writeInt(dimensionChunksLength.get(i));
    }
    int mSize = measureChunkOffsets != null ? measureChunkOffsets.size() : 0;
    output.writeShort(mSize);
    for (int i = 0; i < mSize; i++) {
        output.writeLong(measureChunkOffsets.get(i));
    }
    for (int i = 0; i < mSize; i++) {
        output.writeInt(measureChunksLength.get(i));
    }
    writeChunkInfoForOlderVersions(output);

    boolean isSortedPresent = (isSorted != null);
    output.writeBoolean(isSortedPresent);
    if (isSortedPresent) {
        output.writeBoolean(isSorted);
    }
    if (null != getNumberOfRowsPerPage()) {
        output.writeShort(getNumberOfRowsPerPage().length);
        for (int i = 0; i < getNumberOfRowsPerPage().length; i++) {
            output.writeInt(getNumberOfRowsPerPage()[i]);
        }
    }
}

From source file:org.apache.carbondata.core.metadata.blocklet.BlockletInfo.java

/**
 * Serialize datachunks as well for older versions like V1 and V2
 *//* ww  w . j  av a 2  s .c om*/
private void writeChunkInfoForOlderVersions(DataOutput output) throws IOException {
    int dimChunksSize = dimensionColumnChunk != null ? dimensionColumnChunk.size() : 0;
    output.writeShort(dimChunksSize);
    for (int i = 0; i < dimChunksSize; i++) {
        byte[] bytes = serializeDataChunk(dimensionColumnChunk.get(i));
        output.writeInt(bytes.length);
        output.write(bytes);
    }
    int msrChunksSize = measureColumnChunk != null ? measureColumnChunk.size() : 0;
    output.writeShort(msrChunksSize);
    for (int i = 0; i < msrChunksSize; i++) {
        byte[] bytes = serializeDataChunk(measureColumnChunk.get(i));
        output.writeInt(bytes.length);
        output.write(bytes);
    }
}

From source file:org.apache.carbondata.core.metadata.schema.table.DataMapSchema.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeUTF(dataMapName);//w w w  . ja  va2  s .c  o m
    out.writeUTF(providerName);
    boolean isRelationIdentifierExists = null != relationIdentifier;
    out.writeBoolean(isRelationIdentifierExists);
    if (isRelationIdentifierExists) {
        this.relationIdentifier.write(out);
    }
    boolean isChildSchemaExists = null != this.childSchema;
    out.writeBoolean(isChildSchemaExists);
    if (isChildSchemaExists) {
        this.childSchema.write(out);
    }
    if (properties == null) {
        out.writeShort(0);
    } else {
        out.writeShort(properties.size());
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            out.writeUTF(entry.getKey());
            out.writeUTF(entry.getValue());
        }
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static void writeDSFIDHeader(int dsfid, DataOutput out) throws IOException {
    if (dsfid == DataSerializableFixedID.ILLEGAL) {
        throw new IllegalStateException(
                LocalizedStrings.InternalDataSerializer_ATTEMPTED_TO_SERIALIZE_ILLEGAL_DSFID
                        .toLocalizedString());
    }//from  w  ww  .  j av  a2s .  co  m
    if (dsfid <= Byte.MAX_VALUE && dsfid >= Byte.MIN_VALUE) {
        out.writeByte(DS_FIXED_ID_BYTE);
        out.writeByte(dsfid);
    } else if (dsfid <= Short.MAX_VALUE && dsfid >= Short.MIN_VALUE) {
        out.writeByte(DS_FIXED_ID_SHORT);
        out.writeShort(dsfid);
    } else {
        out.writeByte(DS_FIXED_ID_INT);
        out.writeInt(dsfid);
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

/**
 * Data serializes an instance of a "user class" (that is, a class that can be handled by a
 * registered {@code DataSerializer}) to the given {@code DataOutput}.
 *
 * @return {@code true} if {@code o} was written to {@code out}.
 *//*from  w  w  w .j av  a2s .co  m*/
private static boolean writeUserObject(Object o, DataOutput out, boolean ensurePdxCompatibility)
        throws IOException {

    final Class<?> c = o.getClass();
    final DataSerializer serializer = InternalDataSerializer.getSerializer(c);
    if (serializer != null) {
        int id = serializer.getId();
        if (id != 0) {
            checkPdxCompatible(o, ensurePdxCompatibility);
            // id will be 0 if it is a WellKnowDS
            if (id <= Byte.MAX_VALUE && id >= Byte.MIN_VALUE) {
                out.writeByte(USER_CLASS);
                out.writeByte((byte) id);
            } else if (id <= Short.MAX_VALUE && id >= Short.MIN_VALUE) {
                out.writeByte(USER_CLASS_2);
                out.writeShort(id);
            } else {
                out.writeByte(USER_CLASS_4);
                out.writeInt(id);
            }
        } else {
            if (ensurePdxCompatibility) {
                if (!(serializer instanceof WellKnownPdxDS)) {
                    checkPdxCompatible(o, ensurePdxCompatibility);
                }
            }
        }
        boolean toDataResult;
        try {
            toDataResult = serializer.toData(o, out);
        } catch (IOException io) {
            if (serializer instanceof WellKnownDS) {
                // this is not user code so throw IOException
                throw io; // see bug 44659
            } else {
                // We no longer rethrow IOException here
                // because if user code throws an IOException we want
                // to create a ToDataException to report it as a problem
                // with the plugin code.
                throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c,
                        io);
            }
        } catch (CancelException | ToDataException | GemFireRethrowable ex) {
            // Serializing a PDX can result in a cache closed exception. Just rethrow
            throw ex;
        } catch (VirtualMachineError err) {
            SystemFailure.initiateFailure(err);
            // If this ever returns, rethrow the error. We're poisoned
            // now, so don't let this thread continue.
            throw err;
        } catch (Throwable t) {
            // Whenever you catch Error or Throwable, you must also
            // catch VirtualMachineError (see above). However, there is
            // _still_ a possibility that you are dealing with a cascading
            // error condition, so you also need to check to see if the JVM
            // is still usable:
            SystemFailure.checkFailure();
            throw new ToDataException("toData failed on DataSerializer with id=" + id + " for class " + c, t);
        }
        if (toDataResult) {
            return true;
        } else {
            throw new ToDataException(
                    LocalizedStrings.DataSerializer_SERIALIZER_0_A_1_SAID_THAT_IT_COULD_SERIALIZE_AN_INSTANCE_OF_2_BUT_ITS_TODATA_METHOD_RETURNED_FALSE
                            .toLocalizedString(serializer.getId(), serializer.getClass().getName(),
                                    o.getClass().getName()));
        }
        // Do byte[][] and Object[] here to fix bug 44060
    } else if (o instanceof byte[][]) {
        byte[][] byteArrays = (byte[][]) o;
        out.writeByte(ARRAY_OF_BYTE_ARRAYS);
        writeArrayOfByteArrays(byteArrays, out);
        return true;
    } else if (o instanceof Object[]) {
        Object[] array = (Object[]) o;
        out.writeByte(OBJECT_ARRAY);
        writeObjectArray(array, out, ensurePdxCompatibility);
        return true;
    } else if (is662SerializationEnabled()
            && (o.getClass().isEnum()/* for bug 52271 */ || (o.getClass().getSuperclass() != null
                    && o.getClass().getSuperclass().isEnum()))) {
        if (isPdxSerializationInProgress()) {
            writePdxEnum((Enum<?>) o, out);
        } else {
            checkPdxCompatible(o, ensurePdxCompatibility);
            writeGemFireEnum((Enum<?>) o, out);
        }
        return true;
    } else {
        PdxSerializer pdxSerializer = TypeRegistry.getPdxSerializer();
        return pdxSerializer != null && writePdx(out, null, o, pdxSerializer);
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static void writeUserDataSerializableHeader(int classId, DataOutput out) throws IOException {
    if (classId <= Byte.MAX_VALUE && classId >= Byte.MIN_VALUE) {
        out.writeByte(USER_DATA_SERIALIZABLE);
        out.writeByte(classId);/*from   w  w w .ja va  2 s. c  o  m*/
    } else if (classId <= Short.MAX_VALUE && classId >= Short.MIN_VALUE) {
        out.writeByte(USER_DATA_SERIALIZABLE_2);
        out.writeShort(classId);
    } else {
        out.writeByte(USER_DATA_SERIALIZABLE_4);
        out.writeInt(classId);
    }
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

public static void writeArrayLength(int len, DataOutput out) throws IOException {
    if (len == -1) {
        out.writeByte(NULL_ARRAY);/*from   w  ww.j a  v a 2  s .c o  m*/
    } else if (len <= MAX_BYTE_ARRAY_LEN) {
        out.writeByte(len);
    } else if (len <= 0xFFFF) {
        out.writeByte(SHORT_ARRAY_LEN);
        out.writeShort(len);
    } else {
        out.writeByte(INT_ARRAY_LEN);
        out.writeInt(len);
    }
}

From source file:org.apache.hadoop.fs.permission.FsPermission.java

/** {@inheritDoc} */
public void write(DataOutput out) throws IOException {
    out.writeShort(toShort());
}

From source file:org.apache.hadoop.hbase.filter.RowListFilter.java

@Override
public void write(DataOutput dout) throws IOException {
    dout.writeInt(bytesSetIterator.previousIndex());
    dout.writeInt(bytesSet.size());/*from   www  .j a v  a2 s .  c om*/
    for (byte[] bytes : bytesSet) {
        dout.writeShort(bytes.length);
        dout.write(bytes, 0, bytes.length);
    }
}

From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java

/**
 * Write a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from   w w  w  . j  a v a  2  s  . co m*/
 * @param out
 * @param instance
 * @param declaredClass
 * @param conf
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public 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 (declClass.equals(Result[].class)) {
            Result.writeArray(out, (Result[]) 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 {
        throw new IOException("Can't write: " + instanceObj + " as " + declClass);
    }
}