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:eu.learnpad.simulator.mon.utils.Manager.java

/**
 * It reads text from file and provides it on string
 *     * //  w ww .j  a  v  a 2s  . c o m
 * @param filePath the file to read path
 * @return a String containing all the file text
 */
@SuppressWarnings("deprecation")
public static String ReadTextFromFile(String filePath) {
    File file = new File(filePath);
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    StringBuilder strB = new StringBuilder();

    try {
        fis = new FileInputStream(file);

        // Here BufferedInputStream is added for fast reading.
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        while (dis.available() != 0) {
            // this statement reads the line from the file and print it to
            // the console.
            strB.append(dis.readLine());

        }
        // dispose all the resources after using them.
        fis.close();
        bis.close();
        dis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return strB.toString();
}

From source file:org.kse.crypto.filetype.CryptoFileUtil.java

/**
 * Detect the KeyStore type contained in the supplied file.
 *
 * @param is//w  w  w .java  2  s. c  o m
 *            Input stream to detect type for
 * @return KeyStore type or null if none matched
 * @throws IOException
 *             If an I/O problem occurred
 */
public static KeyStoreType detectKeyStoreType(InputStream is) throws IOException {
    byte[] contents = ReadUtil.readFully(is);

    DataInputStream dis = null;

    try {
        dis = new DataInputStream(new ByteArrayInputStream(contents));

        // If less than 4 bytes are available it isn't a KeyStore
        if (dis.available() < 4) {
            return null;
        }

        // Read first integer (4 bytes)
        int i1 = dis.readInt();

        // Test for JKS - starts with appropriate magic number
        if (i1 == JKS_MAGIC_NUMBER) {
            return JKS;
        }

        // Test for JCEKS - starts with appropriate magic number
        if (i1 == JCEKS_MAGIC_NUMBER) {
            return JCEKS;
        }

        // Test for BKS and UBER

        // Both start with a version number of 0, 1 or 2
        if ((i1 == 0) || (i1 == 1) || (i1 == 2)) {
            /*
             * For BKS and UBER the last 20 bytes of the file are the SHA-1
             * Hash while the byte before that is a ASN1Null (0) indicating
             * the end of the store. UBER, however, encrypts the store
             * content making it highly unlikely that the ASN1Null end byte
             * will be preserved. Therefore if the 21st byte from the end of
             * the file is a ASN1Null then the KeyStore is BKS
             */

            if (contents.length < 26) {
                // Insufficient bytes to be BKS or UBER
                return null;
            }

            // Skip to 21st from last byte (file length minus 21 and the 4 bytes already read)
            dis.skip(contents.length - 25);

            // Read what may be the null byte
            if (dis.readByte() == 0) {
                // Found null byte - BKS/BKS-V1
                if (i1 == 1) {
                    return BKS_V1;
                } else {
                    return BKS;
                }
            } else {
                // No null byte - UBER
                return UBER;
            }
        }
    } finally {
        IOUtils.closeQuietly(dis);
    }

    // @formatter:off
    /*
     * Test for PKCS #12. ASN.1 should look like this:
     *
     * PFX ::= ASN1Sequence { version ASN1Integer {v3(3)}(v3,...), authSafe
     * ContentInfo, macData MacData OPTIONAL
     */
    // @formatter:on

    ASN1Primitive pfx = null;
    try {
        pfx = ASN1Primitive.fromByteArray(contents);
    } catch (IOException e) {
        // if it cannot be parsed as ASN1, it is certainly not a pfx key store
        return null;
    }

    // Is a sequence...
    if ((pfx != null) && (pfx instanceof ASN1Sequence)) {
        // Has two or three components...
        ASN1Sequence sequence = (ASN1Sequence) pfx;

        if ((sequence.size() == 2) || (sequence.size() == 3)) {
            // ...the first of which is a version of 3
            ASN1Encodable firstComponent = sequence.getObjectAt(0);

            if (firstComponent instanceof ASN1Integer) {
                ASN1Integer version = (ASN1Integer) firstComponent;

                if (version.getValue().intValue() == 3) {
                    return PKCS12;
                }
            }
        }
    }

    // KeyStore type not recognised
    return null;
}

From source file:net.sf.keystore_explorer.crypto.filetype.CryptoFileUtil.java

/**
 * Detect the KeyStore type contained in the supplied file.
 *
 * @param is//from  ww  w  . ja  va 2s. c  o  m
 *            Input stream to detect type for
 * @return KeyStore type or null if none matched
 * @throws IOException
 *             If an I/O problem occurred
 */
public static KeyStoreType detectKeyStoreType(InputStream is) throws IOException {
    byte[] contents = ReadUtil.readFully(is);

    DataInputStream dis = null;

    try {
        dis = new DataInputStream(new ByteArrayInputStream(contents));

        // If less than 4 bytes are available it isn't a KeyStore
        if (dis.available() < 4) {
            return null;
        }

        // Read first integer (4 bytes)
        int i1 = dis.readInt();

        // Test for JKS - starts with appropriate magic number
        if (i1 == JKS_MAGIC_NUMBER) {
            return JKS;
        }

        if (i1 == HTKS_MAGIC_NUMBER) {
            return HTKS;
        }

        // Test for JCEKS - starts with appropriate magic number
        if (i1 == JCEKS_MAGIC_NUMBER) {
            return JCEKS;
        }

        // Test for BKS and UBER

        // Both start with a version number of 0, 1 or 2
        if ((i1 == 0) || (i1 == 1) || (i1 == 2)) {
            /*
             * For BKS and UBER the last 20 bytes of the file are the SHA-1
             * Hash while the byte before that is a ASN1Null (0) indicating
             * the end of the store. UBER, however, encrypts the store
             * content making it highly unlikely that the ASN1Null end byte
             * will be preserved. Therefore if the 21st byte from the end of
             * the file is a ASN1Null then the KeyStore is BKS
             */

            if (contents.length < 26) {
                // Insufficient bytes to be BKS or UBER
                return null;
            }

            // Skip to 21st from last byte (file length minus 21 and the 4 bytes already read)
            dis.skip(contents.length - 25);

            // Read what may be the null byte
            if (dis.readByte() == 0) {
                // Found null byte - BKS/BKS-V1
                if (i1 == 1) {
                    return BKS_V1;
                } else {
                    return BKS;
                }
            } else {
                // No null byte - UBER
                return UBER;
            }
        }
    } finally {
        IOUtils.closeQuietly(dis);
    }

    // @formatter:off
    /*
     * Test for PKCS #12. ASN.1 should look like this:
     *
     * PFX ::= ASN1Sequence { version ASN1Integer {v3(3)}(v3,...), authSafe
     * ContentInfo, macData MacData OPTIONAL
     */
    // @formatter:on

    ASN1Primitive pfx = null;
    try {
        pfx = ASN1Primitive.fromByteArray(contents);
    } catch (IOException e) {
        // if it cannot be parsed as ASN1, it is certainly not a pfx key store
        return null;
    }

    // Is a sequence...
    if ((pfx != null) && (pfx instanceof ASN1Sequence)) {
        // Has two or three components...
        ASN1Sequence sequence = (ASN1Sequence) pfx;

        if ((sequence.size() == 2) || (sequence.size() == 3)) {
            // ...the first of which is a version of 3
            ASN1Encodable firstComponent = sequence.getObjectAt(0);

            if (firstComponent instanceof ASN1Integer) {
                ASN1Integer version = (ASN1Integer) firstComponent;

                if (version.getValue().intValue() == 3) {
                    return PKCS12;
                }
            }
        }
    }

    // KeyStore type not recognised
    return null;
}

From source file:nl.dreamkernel.s4.tweaker.util.RuntimeExec.java

public static String[] execute(String[] commands, boolean needResponce) {
    try {// w  w  w . j  a  va 2  s .  com
        Process process = Runtime.getRuntime().exec(commands);
        if (needResponce) {
            DataInputStream inputStream = new DataInputStream(process.getInputStream());
            if (inputStream != null) {
                String ret = "";
                int size = 0;
                byte[] buffer = new byte[1024];
                try {
                    do {
                        size = inputStream.read(buffer);
                        if (size > 0) {
                            ret += new String(buffer, 0, size, HTTP.UTF_8);
                        }
                    } while (inputStream.available() > 0);
                } catch (IOException e) {
                }
                return ret.split("\n");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.commoncrawl.service.queryserver.query.InverseLinksByDomainQuery.java

static void collectAllTopLevelDomainRecordsByDomain(FileSystem fs, Configuration conf, long databaseId,
        long targetRootDomainFP, FileSystem outputFileSystem, Path finalOutputPath) throws IOException {

    File tempFile = new File("/tmp/inverseLinksReport-" + System.currentTimeMillis());
    tempFile.mkdir();/*from  w  w  w.ja  va2s . c om*/

    try {
        // create the final output spill writer ...  
        SequenceFileSpillWriter<FlexBuffer, URLFPV2> spillwriter = new SequenceFileSpillWriter<FlexBuffer, URLFPV2>(
                outputFileSystem, conf, finalOutputPath, FlexBuffer.class, URLFPV2.class,
                new PositionBasedSequenceFileIndex.PositionBasedIndexWriter(outputFileSystem,
                        PositionBasedSequenceFileIndex.getIndexNameFromBaseName(finalOutputPath)),
                true);

        try {

            MergeSortSpillWriter<FlexBuffer, URLFPV2> finalMerger = new MergeSortSpillWriter<FlexBuffer, URLFPV2>(
                    conf, spillwriter, FileSystem.getLocal(conf), new Path(tempFile.getAbsolutePath()), null,
                    new ComplexKeyComparator(), FlexBuffer.class, URLFPV2.class, true, null);

            try {

                for (int targetShardId = 0; targetShardId < CrawlEnvironment.NUM_DB_SHARDS; ++targetShardId) {
                    // 0. shard domain id to find index file location ... 
                    int indexShardId = (int) ((targetRootDomainFP & Integer.MAX_VALUE)
                            % CrawlEnvironment.NUM_DB_SHARDS);
                    // build path to index file 
                    Path indexFilePath = new Path("crawl/inverseLinkDB_ByDomain/" + databaseId
                            + "/phase3Data/part-" + NUMBER_FORMAT.format(indexShardId));
                    LOG.info("rootDomain is:" + targetRootDomainFP + " ShardId:" + indexShardId + " Index Path:"
                            + indexFilePath);
                    // 1. scan domainFP to index file first
                    // 2. given index, scan index->pos file to find scan start position
                    // 3. given scan start position, scan forward until fp match is found.
                    // 4. collect all matching entries and output to a file ? 

                    FSDataInputStream indexDataInputStream = fs.open(indexFilePath);
                    try {
                        TFile.Reader reader = new TFile.Reader(indexDataInputStream,
                                fs.getFileStatus(indexFilePath).getLen(), conf);
                        try {
                            TFile.Reader.Scanner scanner = reader.createScanner();

                            try {
                                // generate key ... 
                                DataOutputBuffer keyBuffer = new DataOutputBuffer();
                                keyBuffer.writeLong(targetRootDomainFP);
                                if (scanner.seekTo(keyBuffer.getData(), 0, keyBuffer.getLength())) {
                                    // setup for value scan 
                                    DataInputStream valueStream = scanner.entry().getValueStream();
                                    int dataOffsetOut = -1;
                                    while (valueStream.available() > 0) {
                                        // read entries looking for our specific entry
                                        int shardIdx = valueStream.readInt();
                                        int dataOffset = valueStream.readInt();
                                        if (shardIdx == targetShardId) {
                                            dataOffsetOut = dataOffset;
                                            break;
                                        }
                                    }
                                    LOG.info("Index Search Yielded:" + dataOffsetOut);
                                    if (dataOffsetOut != -1) {
                                        // ok create a data path 
                                        Path finalDataPath = new Path("crawl/inverseLinkDB_ByDomain/"
                                                + databaseId + "/phase2Data/data-"
                                                + NUMBER_FORMAT.format(targetShardId));
                                        Path finalDataIndexPath = new Path("crawl/inverseLinkDB_ByDomain/"
                                                + databaseId + "/phase2Data/data-"
                                                + NUMBER_FORMAT.format(targetShardId) + ".index");
                                        // check to see if index is already loaded ... 
                                        PositionBasedSequenceFileIndex<FlexBuffer, TextBytes> index = null;
                                        synchronized (_shardToIndexMap) {
                                            index = _shardToIndexMap.get(targetShardId);
                                        }
                                        if (index == null) {
                                            LOG.info("Loading Index from Path:" + finalDataIndexPath);
                                            // load index
                                            index = new PositionBasedSequenceFileIndex<FlexBuffer, TextBytes>(
                                                    fs, finalDataIndexPath, FlexBuffer.class, TextBytes.class);
                                            // put in cache
                                            synchronized (_shardToIndexMap) {
                                                _shardToIndexMap.put(targetShardId, index);
                                            }
                                        }

                                        LOG.info("Initializing Data Reader at Path:" + finalDataPath);
                                        // ok time to create a reader 
                                        SequenceFile.Reader dataReader = new SequenceFile.Reader(fs,
                                                finalDataPath, conf);

                                        try {
                                            LOG.info("Seeking Reader to Index Position:" + dataOffsetOut);
                                            index.seekReaderToItemAtIndex(dataReader, dataOffsetOut);

                                            FlexBuffer keyBytes = new FlexBuffer();
                                            URLFPV2 sourceFP = new URLFPV2();
                                            DataInputBuffer keyReader = new DataInputBuffer();
                                            TextBytes urlTxt = new TextBytes();

                                            // ok read to go ... 
                                            while (dataReader.next(keyBytes, sourceFP)) {
                                                // initialize reader 
                                                keyReader.reset(keyBytes.get(), keyBytes.getOffset(),
                                                        keyBytes.getCount());

                                                long targetFP = keyReader.readLong();

                                                if (targetRootDomainFP == targetFP) {
                                                    finalMerger.spillRecord(keyBytes, sourceFP);
                                                } else {
                                                    LOG.info("FP:" + targetFP + " > TargetFP:"
                                                            + targetRootDomainFP + " Exiting Iteration Loop");
                                                    break;
                                                }
                                            }
                                        } finally {
                                            LOG.info("Closing Reader");
                                            dataReader.close();
                                        }
                                    }
                                }
                            } finally {
                                LOG.info("Closing Scanner");
                                scanner.close();
                            }

                        } finally {
                            LOG.info("Closing TFile Reader");
                            reader.close();
                        }
                    } finally {
                        LOG.info("Closing InputStream");
                        indexDataInputStream.close();
                    }
                }
            } finally {
                finalMerger.close();
            }
        } finally {
            spillwriter.close();
        }
    } catch (IOException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        FileUtils.recursivelyDeleteFile(tempFile);
    }

}

From source file:eu.learnpad.simulator.mon.utils.Manager.java

@SuppressWarnings("deprecation")
public static Properties Read(String fileName) {
    Properties readedProps = new Properties();

    File file = new File(fileName);
    FileInputStream fis = null;//from   w ww. j a v a  2 s. c o  m
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
        fis = new FileInputStream(file);

        // Here BufferedInputStream is added for fast reading.
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        // dis.available() returns 0 if the file does not have more lines.
        String property = "";
        String key = "";
        String value = "";

        while (dis.available() != 0) {
            // this statement reads the line from the file and print it to
            // the console.
            property = dis.readLine().trim();
            if (property.length() > 0) {
                key = property.substring(0, property.indexOf("="));
                value = property.substring(property.indexOf("=") + 1, property.length());

                readedProps.put(key.trim(), value.trim());
            }
        }

        // dispose all the resources after using them.
        fis.close();
        bis.close();
        dis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return readedProps;
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java

/**
 * @return A 1 byte character string. A string object is stored as an INT (to store the string length) followed by
 *         the CHAR//from  w ww  .  jav  a  2s.  c  o  m
 *         array (to store the string contents).
 */
private static String readString(DataInputStream str) throws IOException {
    int fieldLength = readIntBigEndian(str);
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        buf.append(new String(new byte[] { str.readByte() }));
    }
    return buf.toString();
}

From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java

/**
 * The data is stored as a int, then the array of bytes.
 *//*from  ww w. j  av a  2 s.c om*/
private static byte[] readBytes(DataInputStream str) throws IOException {
    int fieldLength = readIntBigEndian(str);

    byte[] result = new byte[fieldLength];
    for (int i = 0; i < fieldLength; i++) {
        if (str.available() == 0)
            throw new IOException("Reached end of file without string end");
        result[i] = str.readByte();
    }

    return result;
}

From source file:Main.java

public static String runCommand(String[] commands) {
    DataOutputStream outStream = null;
    DataInputStream responseStream;
    try {/* w w w .  j  av a2s.  c om*/
        ArrayList<String> logs = new ArrayList<String>();
        Process process = Runtime.getRuntime().exec("su");
        Log.i(TAG, "Executed su");
        outStream = new DataOutputStream(process.getOutputStream());
        responseStream = new DataInputStream(process.getInputStream());

        for (String single : commands) {
            Log.i(TAG, "Command = " + single);
            outStream.writeBytes(single + "\n");
            outStream.flush();
            if (responseStream.available() > 0) {
                Log.i(TAG, "Reading response");
                logs.add(responseStream.readLine());
                Log.i(TAG, "Read response");
            } else {
                Log.i(TAG, "No response available");
            }
        }
        outStream.writeBytes("exit\n");
        outStream.flush();
        String log = "";
        for (int i = 0; i < logs.size(); i++) {
            log += logs.get(i) + "\n";
        }
        Log.i(TAG, "Execution compeleted");
        return log;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}

From source file:org.orbisgis.commons.utils.FileUtils.java

/**
 * Retrieve the content of the Inputstream as an array of bytes.
 * @param fis//from  www  .  j  av a2s  . c o  m
 * @return
 * @throws IOException
 * @deprecated use {@link org.apache.commons.io.IOUtils#toByteArray(java.io.InputStream) }
 */
@Deprecated
public static byte[] getContent(InputStream fis) throws IOException {
    DataInputStream dis = null;
    byte[] buffer;
    try {
        dis = new DataInputStream(fis);
        buffer = new byte[dis.available()];
        dis.readFully(buffer);

    } finally {
        if (dis != null) {
            dis.close();
        }
    }
    return buffer;
}