Example usage for java.nio ByteOrder BIG_ENDIAN

List of usage examples for java.nio ByteOrder BIG_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder BIG_ENDIAN.

Prototype

ByteOrder BIG_ENDIAN

To view the source code for java.nio ByteOrder BIG_ENDIAN.

Click Source Link

Document

This constant represents big endian.

Usage

From source file:com.arpnetworking.tsdcore.model.AggregationMessage.java

/**
 * Deserialize message from <code>Buffer</code>.
 *
 * TODO(vkoskela): The header and message need to be versioned [MAI-133].
 *
 * @param data The <code>Buffer</code> containing the serialized message.
 * @return The deserialized <code>AggregationMessage</code> or absent if
 * the <code>Buffer</code> could not be deserialized.
 *///from   w  w  w . j a  v a2  s. c om
public static Optional<AggregationMessage> deserialize(final ByteString data) {
    int position = 0;
    // Make sure we have enough data to get the size
    if (data.length() < HEADER_SIZE_IN_BYTES) {
        return Optional.absent();
    }

    // Deserialize and validate buffer length
    final ByteIterator reader = data.iterator();
    final int length = reader.getInt(ByteOrder.BIG_ENDIAN);
    position += INTEGER_SIZE_IN_BYTES;
    if (data.length() < length) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(String.format("we only have %d of %d bytes.", data.length(), length));
        }
        return Optional.absent();
    }

    // Deserialize message type
    final byte type = reader.getByte();
    position += BYTE_SIZE_IN_BYTES;

    final byte subType;
    if (typeHasSubtype(type)) {
        subType = reader.getByte();
        position += BYTE_SIZE_IN_BYTES;
    } else {
        subType = 0x00;
    }

    // Obtain the serialized payload
    final byte[] payloadBytes = new byte[length - position];
    reader.getBytes(payloadBytes);

    // Deserialize the message based on the type
    try {
        switch (type) {
        case 0x01:
            return Optional.of(new AggregationMessage(Messages.HostIdentification.parseFrom(payloadBytes)));
        case 0x03:
            return Optional.of(new AggregationMessage(Messages.HeartbeatRecord.parseFrom(payloadBytes)));
        case 0x04:
            return Optional.of(new AggregationMessage(Messages.StatisticSetRecord.parseFrom(payloadBytes)));
        case 0x05:
            // 0x05 is the message type for all supporting data
            switch (subType) {
            case 0x01:
                return Optional
                        .of(new AggregationMessage(Messages.SamplesSupportingData.parseFrom(payloadBytes)));
            case 0x02:
                return Optional.of(
                        new AggregationMessage(Messages.SparseHistogramSupportingData.parseFrom(payloadBytes)));
            default:
                LOGGER.warn(
                        String.format("Invalid protocol buffer, unknown subtype; type=%s, subtype=%s, bytes=%s",
                                type, subType, Hex.encodeHexString(payloadBytes)));
                return Optional.absent();
            }
        default:
            LOGGER.warn(String.format("Unsupported message type; type=%s", type));
            return Optional.absent();
        }
    } catch (final InvalidProtocolBufferException e) {
        LOGGER.warn(String.format("Invalid protocol buffer; type=%s bytes=%s", type,
                Hex.encodeHexString(payloadBytes)), e);
        return Optional.absent();
    }
}

From source file:io.warp10.quasar.trl.QuasarTokenRevocationListLoader.java

public static QuasarTokenRevocationListLoader getInstance(Properties config, byte[] appSipHashKey) {
    if (singleton.compareAndSet(false, true)) {
        ByteBuffer bb = ByteBuffer.wrap(appSipHashKey);
        bb.order(ByteOrder.BIG_ENDIAN);
        appIdSipHashKeyK0 = bb.getLong();
        appIdSipHashKeyK1 = bb.getLong();
        quasarTokenRevocationListLoader = new QuasarTokenRevocationListLoader(config);
    }//from   ww  w  .  j  a  v a  2s  .  co m
    return quasarTokenRevocationListLoader;
}

From source file:byps.BBufferJson.java

public BBufferJson(ByteBuffer buf) {
    super(BBinaryModel.MEDIUM, buf);
    if (buf != null) {
        buf.order(ByteOrder.BIG_ENDIAN);
    }
}

From source file:interactivespaces.service.comm.network.client.internal.netty.NettyUdpClientNetworkCommunicationEndpointService.java

@Override
public UdpClientNetworkCommunicationEndpoint newClient(Log log) {
    return newClient(ByteOrder.BIG_ENDIAN, log);
}

