Example usage for java.io DataInputStream readInt

List of usage examples for java.io DataInputStream readInt

Introduction

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

Prototype

public final int readInt() throws IOException 

Source Link

Document

See the general contract of the readInt method of DataInput.

Usage

From source file:es.logongas.util.seguridad.CodigoVerificacionSeguro.java

public boolean isValido() {
    try {/*from w  w w .  j a  va 2  s.c  om*/
        Base32 base32 = new Base32();
        byte datos[] = base32.decode(valor);
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(datos));
        int key = dataInputStream.readInt();
        int numeroAleatorio = dataInputStream.readInt();
        int crcReal = dataInputStream.readInt();

        CRC crc = new CRC();
        crc.update(key).update(numeroAleatorio);

        if (crcReal == crc.getCRC()) {
            return true;
        } else {
            return false;
        }
    } catch (IOException ex) {
        return false;
    }
}

From source file:org.apache.cassandra.db.SliceByNamesReadCommand.java

@Override
public ReadCommand deserialize(DataInputStream dis) throws IOException {
    boolean isDigest = dis.readBoolean();
    String table = dis.readUTF();
    String key = dis.readUTF();/*from  w ww. j av  a  2s.c  om*/
    QueryPath columnParent = QueryPath.deserialize(dis);

    int size = dis.readInt();
    List<byte[]> columns = new ArrayList<byte[]>();
    for (int i = 0; i < size; ++i) {
        columns.add(ColumnSerializer.readName(dis));
    }
    SliceByNamesReadCommand rm = new SliceByNamesReadCommand(table, key, columnParent, columns);
    rm.setDigestQuery(isDigest);
    return rm;
}

From source file:org.squidy.nodes.laserpointer.proxy.ProxyCam.java

