Example usage for java.io BufferedInputStream reset

List of usage examples for java.io BufferedInputStream reset

Introduction

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

Prototype

public synchronized void reset() throws IOException 

Source Link

Document

See the general contract of the reset method of InputStream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream iStream = new FileInputStream("c:/test.txt");
    BufferedInputStream bis = new BufferedInputStream(iStream);

    // read and print characters one by one
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

    // mark is set on the input stream
    bis.mark(0);/*  www .j  a va  2 s.co  m*/
    System.out.println((char) bis.read());

    // reset is called
    bis.reset();

    // read and print characters
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    InputStream iStream = new FileInputStream("c:/test.txt");

    // input stream converted to buffered input stream
    BufferedInputStream bis = new BufferedInputStream(iStream);

    // read and print characters one by one
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

    // mark is set on the input stream
    bis.mark(0);/*w  w w . j  a v  a 2 s. c o m*/
    System.out.println((char) bis.read());
    System.out.println("reset() invoked");

    // reset is called
    bis.reset();

    // read and print characters
    System.out.println((char) bis.read());
    System.out.println((char) bis.read());

}

From source file:org.schreibubi.tolka.Tolka.java

/**
 * @param args//  w  w  w  .j  a v  a2 s.  c o  m
 */
@SuppressWarnings("static-access")
public static void main(String[] args) {
    Options options = new Options();
    try {
        CommandLineParser CLparser = new PosixParser();

        options.addOption(OptionBuilder.withLongOpt("format").withDescription("format, either plain or chipid")
                .hasArg().withArgName("format").create('f'));
        options.addOption(OptionBuilder.withLongOpt("dclog").withDescription("output results in dclog format")
                .hasArg().withArgName("file").create('d'));
        options.addOption(OptionBuilder.withLongOpt("help").withDescription("help").create('h'));
        options.addOption(OptionBuilder.withLongOpt("version").withDescription("version").create('v'));

        CommandLine line = CLparser.parse(options, args);

        if (line.hasOption("h")) {
            printHelpAndExit(options);
        }

        if (line.hasOption("v")) {
            Info.printVersion("Tolka");
            Runtime.getRuntime().exit(0);
        }

        String dclogFile = null;
        if (line.hasOption("d")) {
            dclogFile = line.getOptionValue("d");
        }

        String[] leftargs = line.getArgs();

        if (leftargs.length < 2) {
            System.err.println("Not enough arguments, see -h or --help");
            Runtime.getRuntime().exit(0);
        } else {
            try {
                BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(leftargs[0]));
                fileInputStream.mark(1100);
                byte[] start = new byte[1024];
                fileInputStream.read(start);
                fileInputStream.reset();

                ImportDataInterface in = null;
                if (EfuseModuleFormat.testForMagic(start)) {
                    System.out.println("Detected e-fuse modules format");
                    in = new EfuseModuleFormat();
                } else if (BsreadFormat.testForMagic(start)) {
                    System.out.println("Detected generic BE bitstream format");
                    in = new BsreadFormat();
                } else if (line.hasOption("f")) {
                    String format = line.getOptionValue("f");
                    if (format.contains("chipid")) {
                        System.out.println("BE chipid format");
                        in = new ChipIdFormat();
                    } else if (format.contains("plain")) {
                        System.out.println("Plain format");
                        in = new PlainFormat();
                    } else if (format.contains("cbm")) {
                        System.out.println("CBM format");
                        in = new CBMDatFormat();
                    } else {
                        System.err.println("Unknown file format specified");
                        Runtime.getRuntime().exit(0);
                    }
                } else {
                    System.err.println("File format could not be autodetected, please use --format");
                    Runtime.getRuntime().exit(0);
                }
                VArrayList<DutBitstream> bitstreamList = null;
                try {
                    bitstreamList = in.readData(fileInputStream);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                fileInputStream.close();

                System.out.println("Raw data (MSB..LSB):");
                for (DutBitstream bitstream : bitstreamList) {
                    System.out.print(bitstream.getInfo() + ":");
                    for (int stream = 0; stream < bitstream.getStreamCount(); stream++) {
                        for (int bit = in.getBitLength() - 1; bit >= 0; bit--) {
                            if (bit < bitstream.getBitstream(stream).bitLength()) {
                                if (bitstream.getBitstream(stream).testBit(bit)) {
                                    System.out.print("1");
                                } else {
                                    System.out.print("0");
                                }
                            } else {
                                System.out.print("0");
                            }
                        }
                        System.out.print(":");
                    }
                    System.out.println();
                }

                CharStream input = new ANTLRFileStream(leftargs[1]);
                TolkaGrammarLexer lex = new TolkaGrammarLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lex);
                TolkaGrammarParser parser = new TolkaGrammarParser(tokens);
                TreeAdaptor adaptor = new IndexedBufferedTreeAdaptor();
                String[] tokenNames = parser.getTokenNames();
                parser.setTreeAdaptor(adaptor);
                TolkaGrammarParser.rules_return r = parser.rules();
                // System.out.println("tree: " + ((Tree)
                // r.getTree()).toStringTree());

                IndexedBufferedTree t = (IndexedBufferedTree) r.getTree();

                // ASTFrame af = new ASTFrame("Tree", t);
                // af.setVisible(true);

                // Construct the tree.

                System.out.println("Interpreted data:");

                IndexedBufferedTreeNodeStream nodes = new IndexedBufferedTreeNodeStream(t);
                //int dummy = nodes.size(); // do not delete!
                TreeWizard wiz = new TreeWizard(adaptor, tokenNames);
                final LinkedHashMap<String, Integer> rules = new LinkedHashMap<String, Integer>();
                wiz.visit(t, wiz.getTokenType("RULE"), new TreeWizard.Visitor() {
                    @Override
                    public void visit(Object t) {
                        String name = ((IndexedBufferedTree) t).getChild(0).getText();
                        rules.put(name, ((IndexedBufferedTree) t).getIndex());
                    }
                });

                TolkaTreeWalker walker = new TolkaTreeWalker(nodes);
                walker.rulesLookup = rules;
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                if (dclogFile != null) {
                    addDclogHeader(pw);
                }
                for (DutBitstream bitstream : bitstreamList) {
                    nodes.rewind();
                    if (dclogFile != null) {
                        addDclogPrefix(pw, bitstream.getDut(), bitstream.getInfo(), bitstream.getTname());
                    }
                    ArrayList<Symbol> params = new ArrayList<Symbol>();
                    for (int stream = 0; stream < bitstream.getStreamCount(); stream++) {
                        params.add(new SymbolInteger(bitstream.getBitstream(stream)));
                    }
                    walker.rulestart(pw, params);
                }
                System.out.println(sw.toString());
                // Write also to dclog-file
                if (dclogFile != null) {
                    PrintWriter dcw = new PrintWriter(new BufferedWriter(new FileWriter(dclogFile)));
                    dcw.print(sw.toString());
                    dcw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (RecognitionException e) {
                e.printStackTrace();
            }
        }
    } catch (ParseException e) {
        printHelpAndExit(options);
    } catch (Exception e) {
        System.out.println("Tolka error: " + e);
    }

}

From source file:BufferedInputStreamDemo.java

public static void main(String args[]) throws IOException {
    String s = "This is a &copy; copyright symbol " + "but this is &copy not.\n";
    byte buf[] = s.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    BufferedInputStream f = new BufferedInputStream(in);
    int c;/*  w  w  w . j  a v a  2  s.  com*/
    boolean marked = false;

    while ((c = f.read()) != -1) {
        switch (c) {
        case '&':
            if (!marked) {
                f.mark(32);
                marked = true;
            } else {
                marked = false;
            }
            break;
        case ';':
            if (marked) {
                marked = false;
                System.out.print("(c)");
            } else
                System.out.print((char) c);
            break;
        case ' ':
            if (marked) {
                marked = false;
                f.reset();
                System.out.print("&");
            } else
                System.out.print((char) c);
            break;
        default:
            if (!marked)
                System.out.print((char) c);
            break;
        }
    }
}

From source file:pl.nask.hsn2.service.Md5HashGenerator.java

private static String md5hashFromFile(BufferedInputStream bufferedInputStream) throws IOException {
    bufferedInputStream.reset();
    String result = null;/*from ww  w .j  a  v  a2  s  .c om*/
    MessageDigest md;
    try {
        md = MessageDigest.getInstance("MD5");
        md.reset();
        try (InputStream dis = new DigestInputStream(new WhiteListFileInputStream(bufferedInputStream), md)) {
            while (dis.read() != -1) {
                // Nothing to do.
            }
            char[] md5 = Hex.encodeHex(md.digest());
            result = String.valueOf(md5);
        }
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("Could not create MD5 hash for whitelisting.\n{}", e);
        result = "";
    }
    return result;
}

From source file:Main.java

public static Document load(InputStream stream) {
    try {/*from  w w w. ja  v a  2 s . com*/
        BufferedInputStream bs = new BufferedInputStream(stream);
        bs.mark(1);

        if (-1 == bs.read())
            return null;

        bs.reset();
        return newDocumentBuilder().parse(bs);
    } catch (Exception e) {
        return null;
    }
}

From source file:ste.web.beanshell.BeanShellUtils.java

public static Object getJSONBody(final InputStream in) throws IOException {
    Object o = null;//  www.  j  av a  2s .  c  o m
    try {
        BufferedInputStream is = new BufferedInputStream(in);
        is.mark(0);
        if (is.read() == '{') {
            is.reset();
            o = new JSONObject(new JSONTokener(new InputStreamReader(is)));
        } else {
            is.reset();
            o = new JSONArray(new JSONTokener(new InputStreamReader(is)));
        }
    } catch (JSONException x) {
        throw new IOException("error parsing the body as a JSON object", x);
    }

    return o;
}

From source file:Main.java

public static Bitmap decode(InputStream in, int reqWidth, int reqHeight) throws IOException {
    BufferedInputStream inputStream = new BufferedInputStream(in);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w ww  .j  a v  a2 s .  c o  m*/
    inputStream.mark(64 * 1024);
    BitmapFactory.decodeStream(inputStream, null, options);
    inputStream.reset();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeStream(inputStream, null, options);
}

From source file:Main.java

private static Reader getUTF8Reader(InputStream f) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(f);
    assert bis.markSupported();
    bis.mark(3);/*from   www . java 2 s  .co  m*/
    boolean reset = true;
    byte[] t = new byte[3];
    bis.read(t);
    if (t[0] == ((byte) 0xef) && t[1] == ((byte) 0xbb) && t[2] == ((byte) 0xbf)) {
        reset = false;
    }
    if (reset) {
        bis.reset();
    }
    return new InputStreamReader(bis, "UTF-8");
}

From source file:Main.java

@SuppressWarnings("SameParameterValue")
public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException {
    URL url = new URL(uri);
    BufferedInputStream is = null;
    try {/*w w  w  .j a v  a  2  s .  com*/
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        is = new BufferedInputStream(urlConnection.getInputStream());
        is.mark(MAX_READ_LIMIT_PER_IMG);
        int scaleFactor = findScaleFactor(width, height, is);
        is.reset();
        return scaleBitmap(scaleFactor, is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}