Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

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

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:com.jejking.hh.nord.corpus.FetchedDruckSachenProcessor.java

/**
 * Runs the program. The arguments expected are:
 * <ol>//from   w  w  w  .  ja  v a 2 s . c o m
 * <li>file path to a copy of the HTML Drucksachen index.</li>
 * <li>directory containing compressed HTML files downloaded where the file name is the hex encoded originating URL</li>
 * <li>directory to which serialised {@link RawDrucksache} objects are to be written to</li>
 * </ol>
 * 
 * @param args, as above
 */
public static void main(String[] args) throws Exception {
    BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(args[0]));
    DrucksachenLinkAndDateExtractor linkAndDateExtractor = new DrucksachenLinkAndDateExtractor(inputStream);

    ImmutableMap<URL, Optional<LocalDate>> urlDateMap = linkAndDateExtractor.call();

    FetchedDruckSachenProcessor proc = new FetchedDruckSachenProcessor();
    proc.preProcessFetchedDocuments(new File(args[1]), new File(args[2]), urlDateMap);

}

From source file:Base64Stuff.java

public static void main(String[] args) {
    //Random random = new Random();
    try {/*from www.j a v  a 2  s .  c  o  m*/
        File file1 = new File("C:\\\\Program Files\\\\ImageJ\\\\images\\\\confocal-series-10001.tif");
        //File file2 = new File("C:\\Program Files\\ImageJ\\images\\confocal-series-10000.tif");
        ImagePlus image1 = new ImagePlus(
                "C:\\\\Program Files\\\\ImageJ\\\\images\\\\confocal-series-10001.tif");
        //ImagePlus image2 = new ImagePlus("C:\\Program Files\\ImageJ\\images\\two.tif");

        byte[] myBytes1 = org.apache.commons.io.FileUtils.readFileToByteArray(file1);
        //byte[] myBytes2 = org.apache.commons.io.FileUtils.readFileToByteArray(file2);
        //random.nextBytes(randomBytes);

        //String internalVersion1 = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(myBytes1);
        //String internalVersion2 = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(myBytes2);
        byte[] apacheBytes1 = org.apache.commons.codec.binary.Base64.encodeBase64(myBytes1);
        //byte[] apacheBytes2 =  org.apache.commons.codec.binary.Base64.encodeBase64(myBytes2);
        String string1 = new String(apacheBytes1);
        //String string2 = new String(apacheBytes2);

        System.out.println("File1 length:" + string1.length());
        //System.out.println("File2 length:" + string2.length());
        System.out.println(string1);
        //System.out.println(string2);

        System.out.println("Image1 size: (" + image1.getWidth() + "," + image1.getHeight() + ")");
        //System.out.println("Image2 size: (" + image2.getWidth() + "," + image2.getHeight() + ")");

        String urlParameters = "data=" + string1 + "&size=1000x1000";

        URL url = new URL("http://api.qrserver.com/v1/create-qr-code/");
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write(urlParameters);
        writer.flush();

        //byte buf[] = new byte[700000000];

        BufferedInputStream reader = new BufferedInputStream(conn.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("C:\\Users\\expertoweb\\Desktop\\qrcode2.png"));

        int data;
        while ((data = reader.read()) != -1) {
            bos.write(data);
        }

        writer.close();
        reader.close();
        bos.close();

    } catch (IOException e) {
    }
}

From source file:ObjectStreams.java

public static void main(String[] args) throws IOException, ClassNotFoundException {

    ObjectOutputStream out = null;
    try {/*  w ww  .ja v  a2  s .c  o m*/
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

        out.writeObject(Calendar.getInstance());
        for (int i = 0; i < prices.length; i++) {
            out.writeObject(prices[i]);
            out.writeInt(units[i]);
            out.writeUTF(descs[i]);
        }
    } finally {
        out.close();
    }

    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));

        Calendar date = null;
        BigDecimal price;
        int unit;
        String desc;
        BigDecimal total = new BigDecimal(0);

        date = (Calendar) in.readObject();

        System.out.format("On %tA, %<tB %<te, %<tY:%n", date);

        try {
            while (true) {
                price = (BigDecimal) in.readObject();
                unit = in.readInt();
                desc = in.readUTF();
                System.out.format("You ordered %d units of %s at $%.2f%n", unit, desc, price);
                total = total.add(price.multiply(new BigDecimal(unit)));
            }
        } catch (EOFException e) {
        }
        System.out.format("For a TOTAL of: $%.2f%n", total);
    } finally {
        in.close();
    }
}

