List of usage examples for java.io BufferedOutputStream write
@Override public synchronized void write(int b) throws IOException
From source file:GZIPcompress.java
public static void main(String[] args) throws IOException { if (args.length == 0) { System.out.println("Usage: \nGZIPcompress file\n" + "\tUses GZIP compression to compress " + "the file to test.gz"); System.exit(1);/*from www . j a v a 2s . co m*/ } BufferedReader in = new BufferedReader(new FileReader(args[0])); BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz"))); System.out.println("Writing file"); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); System.out.println("Reading file"); BufferedReader in2 = new BufferedReader( new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz")))); String s; while ((s = in2.readLine()) != null) System.out.println(s); }
From source file:MyTest.DownloadFileTest.java
public static void main(String args[]) { CloseableHttpClient httpclient = HttpClients.createDefault(); String url = "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7"; System.out.println(url.charAt(50)); HttpGet httpget = new HttpGet(url); HttpEntity entity = null;/*from w w w . j a v a 2 s . c o m*/ try { HttpResponse response = httpclient.execute(httpget); entity = response.getEntity(); if (entity != null) { InputStream is = entity.getContent(); String filename = "testdata/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow"; BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename))); int readedByte; while ((readedByte = bis.read()) != -1) { bos.write(readedByte); } bis.close(); bos.close(); } httpclient.close(); } catch (IOException ex) { Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:BufferedCopy.java
public static void main(String[] args) throws Exception { BufferedInputStream bis = null; BufferedOutputStream bos = null; FileInputStream fis = new FileInputStream(args[0]); bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream(args[1]); bos = new BufferedOutputStream(fos); int byte_; while ((byte_ = bis.read()) != -1) bos.write(byte_); }
From source file:Base64Stuff.java
public static void main(String[] args) { //Random random = new Random(); try {/*from w ww . j a v a 2 s . c om*/ 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:Main.java
public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A test of Java Zipping"); for (int i = 0; i < args.length; i++) { System.out.println("Writing file " + args[i]); BufferedReader in = new BufferedReader(new FileReader(args[i])); zos.putNextEntry(new ZipEntry(args[i])); int c;//from w w w. ja va2 s.c om while ((c = in.read()) != -1) out.write(c); in.close(); } out.close(); System.out.println("Checksum: " + csum.getChecksum().getValue()); System.out.println("Reading file"); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in2 = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in2); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file " + ze); int x; while ((x = bis.read()) != -1) System.out.write(x); } System.out.println("Checksum: " + csumi.getChecksum().getValue()); bis.close(); }
From source file:ZipCompress.java
public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A test of Java Zipping"); // No corresponding getComment(), though. for (int i = 0; i < args.length; i++) { System.out.println("Writing file " + args[i]); BufferedReader in = new BufferedReader(new FileReader(args[i])); zos.putNextEntry(new ZipEntry(args[i])); int c;//from w w w . jav a 2s .c o m while ((c = in.read()) != -1) out.write(c); in.close(); } out.close(); // Checksum valid only after the file has been closed! System.out.println("Checksum: " + csum.getChecksum().getValue()); // Now extract the files: System.out.println("Reading file"); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in2 = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in2); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file " + ze); int x; while ((x = bis.read()) != -1) System.out.write(x); } System.out.println("Checksum: " + csumi.getChecksum().getValue()); bis.close(); // Alternative way to open and read zip files: ZipFile zf = new ZipFile("test.zip"); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); System.out.println("File: " + ze2); // ... and extract the data as before } }
From source file:boa.compiler.BoaCompiler.java
public static void main(final String[] args) throws IOException { CommandLine cl = processCommandLineOptions(args); if (cl == null) return;/*from w ww .jav a 2 s .co m*/ final ArrayList<File> inputFiles = BoaCompiler.inputFiles; // get the name of the generated class final String className = getGeneratedClass(cl); // get the filename of the jar we will be writing final String jarName; if (cl.hasOption('o')) jarName = cl.getOptionValue('o'); else jarName = className + ".jar"; // make the output directory File outputRoot = null; if (cl.hasOption("cd")) { outputRoot = new File(cl.getOptionValue("cd")); } else { outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString()); } final File outputSrcDir = new File(outputRoot, "boa"); if (!outputSrcDir.mkdirs()) throw new IOException("unable to mkdir " + outputSrcDir); // find custom libs to load final List<URL> libs = new ArrayList<URL>(); if (cl.hasOption('l')) for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL()); final File outputFile = new File(outputSrcDir, className + ".java"); final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile)); try { final List<String> jobnames = new ArrayList<String>(); final List<String> jobs = new ArrayList<String>(); boolean isSimple = true; final List<Program> visitorPrograms = new ArrayList<Program>(); SymbolTable.initialize(libs); final int maxVisitors; if (cl.hasOption('v')) maxVisitors = Integer.parseInt(cl.getOptionValue('v')); else maxVisitors = Integer.MAX_VALUE; for (int i = 0; i < inputFiles.size(); i++) { final File f = inputFiles.get(i); try { final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath())); lexer.removeErrorListeners(); lexer.addErrorListener(new LexerErrorListener()); final CommonTokenStream tokens = new CommonTokenStream(lexer); final BoaParser parser = new BoaParser(tokens); parser.removeErrorListeners(); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException { throw new ParseCancellationException(e); } }); final BoaErrorListener parserErrorListener = new ParserErrorListener(); final Start p = parse(tokens, parser, parserErrorListener); if (cl.hasOption("ast")) new ASTPrintingVisitor().start(p); final String jobName = "" + i; try { if (!parserErrorListener.hasError) { new TypeCheckingVisitor().start(p, new SymbolTable()); final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor(); simpleVisitor.start(p); LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex")); isSimple &= !simpleVisitor.isComplex(); new ShadowTypeEraser().start(p); new InheritedAttributeTransformer().start(p); new LocalAggregationTransformer().start(p); // if a job has no visitor, let it have its own method // also let jobs have own methods if visitor merging is disabled if (!simpleVisitor.isComplex() || maxVisitors < 2 || inputFiles.size() == 1) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(jobName); } // if a job has visitors, fuse them all together into a single program else { p.getProgram().jobName = jobName; visitorPrograms.add(p.getProgram()); } } } catch (final TypeCheckException e) { parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e); } } catch (final Exception e) { System.err.print(f.getName() + ": compilation failed: "); e.printStackTrace(); } } if (!visitorPrograms.isEmpty()) try { for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } catch (final Exception e) { System.err.println("error fusing visitors - falling back: " + e); e.printStackTrace(); for (final Program p : visitorPrograms) { new VisitorOptimizingTransformer().start(p); if (cl.hasOption("pp")) new PrettyPrintVisitor().start(p); if (cl.hasOption("ast2")) new ASTPrintingVisitor().start(p); final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName); cg.start(p); jobs.add(cg.getCode()); jobnames.add(p.jobName); } } if (jobs.size() == 0) throw new RuntimeException("no files compiled without error"); final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program"); st.add("name", className); st.add("numreducers", inputFiles.size()); st.add("jobs", jobs); st.add("jobnames", jobnames); st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings); st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings); st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024); if (DefaultProperties.localDataPath != null) { st.add("isLocal", true); } o.write(st.render().getBytes()); } finally { o.close(); } compileGeneratedSrc(cl, jarName, outputRoot, outputFile); }
From source file:Main.java
public static void saveFile(File f, byte[] content) throws IOException { FileOutputStream fos = new FileOutputStream(f); try {/*from ww w .ja va2 s . c o m*/ BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write(content); bos.flush(); } finally { fos.close(); } }
From source file:Main.java
public static void writeFully(File file, byte[] data) throws IOException { final OutputStream out = new FileOutputStream(file); final BufferedOutputStream bos = new BufferedOutputStream(out); try {//from ww w. j a v a 2 s . c o m bos.write(data); } finally { bos.flush(); bos.close(); } }
From source file:Main.java
public static void writeData(OutputStream stream, String str) { if (null == stream || TextUtils.isEmpty(str)) { return;/*ww w. j av a 2 s .com*/ } str = str + "\r\n"; try { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(stream); bufferedOutputStream.write(str.getBytes()); bufferedOutputStream.flush(); } catch (Exception ex) { ex.printStackTrace(); } finally { } }