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:com.facebook.infrastructure.db.CommitLogHeader.java

public CommitLogHeader deserialize(DataInputStream dis) throws IOException {
    int size = dis.readInt();
    byte[] bitFlags = new byte[size];
    dis.readFully(bitFlags);/* w w w  .ja  va2s  .c o m*/

    int[] position = new int[size];
    for (int i = 0; i < size; ++i) {
        position[i] = dis.readInt();
    }

    return new CommitLogHeader(bitFlags, position);
}

From source file:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

@Override
public boolean isZip(final InputStream inputStream) {

    boolean result = false;

    try {/*from w  w w . j  a va 2 s.c o  m*/
        final DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream));
        final int test = dataInputStream.readInt();
        dataInputStream.close();
        result = (test == 0x504b0304);
    } catch (final IOException e) {
        result = false;
    }

    return result;
}

From source file:com.atlassian.extras.decoder.v2.Version2LicenseDecoder.java

private byte[] checkAndGetLicenseText(String licenseContent) {
    try {/*from   w  ww  .java2s.c o m*/
        byte[] decodedBytes = Base64.decodeBase64(licenseContent.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
        DataInputStream dIn = new DataInputStream(in);
        int textLength = dIn.readInt();
        byte[] 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);
        }*/
        return licenseText;
    } catch (IOException e) {
        throw new LicenseException(e);
    }
}

From source file:com.alphabetbloc.accessmrs.tasks.CheckConnectivityTask.java

protected DataInputStream getServerStream() throws Exception {

    HttpPost request = new HttpPost(mServer);
    request.setEntity(new OdkAuthEntity());
    HttpResponse response = httpClient.execute(request);
    response.getStatusLine().getStatusCode();
    HttpEntity responseEntity = response.getEntity();
    responseEntity.getContentLength();//  w  ww. j  av a 2 s .  c o m

    DataInputStream zdis = new DataInputStream(new GZIPInputStream(responseEntity.getContent()));

    int status = zdis.readInt();
    if (status == HttpURLConnection.HTTP_UNAUTHORIZED) {
        zdis.close();
        throw new IOException("Access denied. Check your username and password.");
    } else if (status <= 0 || status >= HttpURLConnection.HTTP_BAD_REQUEST) {
        zdis.close();
        throw new IOException("Connection Failed. Please Try Again.");
    } else {
        assert (status == HttpURLConnection.HTTP_OK); // success
        return zdis;
    }
}

From source file:org.nuxeo.osgi.BundleIdGenerator.java

public synchronized void load(File file) {
    DataInputStream in = null;
    try {/*from  w ww  .j  a va2 s  .c om*/
        in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        count = in.readLong();
        int size = in.readInt();
        for (int i = 0; i < size; i++) {
            String key = in.readUTF();
            long id = in.readLong();
            ids.put(key, id);
        }
    } catch (FileNotFoundException e) {
        // do nothing - this is the first time the runtime is started
    } catch (IOException e) {
        // may be the file is corrupted
        file.delete();
        log.error("The bundle.ids file is corrupted. reseting bundle ids.");
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.ListSerializerTest.java

@Test
public void serialize_shouldSerializePatientInformation() throws Exception {
    File file = File.createTempFile("PatientSerialization", "Example");
    GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

    log.info("Writing to: " + file.getAbsolutePath());

    Cohort cohort = new Cohort();
    cohort.addMember(6);//from  w ww.  j a  va  2s  .  c o  m
    cohort.addMember(7);
    cohort.addMember(8);

    List<Patient> patients = new ArrayList<Patient>();
    List<Obs> observations = new ArrayList<Obs>();
    List<Form> forms = new ArrayList<Form>();

    for (Integer patientId : cohort.getMemberIds()) {
        Patient patient = Context.getPatientService().getPatient(patientId);
        observations.addAll(Context.getObsService().getObservationsByPerson(patient));
        patients.add(patient);
    }

    Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class);
    serializer.write(outputStream, patients);
    serializer.write(outputStream, observations);
    serializer.write(outputStream, forms);

    outputStream.close();

    GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    DataInputStream dataInputStream = new DataInputStream(inputStream);

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

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

        byte type = dataInputStream.readByte();
        if (type == ObsSerializer.TYPE_STRING)
            System.out.println("Value: " + dataInputStream.readUTF());
        else if (type == ObsSerializer.TYPE_INT)
            System.out.println("Value: " + dataInputStream.readInt());
        else if (type == ObsSerializer.TYPE_DOUBLE)
            System.out.println("Value: " + dataInputStream.readDouble());
        else if (type == ObsSerializer.TYPE_DATE)
            System.out.println("Value: " + dataInputStream.readLong());
        System.out.println("Time: " + dataInputStream.readLong());
    }

    Integer formCounter = dataInputStream.readInt();
    for (int j = 0; j < formCounter; j++) {
        System.out.println("==================Form=================");
        System.out.println("Form Id: " + dataInputStream.readInt());
    }

    System.out.println();

    inputStream.close();
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.PatientSerializerTest.java

@Test
public void serialize_shouldSerializePatientInformation() throws Exception {
    File file = File.createTempFile("PatientSerialization", "Example");
    GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

    log.info("Writing to: " + file.getAbsolutePath());

    Cohort cohort = new Cohort();
    cohort.addMember(6);//from w  w w  .  j a  va 2  s . co m
    cohort.addMember(7);
    cohort.addMember(8);

    List<Patient> patients = new ArrayList<Patient>();
    List<Obs> observations = new ArrayList<Obs>();
    List<Form> forms = new ArrayList<Form>();

    for (Integer patientId : cohort.getMemberIds()) {
        Patient patient = Context.getPatientService().getPatient(patientId);
        observations.addAll(Context.getObsService().getObservationsByPerson(patient));
        patients.add(patient);
    }

    Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class);
    serializer.write(outputStream, patients);
    serializer.write(outputStream, observations);
    serializer.write(outputStream, forms);

    outputStream.close();

    GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    DataInputStream dataInputStream = new DataInputStream(inputStream);

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

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

        byte type = dataInputStream.readByte();
        if (type == ObsSerializer.TYPE_STRING)
            System.out.println("Value: " + dataInputStream.readUTF());
        else if (type == ObsSerializer.TYPE_INT)
            System.out.println("Value: " + dataInputStream.readInt());
        else if (type == ObsSerializer.TYPE_DOUBLE)
            System.out.println("Value: " + dataInputStream.readDouble());
        else if (type == ObsSerializer.TYPE_DATE)
            System.out.println("Value: " + dataInputStream.readUTF());
        System.out.println("Time: " + dataInputStream.readUTF());
    }

    Integer formCounter = dataInputStream.readInt();
    for (int j = 0; j < formCounter; j++) {
        System.out.println("==================Form=================");
        System.out.println("Form Id: " + dataInputStream.readInt());
    }

    System.out.println();

    inputStream.close();
}