From source file:Main.java

/**
 * Creates the Uri string with embedded expansion codes.
 *
 * @param uri to be encoded/*w w  w  . j a va2  s  . com*/
 * @return the Uri string with expansion codes.
 */
public static byte[] encodeUri(String uri) {
    if (uri == null || uri.length() == 0) {
        Log.i(TAG, "null or empty uri");
        return new byte[0];
    }
    ByteBuffer bb = ByteBuffer.allocate(uri.length());
    // UUIDs are ordered as byte array, which means most significant first
    bb.order(ByteOrder.BIG_ENDIAN);
    int position = 0;

    // Add the byte code for the scheme or return null if none
    Byte schemeCode = encodeUriScheme(uri);
    if (schemeCode == null) {
        Log.i(TAG, "null scheme code");
        return null;
    }
    String scheme = URI_SCHEMES.get(schemeCode);
    bb.put(schemeCode);
    position += scheme.length();

    if (URLUtil.isNetworkUrl(scheme)) {
        Log.i(TAG, "is network URL");
        return encodeUrl(uri, position, bb);
    } else if ("urn:uuid:".equals(scheme)) {
        Log.i(TAG, "is UUID");
        return encodeUrnUuid(uri, position, bb);
    }
    return null;
}

From source file:net.jradius.freeradius.FreeRadiusRequest.java

public FreeRadiusRequest() {
    super();//  w  w  w.j  ava2s  .c o m

    buffer_in = ByteBuffer.allocate(25000);
    buffer_in.order(ByteOrder.BIG_ENDIAN);

    buffer_out = ByteBuffer.allocate(25000);
    buffer_out.order(ByteOrder.BIG_ENDIAN);
}

From source file:nl.salp.warcraft4j.casc.cdn.BlteMultiChunkParser.java

@Override
protected BlteChunkHeader parseChunkHeader(DataReader reader)
        throws DataReadingException, DataParsingException, CascParsingException {
    long compressedSize = reader.readNext(DataTypeFactory.getUnsignedInteger(), ByteOrder.BIG_ENDIAN);
    long decompressedSize = reader.readNext(DataTypeFactory.getUnsignedInteger(), ByteOrder.BIG_ENDIAN);
    byte[] hash = reader.readNext(DataTypeFactory.getByteArray(16));
    return new BlteChunkHeader(compressedSize, decompressedSize, hash);
}

From source file:com.indeed.lsmtree.recordlog.BasicRecordFile.java

public BasicRecordFile(File file, Serializer<E> serializer) throws IOException {
    this.file = file;
    this.serializer = serializer;
    buffer = new MMapBuffer(file, FileChannel.MapMode.READ_ONLY, ByteOrder.BIG_ENDIAN);
    memory = buffer.memory();/*from  ww  w.ja  v a  2 s.c om*/
}

From source file:eu.xworlds.util.raknet.protocol.BaseMessage.java

/**
 * writes an unsigned short to byte buf//from   www  .  j a  va 2 s. c o m
 * 
 * @param target target byte buffer.
 * @param value the unsigned short value.
 */
protected void writeUnsignedShort(ByteBuf target, int value) {
    if (value < 0 || value > 0xffff) {
        throw new IllegalArgumentException(value + " exceeds allowed size"); //$NON-NLS-1$
    }
    if (target.order() == ByteOrder.BIG_ENDIAN) {
        target.writeByte(value >> 8);
        target.writeByte(value);
    } else {
        target.writeByte(value);
        target.writeByte(value >> 8);
    }
}

From source file:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 *
 * @param digest byte array for encoding in base 64,
 * @param  chngByteOrd boolean indicating if to change byte order
 * @return String the encoded base64 of digest
 *//* w w  w. j a  v  a  2 s .c o m*/
public static String tobase64(byte[] digest, boolean chngByteOrd) {

    byte[] tobase64 = null;
    ByteOrder local = ByteOrder.nativeOrder();
    String ordbyte = local.toString();
    mLog.finer("Native byte order is: " + ordbyte);
    ByteBuffer btstream = ByteBuffer.wrap(digest);
    btstream.order(ByteOrder.BIG_ENDIAN);
    byte[] revdigest = null;
    if (chngByteOrd) {
        revdigest = changeByteOrder(digest, local);
    }
    if (revdigest != null) {
        btstream.put(revdigest);
    } else {
        btstream.put(digest);
    }

    tobase64 = Base64.encodeBase64(btstream.array());

    return new String(tobase64);

}