public void run() {

    updateConfig();/*from   ww  w.ja  v  a2  s  .  c o m*/

    while (running) {
        int bytesRead = 0;
        LOG.debug("wait for image");
        try {
            if (debug) {
                debugWriter.newLine();
                debugWriter.write("========= New image =========");
                debugWriter.newLine();
            }
            BufferedInputStream bis = new BufferedInputStream(input);

            // read 4-byte header with number of bytes and allocate byte[]
            DataInputStream dis = new DataInputStream(bis);
            int size = Integer.reverseBytes(dis.readInt());
            //System.out.println(Integer.toHexString(size));
            //System.out.println(size);

            byte[] sizebytes = new byte[4];
            sizebytes[0] = (byte) (size >> 24);
            sizebytes[1] = (byte) (size >> 16);
            sizebytes[2] = (byte) (size >> 8);
            sizebytes[3] = (byte) size;
            ProxyConfig.getConfigConnection().sendMessage(sizebytes);

            bytes = new byte[size];

            bytesRead = 0;
            do {
                bytes[bytesRead] = (byte) bis.read();
                bytesRead++;
            } while (bytesRead < size);

            LOG.debug("Total bytes read: " + bytesRead + ".");

            if (debug) {
                debugWriter.newLine();
                debugWriter.write("========= " + aoiW + "x" + aoiH + " =========");
                debugWriter.newLine();
                debugWriter.flush();
            }

        } catch (IOException e) {
            LOG.error("Connection error occured.");
            close();
            return;
        }

        try {
            ProxyConfig.getConfigConnection().sendMessage(bytes);
        } catch (IOException e) {
            LOG.error("Couldn't send image to config.");
        }
    }

    close();
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

@Override
public void read(DataInputStream in) throws IOException {
    final int version = in.readInt();
    switch (version) {
    case VERSION_INIT:
        throw new ProtocolException("Ignored upgrade");
    case VERSION_SPLIT_URI:
        authority = DurableUtils.readNullableString(in);
        documentId = DurableUtils.readNullableString(in);
        mimeType = DurableUtils.readNullableString(in);
        displayName = DurableUtils.readNullableString(in);
        lastModified = in.readLong();// w  w w  .  j  a v a2s. com
        flags = in.readInt();
        summary = DurableUtils.readNullableString(in);
        size = in.readLong();
        icon = in.readInt();
        deriveFields();
        break;
    default:
        throw new ProtocolException("Unknown version " + version);
    }
}

From source file:org.openmrs.module.odkconnector.serialization.web.PatientWebConnectorTest.java

@Test
public void serialize_shouldDisplayAllPatientInformation() throws Exception {

    // compose url
    URL u = new URL(SERVER_URL + "/module/odkconnector/download/patients.form");

    // setup http url connection
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
    connection.setDoOutput(true);/*ww w . j av  a2s  .  c om*/
    connection.setRequestMethod("POST");
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);
    connection.addRequestProperty("Content-type", "application/octet-stream");

    // write auth details to connection
    DataOutputStream outputStream = new DataOutputStream(new GZIPOutputStream(connection.getOutputStream()));
    outputStream.writeUTF("admin");
    outputStream.writeUTF("test");
    outputStream.writeBoolean(true);
    outputStream.writeInt(2);
    outputStream.writeInt(1);
    outputStream.close();

    DataInputStream inputStream = new DataInputStream(new GZIPInputStream(connection.getInputStream()));
    Integer responseStatus = inputStream.readInt();

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();

    int count = 0;
    byte[] buffer = new byte[1024];
    while ((count = inputStream.read(buffer)) > 0) {
        arrayOutputStream.write(buffer, 0, count);
    }
    arrayOutputStream.close();
    inputStream.close();

    File file = new File("/home/nribeka/connector.data");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(arrayOutputStream.toByteArray());
    fos.close();

    inputStream = new DataInputStream(new FileInputStream(file));

    if (responseStatus == HttpURLConnection.HTTP_OK) {

        // total number of patients
        Integer patientCounter = inputStream.readInt();
        System.out.println("Patient Counter: " + patientCounter);
        for (int j = 0; j < patientCounter; j++) {
            System.out.println("=================Patient=====================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Family Name: " + inputStream.readUTF());
            System.out.println("Middle Name: " + inputStream.readUTF());
            System.out.println("Last Name: " + inputStream.readUTF());
            System.out.println("Gender: " + inputStream.readUTF());
            System.out.println("Birth Date: " + inputStream.readUTF());
            System.out.println("Identifier: " + inputStream.readUTF());
            System.out.println("Patients: " + j + " out of " + patientCounter);
        }

        Integer obsCounter = inputStream.readInt();
        System.out.println("Observation Counter: " + obsCounter);
        for (int j = 0; j < obsCounter; j++) {
            System.out.println("==================Observation=================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Concept Name: " + inputStream.readUTF());

            byte type = inputStream.readByte();
            if (type == ObsSerializer.TYPE_STRING)
                System.out.println("Value: " + inputStream.readUTF());
            else if (type == ObsSerializer.TYPE_INT)
                System.out.println("Value: " + inputStream.readInt());
            else if (type == ObsSerializer.TYPE_DOUBLE)
                System.out.println("Value: " + inputStream.readDouble());
            else if (type == ObsSerializer.TYPE_DATE)
                System.out.println("Value: " + inputStream.readUTF());
            System.out.println("Time: " + inputStream.readUTF());
            System.out.println("Obs: " + j + " out of: " + obsCounter);
        }
        Integer formCounter = inputStream.readInt();
        System.out.println("Form Counter: " + formCounter);
        for (int j = 0; j < formCounter; j++) {
            System.out.println("==================Observation=================");
            System.out.println("Patient Id: " + inputStream.readInt());
            System.out.println("Concept Name: " + inputStream.readUTF());

            byte type = inputStream.readByte();
            if (type == ObsSerializer.TYPE_STRING)
                System.out.println("Value: " + inputStream.readUTF());
            else if (type == ObsSerializer.TYPE_INT)
                System.out.println("Value: " + inputStream.readInt());
            else if (type == ObsSerializer.TYPE_DOUBLE)
                System.out.println("Value: " + inputStream.readDouble());
            else if (type == ObsSerializer.TYPE_DATE)
                System.out.println("Value: " + inputStream.readUTF());
            System.out.println("Time: " + inputStream.readUTF());
            System.out.println("Form: " + j + " out of: " + formCounter);
        }
    }
    inputStream.close();
}

From source file:com.vimukti.accounter.license.LicenseManager.java

