100% found this document useful (1 vote)
136 views4 pages

Watts SDK Protocol Specification - Sample Code For Reading Message

This document contains sample Java code for reading messages from a data stream according to the Watts SDK Protocol specification. It defines methods for reading the length of an incoming message, reading the message contents byte-by-byte into a buffer, and converting bytes to unsigned integer values since the DataInputStream class does not support unsigned data types. The main readMessage method returns a byte array containing the full contents of a single message read from the input stream.

Uploaded by

Mohran Hakim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
136 views4 pages

Watts SDK Protocol Specification - Sample Code For Reading Message

This document contains sample Java code for reading messages from a data stream according to the Watts SDK Protocol specification. It defines methods for reading the length of an incoming message, reading the message contents byte-by-byte into a buffer, and converting bytes to unsigned integer values since the DataInputStream class does not support unsigned data types. The main readMessage method returns a byte array containing the full contents of a single message read from the input stream.

Uploaded by

Mohran Hakim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Watts SDK Protocol specification - Sample code for

reading message
package com.traficon.util;

import java.io.DataInputStream;
import java.io.IOException;

public class SdkCommunicationsUtil


{
// -------------------------- STATIC METHODS --------------------------
/**
* Reads one message from the stream. Returns the the full message including
* header and payload
*
* @param inputStream the stream to read from
* @return an array of bytes representing a full framing of a message
*/
public static byte[] readMessage( DataInputStream inputStream )
{
int msglen;
int msglen1 = readUnsignedByte( inputStream );
int msglen2 = readUnsignedByte( inputStream );
int msglen3 = readUnsignedByte( inputStream );
int msglen4 = readUnsignedByte( inputStream );

if (msglen4 == 0)
{
msglen = (msglen1 * 256) + msglen2;
}
else if (msglen4 == 1)
{
msglen = (msglen1 * 256 * 256) + (msglen2 * 256) + msglen3;
}
else
{
throw new RuntimeException( "Protocol-length version not correct in first 4 bytes:
" + msglen1 + "," + msglen2 + "," + msglen3 + "," + msglen4 );
}

// for robustness store the message in a bytebuffer before continuing


byte[] bytes = new byte[msglen + 2];
bytes[0] = (byte)msglen1;
bytes[1] = (byte)msglen2;
bytes[2] = (byte)msglen3;
bytes[3] = (byte)msglen4;

try
{
int bytesLeft = msglen - 2;
while (bytesLeft > 0)
{ // keep reading until everything is read
int res = inputStream.read( bytes, 2 + msglen - bytesLeft, bytesLeft );
if (res != -1)
{
bytesLeft -= res;
}
else
{
throw new RuntimeException( "Error reading Socket InStream" );
}
}
}
catch (IOException e)
{
throw new RuntimeException( e );
}

return bytes;
}

/**
* reads an unsigned byte from the stream
*
* @param inputStream the stream to read from
* @return an unsigned byte
*/
private static short readUnsignedByte( DataInputStream inputStream )
{
try
{
return toUnsigned( inputStream.readByte() );
}
catch (IOException e)
{
throw new RuntimeException( e );
}
}

/**
* Interprets the bits of a byte as a unsigned byte. eg. 52->52, -126 -> 130
*
* @param b the byte
* @return a short containing the value of the unsigned byte
*/
private static short toUnsigned( byte b )
{
return b < 0 ? (short)(b + 256) : b;
}

/**
* convert the bytes to an integer. When more that 4 bytes are given, only the first
four bytes are taken into account
*
* @param bytes The bytes to be converted. Most significant byte first.
* @return A long represented by the byte array
*/
private static long toUnsigned( byte... bytes )
{
long result = 0;
final int length = Math.min( 4, bytes.length );
for (int i = 0; i < length; i++)
{
result += toUnsigned( bytes[i] ) << ((length - 1 - i) * 8);
}
return result;
}

/**
* This function is needed because DataInputStream does not support readUnsignedInt
*
* @param dataInputStream the input stream
* @return unsigned int value
* @throws java.io.IOException due to error while reading stream
*/
private static long readUnsignedInt( DataInputStream dataInputStream ) throws
IOException
{
byte[] bytes = new byte[4];
dataInputStream.readFully( bytes );
return toUnsigned( bytes );
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy