List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:PrintFile.java
public static void main(String args[]) throws Exception { if (args.length != 2) { System.err.println("usage : java PrintFile port file"); System.err.println("sample: java PrintFile LPT1 sample.prn"); System.exit(-1);// ww w . j ava2s. co m } String portname = args[0]; String filename = args[1]; // Get port CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname); // Open port // Requires owner name and timeout CommPort port = portId.open("Java Printing", 30000); // Setup reading from file FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); // Setup output OutputStream os = port.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(os); int c; while ((c = bis.read()) != -1) { bos.write(c); } // Close bos.close(); bis.close(); port.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ZipFile zf = new ZipFile("a.zip"); Enumeration<? extends ZipEntry> files = zf.entries(); while (files.hasMoreElements()) { ZipEntry ze = files.nextElement(); System.out.println("Decompressing " + ze.getName()); System.out.println(/*w w w . j a va2 s. c om*/ " Compressed Size: " + ze.getCompressedSize() + " Expanded Size: " + ze.getSize() + "\n"); BufferedInputStream fin = new BufferedInputStream(zf.getInputStream(ze)); BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(ze.getName())); int i; do { i = fin.read(); if (i != -1) fout.write(i); } while (i != -1); fout.close(); fin.close(); } zf.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0]))); for (int n = 1; n < args.length; n++) { BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n])); ZipEntry ze = new ZipEntry(rmPath(args[n])); fout.putNextEntry(ze);//from ww w . ja v a 2 s . c o m int i; do { i = fin.read(); if (i != -1) fout.write(i); } while (i != -1); fout.closeEntry(); fin.close(); System.out.println("Compressing " + args[n]); System.out.println( " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n"); } fout.close(); }
From source file:CompressIt.java
public static void main(String[] args) { String filename = args[0];/*from w w w.j a va2 s . c o m*/ try { File file = new File(filename); int length = (int) file.length(); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); ByteArrayOutputStream baos = new ByteArrayOutputStream(length); GZIPOutputStream gos = new GZIPOutputStream(baos); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = bis.read(buffer)) != -1) { gos.write(buffer, 0, bytesRead); } bis.close(); gos.close(); System.out.println("Input Length: " + length); System.out.println("Output Length: " + baos.size()); } catch (FileNotFoundException e) { System.err.println("Invalid Filename"); } catch (IOException e) { System.err.println("I/O Exception"); } }
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;/* ww w . ja v a 2 s. c o m*/ 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;// w w w.j a v a 2 s. 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: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 ww. jav a2s. 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:com.manning.blogapps.chapter10.examples.AuthPostJava.java
public static void main(String[] args) throws Exception { if (args.length < 4) { System.out.println("USAGE: authpost <username> <password> <filepath> <url>"); System.exit(-1);/*from w w w . ja va2s .c o m*/ } String credentials = args[0] + ":" + args[1]; String filepath = args[2]; URL url = new URL(args[3]); URLConnection conn = url.openConnection(); conn.setDoOutput(true); conn.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64(credentials.getBytes()))); File upload = new File(filepath); conn.setRequestProperty("name", upload.getName()); String contentType = "application/atom+xml; charset=utf8"; if (filepath.endsWith(".gif")) contentType = "image/gif"; else if (filepath.endsWith(".jpg")) contentType = "image/jpg"; conn.setRequestProperty("Content-type", contentType); BufferedInputStream filein = new BufferedInputStream(new FileInputStream(upload)); BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream()); byte buffer[] = new byte[8192]; for (int count = 0; count != -1;) { count = filein.read(buffer, 0, 8192); if (count != -1) out.write(buffer, 0, count); } filein.close(); out.close(); String s = null; BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((s = in.readLine()) != null) { System.out.println(s); } }
From source file:Base64Stuff.java
public static void main(String[] args) { //Random random = new Random(); try {//from w ww. j av 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:Main.java
public static void main(String[] args) throws Exception { String zipname = "data.zip"; ZipFile zipFile = new ZipFile(zipname); Enumeration enumeration = zipFile.entries(); while (enumeration.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); System.out.println("Unzipping: " + zipEntry.getName()); BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry)); int size; byte[] buffer = new byte[2048]; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipEntry.getName()), buffer.length);//from w ww .j av a 2s .c o m while ((size = bis.read(buffer, 0, buffer.length)) != -1) { bos.write(buffer, 0, size); } bos.flush(); bos.close(); bis.close(); } }