Example usage for java.io DataInputStream readUnsignedShort

List of usage examples for java.io DataInputStream readUnsignedShort

Introduction

In this page you can find the example usage for java.io DataInputStream readUnsignedShort.

Prototype

public final int readUnsignedShort() throws IOException 

Source Link

Document

See the general contract of the readUnsignedShort method of DataInput.

Usage

From source file:org.squidy.nodes.Tracking.java

/**
 * //from  w  w  w.j av  a2 s  . co  m
 */
private void startMulticastServer() throws ProcessException {
    InetAddress multicastGroup;
    try {
        multicastGroup = InetAddress.getByName(multicastGroupAddress);
    } catch (UnknownHostException e) {
        throw new ProcessException(e.getMessage(), e);
    }

    server = new MulticastServer(multicastGroup, port);

    server.addMulticastListener(new MulticastAdapter() {

        /* (non-Javadoc)
         * @see org.squidy.manager.protocol.udp.UDPListener#parseData(byte[])
         */
        public void parseData(byte[] data) {
            // TODO [SF]: Do your parsing stuff here!!!
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream instream = new DataInputStream(bais);

            try {
                System.out.println("short: " + instream.readUnsignedShort() + " | "
                        + instream.readUnsignedShort() + " | " + (instream.readInt() & 0x7F));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //               
            //            String s = new String(data);
            //            System.out.println(s);

            /*   
             *              Data is coded with a started MessageID = 7
                         lastFrameCounter = <FrameId>;         
                                 
                          if (<singleMarker == true>){
                                  
                             trackedBodies = <numTrackedSingleMarker>;
                             bodyId = 0;
                                  
                             //Read x,y,z and convert to mm
                             x = <x>*100; 
                             y = <y>*100;
                             z = <z>*100;
                                  
                             publish(new DataPosition3D(Tracking.class, bodyID, x, y, z, width,
                              height, depth, lastFrameCounter, trackedBodies));
                                      
                           // only publish single marker positions, not the ones belonging to a rigit body   
                          }
                          if (<rigitBody == true>){
                                  
                             trackedBodies = <numTrackedRigitBodies>;
                               bodyId = <rigitBodyId>;             
                             //Read x,y,z and convert to mm
                             x = <x>*100; 
                             y = <y>*100;
                             z = <z>*100;
                                     
                             //Read quatrions
                             qx = <qx>;
                             qy = <qy>;
                             qz = <qz>;
                             qw = <qw>;
                                     
                             //Transform to rotation matrix
                             //Spalte 1:
                           rxx = 2*(qx*qx + qw*qw)-1;
                           ryx = 2*(qx*qy + qz*qw);
                           rzx = 2*(qx*qz - qy*qw);
                           Spalte 2:
                           rxy = 2*(qx*qy - qz*qw);
                           ryy = 2*(qy*qy + qq*qw)-1;
                           rzy = 2*(qy*qz + qx*qw);
                           Spalte 3:
                           rxz = 2*(qx*qz + qy*qw);
                           ryz = 2*(qy*qz - qx*qw);
                           rzz = 2*(qz*qz + qw*qw)-1;
                                
                           //quadToMatrix: ?
                           //m[0] = 1-2*q[1]*q[1]-2*q[2]*q[2]; m[1] = 2*q[0]*q[1]-2*q[3]*q[2];   m[2] = 2*q[0]*q[2]+2*q[3]*q[1];
                             //m[3] = 2*q[0]*q[1]+2*q[3]*q[2];   m[4] = 1-2*q[0]*q[0]-2*q[2]*q[2]; m[5] = 2*q[1]*q[2]-2*q[3]*q[0];
                             //m[6] = 2*q[0]*q[2]-2*q[3]*q[1];   m[7] = 2*q[1]*q[2]+2*q[3]*q[0];   m[8] = 1-2*q[0]*q[0]-2*q[1]*q[1];
                                  
                             publish(new DataPosition6D(arTracking.getClass(), bodyID, x, y, z, width,
                           height, depth, rxx, ryx, rzx, rxy, ryy, rzy, rxz, ryz, rzz, lastFrameCounter,
                           trackedBodies));
                    
                                
                        }
                                
                                
            */

            //         
            //             ...
        }
    });
}

From source file:tor.TorCrypto.java

/**
 * Parses a public key encoded as ASN.1//  w w  w .  ja  v  a  2  s .  c  o m
 *
 * @param rsapublickey ASN.1 Encoded public key
 * @return PublicKey
 */
public static PublicKey asn1GetPublicKey(byte[] rsapublickey) {
    int blobsize = rsapublickey.length;
    DataInputStream dis = null;
    int jint = 0; // int to represent unsigned byte or unsigned short
    int datacount = 0;

    try {
        // --- Try to read the ANS.1 encoded RSAPublicKey blob -------------
        ByteArrayInputStream bis = new ByteArrayInputStream(rsapublickey);
        dis = new DataInputStream(bis);

        if (dis.readByte() != 0x30) // asn.1 encoded starts with 0x30
            return null;

        jint = dis.readUnsignedByte(); // asn.1 is 0x80 plus number of bytes
        // representing data count
        if (jint == 0x81)
            datacount = dis.readUnsignedByte(); // datalength is specified
        // in next byte.
        else if (jint == 0x82) // bytes count for any supported keysize
            // would be at most 2 bytes
            datacount = dis.readUnsignedShort(); // datalength is specified
        // in next 2 bytes
        else
            return null; // all supported publickey byte-sizes can be
        // specified in at most 2 bytes

        if ((jint - 0x80 + 2 + datacount) != blobsize) // sanity check for
            // correct number of
            // remaining bytes
            return null;

        //      System.out
        //         .println("\nRead outer sequence bytes; validated outer asn.1 consistency ");

        // ------- Next attempt to read Integer sequence for modulus ------
        if (dis.readUnsignedByte() != 0x02) // next byte read must be
            // Integer asn.1 specifier
            return null;
        jint = dis.readUnsignedByte(); // asn.1 is 0x80 plus number of bytes
        // representing data count
        if (jint == 0x81)
            datacount = dis.readUnsignedByte(); // datalength is specified
        // in next byte.
        else if (jint == 0x82) // bytes count for any supported keysize
            // would be at most 2 bytes
            datacount = dis.readUnsignedShort(); // datalength is specified
        // in next 2 bytes
        else
            return null; // all supported publickey modulus byte-sizes can
        // be specified in at most 2 bytes

        // ---- next bytes are big-endian ordered modulus -----
        byte[] modulus = new byte[datacount];
        int modbytes = dis.read(modulus);
        if (modbytes != datacount) // if we can read enought modulus bytes
            // ...
            return null;

        //System.out.println("Read modulus");

        // ------- Next attempt to read Integer sequence for public exponent
        // ------
        if (dis.readUnsignedByte() != 0x02) // next byte read must be
            // Integer asn.1 specifier
            return null;
        datacount = dis.readUnsignedByte(); // size of modulus is specified
        // in one byte
        byte[] exponent = new byte[datacount];
        int expbytes = dis.read(exponent);
        if (expbytes != datacount)
            return null;
        //System.out.println("Read exponent");

        // ----- Finally, create the PublicKey object from modulus and
        // public exponent --------
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(1, modulus),
                new BigInteger(1, exponent));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
        return pubKey;
    } catch (Exception exc) {
        return null;
    } finally {
        try {
            dis.close();
        } catch (Exception exc) {
            /* ignore */
            ;
        }
    }
}

From source file:VASSAL.tools.imports.adc2.MapBoard.java

/**
 * Hex lines are like spokes of the hex and are typically used for things like roads or other elements that
 * traverse from hex to hex.  The direction of each spoke is encoded as bit flags, and while ADC2 could encode
 * each hex with only one record, modules typically have a separate record for every spoke resulting in
 * data inflation.//from  w  w w.j  a va  2  s. c om
 */
protected void readHexLineBlock(DataInputStream in) throws IOException {
    ADC2Utils.readBlockHeader(in, "Hex Line");

    int nHexLines = ADC2Utils.readBase250Word(in);
    for (int i = 0; i < nHexLines; ++i) {
        int index = ADC2Utils.readBase250Word(in);
        int line = in.readUnsignedByte();
        int direction = in.readUnsignedShort();
        if (isOnMapBoard(index))
            hexLines.add(new HexLine(index, line, direction));
    }
}