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.aliyun.odps.ogg.handler.datahub.DatahubHandler.java

public static int getSkipSendTimes(String fileName, OggAlarm oggAlarm) {
    File handlerInfoFile = new File(fileName);
    if (handlerInfoFile.exists() && !handlerInfoFile.isDirectory()) {
        DataInputStream in = null;
        try {//  ww w  .j a v  a  2  s  .  co m
            in = new DataInputStream(new FileInputStream(handlerInfoFile));
            return in.readInt();
        } catch (IOException e) {
            logger.warn("Error reading handler info file, may cause duplication ", e);
            oggAlarm.warn("Error reading handler info file, may cause duplication ", e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    logger.warn("Close handler info file failed. ", e);
                    oggAlarm.warn("Close handler info file failed. ", e);
                }
            }
        }
    }
    return 0;
}

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

/**
 * Detect the KeyStore type contained in the supplied file.
 *
 * @param is//from  w ww.  java 2s .  co 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:r.base.Connections.java

public static byte[] decompress3(byte buffer[]) throws IOException, DataFormatException {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
    int outlen = in.readInt();
    byte type = in.readByte();

    if (type == 'Z') {
        byte[] properties = Arrays.copyOfRange(buffer, 5, 10);
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer, 10, buffer.length - 5);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(outlen);
        LzmaDecoder decoder = new LzmaDecoder();

        decoder.SetDecoderProperties(properties);
        if (!decoder.Code(bais, baos, outlen)) {
            throw new IOException("LZMA decompression error");
        }//from  w  w  w  . j  av  a2 s  .c om

        return baos.toByteArray();
    }

    throw new EvalException("decompres3: type = " + (char) type);
    //
    // if (type == 'Z') {
    // lzma_stream strm = LZMA_STREAM_INIT;
    // lzma_ret ret;
    // init_filters();
    // ret = lzma_raw_decoder(&strm, filters);
    // if (ret != LZMA_OK) error("internal error %d in R_decompress3", ret);
    // strm.next_in = p + 5;
    // strm.avail_in = inlen - 5;
    // strm.next_out = buf;
    // strm.avail_out = outlen;
    // ret = lzma_code(&strm, LZMA_RUN);
    // if (ret != LZMA_OK && (strm.avail_in > 0))
    // error("internal error %d in R_decompress3 %d",
    // ret, strm.avail_in);
    // lzma_end(&strm);
    // } else if (type == '2') {
    // int res;
    // res = BZ2_bzBuffToBuffDecompress((char *)buf, &outlen,
    // (char *)(p + 5), inlen - 5, 0, 0);
    // if(res != BZ_OK) error("internal error %d in R_decompress2", res);
    // } else if (type == '1') {
    // uLong outl; int res;
    // res = uncompress(buf, &outl, (Bytef *)(p + 5), inlen - 5);
    // if(res != Z_OK) error("internal error %d in R_decompress1");
    // } else if (type == '0') {
    // buf = p + 5;
    // } else error("unknown type in R_decompress3");
    //

}

From source file:com.zacwolf.commons.crypto._CRYPTOfactory.java

final public static _CRYPTOfactory getInstance(InputStream in) throws ClassNotFoundException,
        NoSuchAlgorithmException, IllegalArgumentException, SecurityException, InstantiationException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    final DataInputStream d = new DataInputStream(in);
    int start = 0;
    final int type_length = d.readInt();
    start = start + 4;//from  w w  w. j  a  va  2 s  .c  o m
    final int cipher_length = d.readInt();
    start = start + 4;
    final int salter_length = d.readInt();
    start = start + 4;
    final int key_length = d.readInt();
    start = start + 4;
    final StringBuilder type = new StringBuilder();
    final StringBuilder cipher = new StringBuilder();
    final StringBuilder salter = new StringBuilder();
    final byte[] key = new byte[key_length];
    for (int i = 0; i < type_length; i++)
        type.append((char) d.readByte());
    for (int i = 0; i < cipher_length; i++)
        cipher.append((char) d.readByte());
    for (int i = 0; i < salter_length; i++)
        salter.append((char) d.readByte());
    d.readFully(key);
    return new _CRYPTOfactory(
            (Crypter) Class.forName(_CRYPTOfactory.class.getPackage().getName() + "." + type + "Crypter")
                    .getConstructor(byte[].class, String.class, SecureRandom.class)
                    .newInstance(key, cipher.toString(), SecureRandom.getInstance(salter.toString())));
}

From source file:org.apache.hadoop.hdfs.server.namenode.IngestLocal.java

static private Block[] readBlocks(DataInputStream in) throws IOException {
    int numBlocks = in.readInt();
    Block[] blocks = new Block[numBlocks];
    for (int i = 0; i < numBlocks; i++) {
        blocks[i] = new Block();
        blocks[i].readFields(in);/*  w  w w. ja  v  a  2s  .c om*/
    }
    return blocks;
}

