Example usage for java.io DataInputStream available

List of usage examples for java.io DataInputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Usage

From source file:com.fabernovel.alertevoirie.data.CategoryProvider.java

@Override
public boolean onCreate() {
    // load json data
    DataInputStream in = new DataInputStream(getContext().getResources().openRawResource(R.raw.categories));
    try {/*from  www. j a  v a2  s  .c om*/
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        categories = (JSONObject) new JSONTokener(new String(buffer)).nextValue();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        Log.e("Alerte Voirie", "JSON error", e);
    }
    return true;
}

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

/**
 * Here we need to get the column and apply the filter.
 *//* w  w  w  . jav a 2s  .  com*/
public IColumn deserialize(DataInputStream dis, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;

    String name = dis.readUTF();
    IColumn column = new Column(name);
    column = filter.filter(column, dis);
    if (column != null) {
        column = defreeze(dis, name);
    } else {
        /* Skip a boolean and the timestamp */
        dis.skip(DBConstants.boolSize_ + DBConstants.tsSize_);
        int size = dis.readInt();
        dis.skip(size);
    }
    return column;
}

From source file:com.qut.middleware.spep.authn.bindings.impl.AuthnPostBindingImpl.java

public AuthnPostBindingImpl() {
    try {/*  www.jav a 2s  .c  o m*/
        InputStream inputStream = this.getClass().getResourceAsStream("samlPostRequestTemplate.html");
        DataInputStream dataInputStream = new DataInputStream(inputStream);

        byte[] document = new byte[dataInputStream.available()];
        dataInputStream.readFully(document);

        // Load the document as UTF-8 format and create a formatter for it.
        String samlResponseTemplate = new String(document, "UTF-8");
        this.samlMessageFormat = new MessageFormat(samlResponseTemplate);

        this.logger.info("Created AuthnPostBindingImpl successfully");
    } catch (IOException e) {
        throw new IllegalArgumentException("HTTP POST binding form could not be loaded due to an I/O error", e);
    }

}

From source file:pcap.application.PcapApplicationTest.java

/**
 * Test PcapApplication with little endian on entrency.
 * @throws java.lang.Exception/*from w  w  w .ja  va 2 s . c  o  m*/
 */
@Test
public void testPcapApplicationLittleEndian() throws Exception {
    System.out.println("Test resulting pcap file with little endian");
    String pcapName = "testPcap" + PCAP_EX;
    String resultingPcapName = "test" + PCAP_EX;

    File pcap = createTempPCapLittleEndian(pcapName);

    final File resultingPcap = tempFolder.newFile(resultingPcapName);

    String[] myArgs = { OPTION_F, pcap.getAbsolutePath(), resultingPcap.getAbsolutePath() };

    PcapCommandLineParser parser = new PcapCommandLineParser(myArgs);

    ApplicationHandler handler = new ApplicationHandler(parser.getOutFile());
    PCapImportTask importTask = PCapHelper.createImportTask(parser.getInFile(), handler);
    importTask.init();

    while (!importTask.isFinished()) {
        importTask.processNext();
    }

    long[] expBin = { 0xa1b2c3d400020004L, 0x0000000000000000L, 0x0000ffff00000001L, 0x42c23479000871cdL,
            0x000000080000004bL, 0x1234ffff0000121aL };

    DataInputStream inputStream = new DataInputStream(new FileInputStream(resultingPcap));
    int count = inputStream.available() / 8;
    long[] b = new long[count];

    for (int i = 0; i < count; i++) {
        b[i] = inputStream.readLong();
    }

    assertTrue(Arrays.equals(b, expBin));

}

From source file:edu.cornell.med.icb.goby.compression.MessageChunksReader.java

private boolean confirmDelimiter(DataInputStream in) throws IOException {

    for (int i = 0; i < MessageChunksWriter.DELIMITER_LENGTH; i++) {
        if (in.available() == 0) {
            return false;
        }/*from   ww w . j a  va 2 s . c o  m*/
        final byte b = in.readByte();
        bytesRead += 1;
        if (b != MessageChunksWriter.DELIMITER_CONTENT) {
            return false;
        }
    }

    return true;
}

From source file:pcap.application.PcapApplicationTest.java

/**
 * Test PcapApplication with big endian on entrency.
 * @throws Exception //w w  w .  j  av  a2 s.  c o m
 */