From source file:edu.jhu.hlt.concrete.ingesters.annotatednyt.AnnotatedNYTIngesterRunner.java

/**
 * @param args// w w w  .j  a  v a 2  s.c  o  m
 */
public static void main(String... args) {
    Thread.setDefaultUncaughtExceptionHandler(new LoggedUncaughtExceptionHandler());
    AnnotatedNYTIngesterRunner run = new AnnotatedNYTIngesterRunner();
    JCommander jc = new JCommander(run, args);
    jc.setProgramName(AnnotatedNYTIngesterRunner.class.getSimpleName());
    if (run.delegate.help) {
        jc.usage();
    }

    try {
        Path outpath = Paths.get(run.delegate.outputPath);
        IngesterParameterDelegate.prepare(outpath);

        NYTCorpusDocumentParser parser = new NYTCorpusDocumentParser();
        for (String pstr : run.delegate.paths) {
            LOGGER.debug("Running on file: {}", pstr);
            Path p = Paths.get(pstr);
            new ExistingNonDirectoryFile(p);
            int nPaths = p.getNameCount();
            Path year = p.getName(nPaths - 2);
            Path outWithExt = outpath.resolve(year.toString() + p.getFileName());

            if (Files.exists(outWithExt)) {
                if (!run.delegate.overwrite) {
                    LOGGER.info("File: {} exists and overwrite disabled. Not running.", outWithExt.toString());
                    continue;
                } else {
                    Files.delete(outWithExt);
                }
            }

            try (InputStream is = Files.newInputStream(p);
                    BufferedInputStream bin = new BufferedInputStream(is);
                    TarGzArchiveEntryByteIterator iter = new TarGzArchiveEntryByteIterator(bin);

                    OutputStream os = Files.newOutputStream(outWithExt);
                    GzipCompressorOutputStream gout = new GzipCompressorOutputStream(os);
                    TarArchiver arch = new TarArchiver(gout)) {
                Iterable<byte[]> able = () -> iter;
                StreamSupport.stream(able.spliterator(), false).map(ba -> parser.fromByteArray(ba, false))
                        .map(doc -> new AnnotatedNYTDocument(doc))
                        .map(and -> new CommunicationizableAnnotatedNYTDocument(and).toCommunication())
                        .forEach(comm -> {
                            try {
                                arch.addEntry(new ArchivableCommunication(comm));
                            } catch (IOException e) {
                                LOGGER.error("Caught exception processing file: " + pstr, e);
                            }
                        });
            }
        }
    } catch (NotFileException | IOException e) {
        LOGGER.error("Caught exception processing.", e);
    }
}

From source file:PinotResponseTime.java