From source file:se.llbit.math.Octree.java

/**
 * Deserialize the octree from a data input stream
 * @param in/* w  w w.ja  v  a  2s.c  om*/
 * @return The deserialized octree
 * @throws IOException
 */
public static Octree load(DataInputStream in) throws IOException {
    int treeDepth = in.readInt();
    Octree tree = new Octree(treeDepth);
    tree.root.load(in);
    return tree;
}

From source file:org.apache.hadoop.hdfs.server.namenode.bookkeeper.BookKeeperEditLogInputStream.java

/**
 * Safely reads the log version from the stream. Logic is exactly the same
 * as in the equivalent {@link EditLogFileInputStream} method.
 * @see EditLogFileInputStream#readLogVersion(DataInputStream)
 * @return The log version or 0 if stream is empty
 *//*from  w  w w  .jav  a 2 s.co  m*/
private static int readLogVersion(DataInputStream in) throws IOException {
    int logVersion = 0;
    in.mark(4);
    // See comments in EditLogFileInputStream as to why readLogVersion is
    // implemented in this way
    boolean available = true;
    try {
        logVersion = in.readByte();
    } catch (EOFException e) {
        available = false;
    }

    if (available) {
        in.reset();
        logVersion = in.readInt();
        if (logVersion < FSConstants.LAYOUT_VERSION) {
            throw new LedgerHeaderCorruptException("Unexpected version of the log segment in the ledger: "
                    + logVersion + ". Current version is " + FSConstants.LAYOUT_VERSION + ".");
        }
    }
    return logVersion;
}

From source file:uk.ac.ox.webauth.Token.java

/**
 * Turn a byte array into an int./*from   w w  w.j av  a  2  s  .c om*/
 * @param   bytes   The bytes to turn into an int.
 * @return  An Integer number, or null if there is a problem.
 */
public static Integer bytesToInt(byte[] bytes) {
    if (!(bytes.length == 4)) {
        return null;
    }
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
    try {
        return in.readInt();
    } catch (IOException ioe) {
        ioe.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();/*  w w  w  .j av  a 2s. 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:Messenger.TorLib.java

/**
 * This method creates a socket to the target host and port using TorSocketPre, then reads
 * the SOCKS information./*from   ww  w .  jav  a2s . co m*/
 * @param targetHostname Hostname of destination host.
 * @param targetPort Port on remote destination host.
 * @return Fully initialized TCP Socket that tunnels to the target Host/Port via the Tor Proxy host/port.
 * @throws IOException when Socket and Read/Write exceptions occur.
 */
static Socket TorSocket(String targetHostname, int targetPort) throws IOException {
    Socket s = TorSocketPre(targetHostname, targetPort, TOR_CONNECT);
    DataInputStream is = new DataInputStream(s.getInputStream());

    // only the status is useful on a TOR CONNECT
    byte version = is.readByte();
    byte status = is.readByte();
    if (status != (byte) 90) {
        //failed for some reason, return useful exception
        throw (new IOException(ParseSOCKSStatus(status)));
    }
    //      System.out.println("status: "+ParseSOCKSStatus(status));
    int port = is.readShort();
    int ipAddr = is.readInt();
    return (s);
}