List of usage examples for java.io BufferedInputStream mark
public synchronized void mark(int readlimit)
mark
method of InputStream
. From source file:BufferedInputStreamDemo.java
public static void main(String args[]) throws IOException { String s = "This is a © copyright symbol " + "but this is © not.\n"; byte buf[] = s.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(buf); BufferedInputStream f = new BufferedInputStream(in); int c;//from w w w . j av a2 s . c o m 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: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); System.out.println((char) bis.read()); // reset is called bis.reset();//from w w w . j av a 2 s . c o m // 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); System.out.println((char) bis.read()); System.out.println("reset() invoked"); // reset is called bis.reset();//from ww w . j a v a 2 s.c om // 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//from www .j av a 2s . co 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:org.bimserver.utils.ByteUtils.java
public static byte[] extractHead(BufferedInputStream bufferedInputStream, int maxSize) throws IOException { bufferedInputStream.mark(maxSize); byte[] initialBytes = new byte[maxSize]; int read = IOUtils.read(bufferedInputStream, initialBytes); if (read != maxSize) { byte[] trimmed = new byte[read]; System.arraycopy(initialBytes, 0, trimmed, 0, read); initialBytes = trimmed;/* ww w.j ava 2 s. c o m*/ } bufferedInputStream.reset(); return initialBytes; }
From source file:Main.java
public static Document load(InputStream stream) { try {//from www.java 2 s . co m 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:Main.java
private static Reader getUTF8Reader(InputStream f) throws IOException { BufferedInputStream bis = new BufferedInputStream(f); assert bis.markSupported(); bis.mark(3); boolean reset = true; byte[] t = new byte[3]; bis.read(t);// w w w . j a v a2s . c o m 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:ste.web.beanshell.BeanShellUtils.java
public static Object getJSONBody(final InputStream in) throws IOException { Object o = null;/* w w w . j ava2 s.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 String getCharset(File file) { String charset = "GBK"; byte[] first3Bytes = new byte[3]; try {//from w ww . j a v a2 s . c o m boolean checked = false; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); bis.mark(0); int read = bis.read(first3Bytes, 0, 3); if (read == -1) return charset; if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) { charset = "UTF-16LE"; checked = true; } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) { charset = "UTF-16BE"; checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF-8"; checked = true; } bis.reset(); if (!checked) { int loc = 0; while ((read = bis.read()) != -1) { loc++; if (read >= 0xF0) break; if (0x80 <= read && read <= 0xBF) break; if (0xC0 <= read && read <= 0xDF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) continue; else break; } else if (0xE0 <= read && read <= 0xEF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) { read = bis.read(); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else break; } else break; } } } bis.close(); } catch (Exception e) { e.printStackTrace(); } return charset; }
From source file:Main.java
/** * Decodes image from inputstream into a new Bitmap of specified dimensions. * * This is a long-running operation that must run in a background thread. * * @param is InputStream containing the image. * @param maxWidth target width of the output Bitmap. * @param maxHeight target height of the output Bitmap. * @return new Bitmap containing the image. * @throws IOException//from ww w. java2 s . c om */ public static Bitmap decodeBitmapBounded(InputStream is, int maxWidth, int maxHeight) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream(is, STREAM_BUFFER_SIZE); try { bufferedInputStream.mark(STREAM_BUFFER_SIZE); // should be enough to read image dimensions. // TODO(mattfrazier): fail more gracefully if mark isn't supported, but it should always be // by bufferedinputstream. BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); bufferedInputStream.reset(); bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = calculateInSampleSize(bmOptions.outWidth, bmOptions.outHeight, maxWidth, maxHeight); // TODO(mattfrazier): Samsung devices yield a rotated bitmap no matter what orientation is // captured. Read Exif data and rotate in place or communicate Exif data and rotate display // with matrix. return BitmapFactory.decodeStream(bufferedInputStream, null, bmOptions); } finally { bufferedInputStream.close(); } }