public static void main(String[] args) throws Exception {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        HttpPost post = new HttpPost("http://localhost:8099/query");
        CloseableHttpResponse res;//  ww  w.  jav a2s.c  om

        if (STORE_RESULT) {
            File dir = new File(RESULT_DIR);
            if (!dir.exists()) {
                dir.mkdirs();
            }
        }

        int length;

        // Make sure all segments online
        System.out.println("Test if number of records is " + RECORD_NUMBER);
        post.setEntity(new StringEntity("{\"pql\":\"select count(*) from tpch_lineitem\"}"));
        while (true) {
            System.out.print('*');
            res = client.execute(post);
            boolean valid;
            try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
                length = in.read(BUFFER);
                valid = new String(BUFFER, 0, length, "UTF-8").contains("\"value\":\"" + RECORD_NUMBER + "\"");
            }
            res.close();
            if (valid) {
                break;
            } else {
                Thread.sleep(5000);
            }
        }
        System.out.println("Number of Records Test Passed");

        // Start Benchmark
        for (int i = 0; i < QUERIES.length; i++) {
            System.out.println(
                    "--------------------------------------------------------------------------------");
            System.out.println("Start running query: " + QUERIES[i]);
            post.setEntity(new StringEntity("{\"pql\":\"" + QUERIES[i] + "\"}"));

            // Warm-up Rounds
            System.out.println("Run " + WARMUP_ROUND + " times to warm up cache...");
            for (int j = 0; j < WARMUP_ROUND; j++) {
                res = client.execute(post);
                if (!isValid(res, null)) {
                    System.out.println("\nInvalid Response, Sleep 20 Seconds...");
                    Thread.sleep(20000);
                }
                res.close();
                System.out.print('*');
            }
            System.out.println();

            // Test Rounds
            int[] time = new int[TEST_ROUND];
            int totalTime = 0;
            int validIdx = 0;
            System.out.println("Run " + TEST_ROUND + " times to get average time...");
            while (validIdx < TEST_ROUND) {
                long startTime = System.currentTimeMillis();
                res = client.execute(post);
                long endTime = System.currentTimeMillis();
                boolean valid;
                if (STORE_RESULT && validIdx == 0) {
                    valid = isValid(res, RESULT_DIR + File.separator + i + ".json");
                } else {
                    valid = isValid(res, null);
                }
                if (!valid) {
                    System.out.println("\nInvalid Response, Sleep 20 Seconds...");
                    Thread.sleep(20000);
                    res.close();
                    continue;
                }
                res.close();
                time[validIdx] = (int) (endTime - startTime);
                totalTime += time[validIdx];
                System.out.print(time[validIdx] + "ms ");
                validIdx++;
            }
            System.out.println();

            // Process Results
            double avgTime = (double) totalTime / TEST_ROUND;
            double stdDev = 0;
            for (int temp : time) {
                stdDev += (temp - avgTime) * (temp - avgTime) / TEST_ROUND;
            }
            stdDev = Math.sqrt(stdDev);
            System.out.println("The average response time for the query is: " + avgTime + "ms");
            System.out.println("The standard deviation is: " + stdDev);
        }
    }
}

From source file:edu.msu.cme.rdp.graph.sandbox.BloomFilterInterrogator.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: BloomFilterStats <bloom_filter>");
        System.exit(1);/*from  w w  w .ja v  a 2s . c o m*/
    }

    File bloomFile = new File(args[0]);

    ObjectInputStream ois = ois = new ObjectInputStream(
            new BufferedInputStream(new FileInputStream(bloomFile)));
    BloomFilter filter = (BloomFilter) ois.readObject();
    ois.close();

    printStats(filter, System.out);

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String line;
    CodonWalker walker = null;
    while ((line = reader.readLine()) != null) {
        char[] kmer = line.toCharArray();
        System.out.print(line + "\t");
        try {
            walker = filter.new RightCodonFacade(kmer);
            walker.jumpTo(kmer);
            System.out.print("present");
        } catch (Exception e) {
            System.out.print("not present\t" + e.getMessage());
        }
        System.out.println();
    }
}

From source file:GenSig.java

public static void main(String[] args) {

    /* Generate a DSA signature */

    if (args.length != 1) {
        System.out.println("Usage: GenSig nameOfFileToSign");
    } else//from w  w w . j a  v  a  2 s. c  o m
        try {

            /* Generate a key pair */

            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

            keyGen.initialize(1024, random);

            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();

            /*
             * Create a Signature object and initialize it with the private
             * key
             */

            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

            dsa.initSign(priv);

            /* Update and sign the data */

            FileInputStream fis = new FileInputStream(args[0]);
            BufferedInputStream bufin = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                dsa.update(buffer, 0, len);
            }
            ;

            bufin.close();

            /*
             * Now that all the data to be signed has been read in, generate
             * a signature for it
             */

            byte[] realSig = dsa.sign();

            /* Save the signature in a file */
            FileOutputStream sigfos = new FileOutputStream("sig");
            sigfos.write(realSig);

            sigfos.close();

            /* Save the public key in a file */
            byte[] key = pub.getEncoded();
            FileOutputStream keyfos = new FileOutputStream("suepk");
            keyfos.write(key);

            keyfos.close();

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

}