From source file:CoolBarExamples.java

private void setState(CoolBar coolBar, File file) throws IOException {
    if (!file.exists())
        throw new IOException("File does not exist: " + file);

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

    try {//from  w w w. ja  v a 2s  . c o  m
        // Order
        int size = in.readInt();
        int[] order = new int[size];
        for (int i = 0; i < order.length; i++)
            order[i] = in.readInt();

        // Wrap indices.
        size = in.readInt();
        int[] wrapIndices = new int[size];
        for (int i = 0; i < wrapIndices.length; i++)
            wrapIndices[i] = in.readInt();

        // Sizes.
        size = in.readInt();
        Point[] sizes = new Point[size];
        for (int i = 0; i < sizes.length; i++)
            sizes[i] = new Point(in.readInt(), in.readInt());

        coolBar.setItemLayout(order, wrapIndices, sizes);
    } finally {
        in.close();
    }

}

From source file:Version2LicenseDecoder.java

private byte[] checkAndGetLicenseText(String licenseContent) {
    try {/*from   w  ww.  java2  s .  c o m*/
        byte[] e = Base64.decodeBase64(licenseContent.getBytes());
        ByteArrayInputStream in = new ByteArrayInputStream(e);
        DataInputStream dIn = new DataInputStream(in);
        int textLength = dIn.readInt();
        byte[] licenseText = new byte[textLength];
        dIn.read(licenseText);
        byte[] hash = new byte[dIn.available()];
        dIn.read(hash);

        try {
            Signature e1 = Signature.getInstance("SHA1withDSA");
            e1.initVerify(PUBLIC_KEY);
            e1.update(licenseText);
            if (!e1.verify(hash)) {
                throw new LicenseException("Failed to verify the license.");
            } else {
                return licenseText;
            }
        } catch (InvalidKeyException var9) {
            throw new LicenseException(var9);
        } catch (SignatureException var10) {
            throw new LicenseException(var10);
        } catch (NoSuchAlgorithmException var11) {
            throw new LicenseException(var11);
        }
    } catch (IOException var12) {
        throw new LicenseException(var12);
    }
}

From source file:com.facebook.infrastructure.db.Row.java

public Row deserialize(DataInputStream dis) throws IOException {
    String key = dis.readUTF();// w w  w .  j  a  va2s .c  o  m
    Row row = new Row(key);
    int size = dis.readInt();

    if (size > 0) {
        for (int i = 0; i < size; ++i) {
            ColumnFamily cf = ColumnFamily.serializer().deserialize(dis);
            row.addColumnFamily(cf);
        }
    }
    return row;
}