Example usage for java.io ObjectOutput writeShort

List of usage examples for java.io ObjectOutput writeShort

Introduction

In this page you can find the example usage for java.io ObjectOutput 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:com.splicemachine.pipeline.constraint.ConstraintContext.java

@Override
public void writeExternal(ObjectOutput objectOutput) throws IOException {
    short len = (short) (messageArgs == null ? 0 : messageArgs.length);
    objectOutput.writeShort(len);
    for (int i = 0; i < len; i++) {
        objectOutput.writeUTF(messageArgs[i]);
    }// w ww . j  a va2  s .c o  m
}

From source file:com.delphix.session.impl.frame.SerialNumber.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    out.writeByte(serialBits);/*from   ww  w .j a  va 2s  .co  m*/

    if (serialBits < Byte.SIZE) {
        out.writeByte((int) serialNumber);
    } else if (serialBits < Short.SIZE) {
        out.writeShort((int) serialNumber);
    } else if (serialBits < Integer.SIZE) {
        out.writeInt((int) serialNumber);
    } else {
        out.writeLong(serialNumber);
    }
}

From source file:com.weibo.api.motan.protocol.rpc.CompressRpcCodec.java

private void addAttachment(ObjectOutput output, Map<String, String> attachments) throws IOException {
    output.writeShort(attachments.size());
    for (Map.Entry<String, String> entry : attachments.entrySet()) {
        output.writeUTF(entry.getKey());
        output.writeUTF(entry.getValue());
    }/*  w w w. ja  va2s.c  o m*/
}

From source file:com.weibo.api.motan.protocol.rpc.CompressRpcCodec.java

/**
 * request body ?/*from  www.  jav a 2 s .com*/
 * 
 * <pre>
* 
*     body:
* 
*     byte[] data :  
* 
*          serialize(interface_name, method_name, method_param_desc, method_param_value, attachments_size, attachments_value) 
* 
*   method_param_desc:  for_each (string.append(method_param_interface_name))
* 
*   method_param_value: for_each (method_param_name, method_param_value)
* 
*     attachments_value:  for_each (attachment_name, attachment_value)
* 
* </pre>
 * 
 * @param request
 * @return
 * @throws IOException
 */
private byte[] encodeRequest(Channel channel, Request request) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutput output = createOutput(outputStream);
    addMethodInfo(output, request);

    Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(
            channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));

    if (request.getArguments() != null && request.getArguments().length > 0) {
        for (Object obj : request.getArguments()) {
            serialize(output, obj, serialization);
        }
    }

    if (request.getAttachments() == null || request.getAttachments().isEmpty()) {
        // empty attachments
        output.writeShort(0);
    } else {
        // ?copyattachment????request???
        Map<String, String> attachments = copyMap(request.getAttachments());
        replaceAttachmentParamsBySign(channel, attachments);

        addAttachment(output, attachments);
    }

    output.flush();
    byte[] body = outputStream.toByteArray();

    byte flag = MotanConstants.FLAG_REQUEST;

    output.close();
    Boolean usegz = channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(),
            URLParamType.usegz.getBooleanValue());
    int minGzSize = channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(),
            URLParamType.mingzSize.getIntValue());
    return encode(compress(body, usegz, minGzSize), flag, request.getRequestId());
}

From source file:com.weibo.api.motan.protocol.rpc.CompressRpcCodec.java

/**
 * response body ?//from   w w  w  .  j  av  a  2  s  . c  o  m
 * 
 * <pre>
* 
* body:
* 
*     byte[] :  serialize (result) or serialize (exception)
* 
* </pre>
 *
 * @param channel
 * @param value
 * @return
 * @throws IOException
 */
private byte[] encodeResponse(Channel channel, Response value) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutput output = createOutput(outputStream);
    Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(
            channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));

    byte flag = 0;

    output.writeLong(value.getProcessTime());

    if (value.getException() != null) {
        output.writeUTF(value.getException().getClass().getName());
        serialize(output, value.getException(), serialization);
        flag = MotanConstants.FLAG_RESPONSE_EXCEPTION;
    } else if (value.getValue() == null) {
        flag = MotanConstants.FLAG_RESPONSE_VOID;
    } else {
        output.writeUTF(value.getValue().getClass().getName());
        serialize(output, value.getValue(), serialization);
        // v2?responseattachment
        Map<String, String> attachments = value.getAttachments();
        if (attachments != null) {
            String signed = attachments.get(ATTACHMENT_SIGN);
            String unSigned = attachments.get(UN_ATTACHMENT_SIGN);
            attachments.clear(); // attachment????

            if (StringUtils.isNotBlank(signed)) {
                attachments.put(ATTACHMENT_SIGN, signed);
            }
            if (StringUtils.isNotBlank(unSigned)) {
                attachments.put(UN_ATTACHMENT_SIGN, unSigned);
            }
        }
        if (attachments != null && !attachments.isEmpty()) {// ??
            addAttachment(output, attachments);
        } else {
            // empty attachments
            output.writeShort(0);
        }
        flag = MotanConstants.FLAG_RESPONSE_ATTACHMENT; // v2flag
    }

    output.flush();

    byte[] body = outputStream.toByteArray();

    output.close();
    Boolean usegz = channel.getUrl().getBooleanParameter(URLParamType.usegz.getName(),
            URLParamType.usegz.getBooleanValue());
    int minGzSize = channel.getUrl().getIntParameter(URLParamType.mingzSize.getName(),
            URLParamType.mingzSize.getIntValue());
    return encode(compress(body, usegz, minGzSize), flag, value.getRequestId());
}