From source file:edu.gslis.ts.RunQuery.java

public static void main(String[] args) {
    try {// w ww. j av a  2  s .co m
        // Get the commandline options
        Options options = createOptions();
        CommandLineParser parser = new GnuParser();
        CommandLine cmd = parser.parse(options, args);

        String inputPath = cmd.getOptionValue("input");
        String eventsPath = cmd.getOptionValue("events");
        String stopPath = cmd.getOptionValue("stop");
        int queryId = Integer.valueOf(cmd.getOptionValue("query"));

        List<String> ids = FileUtils.readLines(new File(inputPath + File.separator + "ids.txt"));

        Stopper stopper = new Stopper(stopPath);
        Map<Integer, FeatureVector> queries = readEvents(eventsPath, stopper);

        FeatureVector query = queries.get(queryId);

        Pairtree ptree = new Pairtree();
        Bag<String> words = new HashBag<String>();

        for (String streamId : ids) {

            String ppath = ptree.mapToPPath(streamId.replace("-", ""));

            String inpath = inputPath + File.separator + ppath + File.separator + streamId + ".xz";
            //                System.out.println(inpath);
            File infile = new File(inpath);
            InputStream in = new XZInputStream(new FileInputStream(infile));

            TTransport inTransport = new TIOStreamTransport(new BufferedInputStream(in));
            TBinaryProtocol inProtocol = new TBinaryProtocol(inTransport);
            inTransport.open();
            final StreamItem item = new StreamItem();

            while (true) {
                try {
                    item.read(inProtocol);
                    //                        System.out.println("Read " + item.stream_id);

                } catch (TTransportException tte) {
                    // END_OF_FILE is used to indicate EOF and is not an exception.
                    if (tte.getType() != TTransportException.END_OF_FILE)
                        tte.printStackTrace();
                    break;
                }
            }

            // Do something with this document...
            String docText = item.getBody().getClean_visible();

            StringTokenizer itr = new StringTokenizer(docText);
            while (itr.hasMoreTokens()) {
                words.add(itr.nextToken());
            }

            inTransport.close();

        }

        for (String term : words.uniqueSet()) {
            System.out.println(term + ":" + words.getCount(term));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:edu.msu.cme.rdp.graph.utils.BloomFilterStats.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("USAGE: BloomFilterStats <bloom_filter>");
        System.exit(1);/* w  w w. jav  a2 s .  c  om*/
    }

    File bloomFile = new File(args[0]);

    ObjectInputStream ois = ois = new ObjectInputStream(
            new BufferedInputStream(new FileInputStream(bloomFile)));
    BloomFilter filter = (BloomFilter) ois.readObject();
    ois.close();

    printStats(filter, System.out);
}

From source file:edu.msu.cme.rdp.graph.sandbox.CheckReadKmerPresence.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("USAGE: CheckReadKmerPresence <bloom_filter> <nucl_seq_file>");
        System.exit(1);//  w  ww  . ja  va  2s  .  c  o m
    }

    File bloomFile = new File(args[0]);
    SeqReader reader = new SequenceReader(new File(args[1]));

    ObjectInputStream ois = ois = new ObjectInputStream(
            new BufferedInputStream(new FileInputStream(bloomFile)));
    BloomFilter filter = (BloomFilter) ois.readObject();
    ois.close();

    printStats(filter, System.out);
    Sequence seq;
    CodonWalker walker = null;
    while ((seq = reader.readNextSequence()) != null) {
        int kmerNum = 0;
        for (char[] kmer : KmerGenerator.getKmers(seq.getSeqString(), filter.getKmerSize())) {
            System.out.print(seq.getSeqName() + "\t" + (++kmerNum) + "\t" + kmer + "\t");
            try {
                walker = filter.new RightCodonFacade(kmer);
                System.out.println("true");
            } catch (Exception e) {
                System.out.println("false");
            }

        }
    }
}