private byte[] checkAndGetLicenseText(String licenseContent) {
    byte[] licenseText;
    try {/*from w  ww . jav a  2s  . c o  m*/
        byte[] decodedBytes = Base64.decodeBase64(licenseContent.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
        DataInputStream dIn = new DataInputStream(in);
        int textLength = dIn.readInt();
        licenseText = new byte[textLength];
        dIn.read(licenseText);
        byte[] hash = new byte[dIn.available()];
        dIn.read(hash);
        try {
            Signature signature = Signature.getInstance("SHA1withDSA");
            signature.initVerify(PUBLIC_KEY);
            signature.update(licenseText);
            if (!signature.verify(hash)) {
                throw new LicenseException("Failed to verify the license.");
            }

        } catch (InvalidKeyException e) {
            throw new LicenseException(e);
        } catch (SignatureException e) {
            throw new LicenseException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new LicenseException(e);
        }

    } catch (IOException e) {
        throw new LicenseException(e);
    }

    return licenseText;
}

From source file:com.opensoc.json.serialization.JSONKafkaSerializer.java

@SuppressWarnings("unchecked")
public JSONObject fromBytes(byte[] input) {

    ByteArrayInputStream inputBuffer = new ByteArrayInputStream(input);
    DataInputStream data = new DataInputStream(inputBuffer);

    JSONObject output = new JSONObject();

    try {/*from  ww w.j  a  v a  2  s . c  om*/
        int mapSize = data.readInt();

        for (int i = 0; i < mapSize; i++) {
            String key = (String) getObject(data);
            // System.out.println("Key Found"+ key);
            Object val = getObject(data);
            output.put(key, val);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return output;
}

From source file:runtime.daemon.ProcessLauncher.java

private String getStringFromInputStream(InputStream is) {

    DataInputStream in = new DataInputStream(is);
    int len = 0;//from   w w w  .ja v a  2  s  .c  o m
    try {
        len = in.readInt();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (DEBUG && logger.isDebugEnabled()) {
        logger.debug("Ticket length  " + len);
    }
    byte[] xml = new byte[len];
    try {

        in.readFully(xml, 0, len);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(xml);
}

From source file:org.jboss.windup.decorator.archive.JVMVersionDecorator.java

@Override
public void processMeta(ZipMetadata meta) {
    try {/*from w  w  w.  j  a  va  2  s.c o  m*/
        ZipEntry entry;
        Enumeration<?> e = meta.getZipFile().entries();
        // locate a random class entry...
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();

            if (StringUtils.endsWith(entry.getName(), ".class")) {
                String version = null;
                DataInputStream in = null;
                try {
                    in = new DataInputStream(meta.getZipFile().getInputStream(entry));

                    // ignore Java "magic" number.
                    in.readInt();
                    int minor = in.readUnsignedShort();
                    int major = in.readUnsignedShort();

                    switch (major) {
                    case ClassFile.JAVA_1:
                        version = "1.1";
                        break;
                    case ClassFile.JAVA_2:
                        version = "1.2";
                        break;
                    case ClassFile.JAVA_3:
                        version = "1.3";
                        break;
                    case ClassFile.JAVA_4:
                        version = "1.4";
                        break;
                    case ClassFile.JAVA_5:
                        version = "5.0";
                        break;
                    case ClassFile.JAVA_6:
                        version = "6.0";
                        break;
                    case ClassFile.JAVA_7:
                        version = "7.0";
                        break;
                    default:
                        LOG.warn("No version mapping for: " + version);
                    }
                    version = version + "." + minor;
                } finally {
                    in.close();
                }
                JVMBuildVersionResult vr = new JVMBuildVersionResult();
                vr.setJdkBuildVersion(version);
                meta.getDecorations().add(vr);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Built with: " + version);
                }
                break;
            }
        }
    } catch (Exception e) {
        LOG.error("Exception getting JDK version.", e);
    }
}

From source file:es.urjc.mctwp.image.impl.analyze.AnalyzeImagePlugin.java

private boolean checkFormat(File file) throws IOException {
    boolean result = false;

    if (file != null) {
        DataInputStream stream = new DataInputStream(new FileInputStream(file));
        int check = stream.readInt();
        stream.close();//  w  w w.  jav a 2  s  . c om

        result = (check == 0x15c) || (swap(check) == 0x15c);
    }

    return result;
}