@Test
public void testPcapApplicationBigEndian() throws Exception {
    System.out.println("Test resulting pcap file with big endian");
    String pcapName = "testPcap" + PCAP_EX;
    String resultingPcapName = "test" + PCAP_EX;

    File pcap = createTempPCapBigEndian(pcapName);

    final File resultingPcap = tempFolder.newFile(resultingPcapName);

    String[] myArgs = { OPTION_F, pcap.getAbsolutePath(), resultingPcap.getAbsolutePath() };

    PcapCommandLineParser parser = new PcapCommandLineParser(myArgs);

    ApplicationHandler handler = new ApplicationHandler(parser.getOutFile());
    PCapImportTask importTask = PCapHelper.createImportTask(parser.getInFile(), handler);
    importTask.init();

    while (!importTask.isFinished()) {
        importTask.processNext();
    }

    long[] expBin = { 0xa1b2c3d400020004L, 0x0000000000000000L, 0x0000ffff00000001L, 0x42c23479000871cdL,
            0x000000080000004bL, 0x1234ffff0000121aL };

    DataInputStream inputStream = new DataInputStream(new FileInputStream(resultingPcap));
    int count = inputStream.available() / 8;
    long[] b = new long[count];

    for (int i = 0; i < count; i++) {
        b[i] = inputStream.readLong();
        System.out.println(Long.toHexString(b[i]));
    }

    assertTrue(Arrays.equals(b, expBin));

}

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

/**
 * We know the name of the column here so just return it.
 * Filter is pretty much useless in this call and is ignored.
 *//* ww w. j a v a 2s .com*/
public IColumn deserialize(DataInputStream dis, String columnName, IFilter filter) throws IOException {
    if (dis.available() == 0)
        return null;
    IColumn column = null;
    String name = dis.readUTF();
    if (name.equals(columnName)) {
        column = defreeze(dis, name);
        if (filter instanceof IdentityFilter) {
            /*
             * If this is being called with identity filter
             * since a column name is passed in we know
             * that this is a final call 
             * Hence if the column is found set the filter to done 
             * so that we do not look for the column in further files
             */
            IdentityFilter f = (IdentityFilter) filter;
            f.setDone();
        }
    } else {
        /* Skip a boolean and the timestamp */
        dis.skip(DBConstants.boolSize_ + DBConstants.tsSize_);
        int size = dis.readInt();
        dis.skip(size);
    }
    return column;
}

From source file:edu.cornell.med.icb.goby.alignments.perms.PermutationReader.java

private void makeIndex(FastBufferedInputStream inputStream) throws IOException {
    input.position(0);/*from   www . j  av a 2s . c o m*/
    final ObjectArrayList<Block> blocks = new ObjectArrayList<Block>();

    final DataInputStream dataInput = new DataInputStream(
            new FastBufferedInputStream(new FileInputStream(basename + ".perm")));
    try {
        long offset = 0;

        while (dataInput.available() > 0) {

            final Block block = new Block();
            block.offset = offset;
            block.n = dataInput.readInt();
            block.firstSmallIndex = dataInput.readInt();
            dataInput.skip(block.n * 4L);
            blocks.add(block);
            offset += block.n * 4L + 8L;
        }
        Collections.sort(blocks, SMALL_INDEX_COMPARATOR);
        indexBlocks = blocks.toArray(new Block[blocks.size()]);
    } finally {
        dataInput.close();
    }
}

From source file:Decoder.java

private byte[] checkAndGetLicenseText(String licenseContent) throws Exception {
    byte[] licenseText;
    try {/*from   w ww .j  a v a  2 s .  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 Exception("Failed to verify the license.");
            }

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

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

    return licenseText;
}

From source file:ZipExploder.java

/** Read all the bytes in a ZIPed file */
protected byte[] readAllBytes(DataInputStream is) throws IOException {
    byte[] bytes = new byte[0];
    for (int len = is.available(); len > 0; len = is.available()) {
        byte[] xbytes = new byte[len];
        int count = is.read(xbytes);
        if (count > 0) {
            byte[] nbytes = new byte[bytes.length + count];
            System.arraycopy(bytes, 0, nbytes, 0, bytes.length);
            System.arraycopy(xbytes, 0, nbytes, bytes.length, count);
            bytes = nbytes;/*from w  w  w.  j  a  v a2s  .  com*/
        } else if (count < 0) {
            // accommodate apparent bug in IBM JVM where
            // available() always returns positive value on some files
            break;
        }
    }
    return bytes;
}