List of usage examples for java.io InputStream close
public void close() throws IOException
From source file:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and writes it in uncompressed * form to the desired file.//from ww w. j a v a 2s.c om * @param zipFile the zip file to read from * @param dest the file to write the first zip file entry to * @return same as destination * @throws IOException if there is an error accessing the zip file or the * destination file */ public static File readFirstZipEntry(File zipFile, File dest) throws IOException { // open zip and get first entry ZipFile zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> entries = zf.getEntries(); ZipArchiveEntry entry = entries.nextElement(); // write to file InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(dest); ByteStreams.copy(in, out); // close all streams and return the new file in.close(); out.close(); zf.close(); return dest; }
From source file:net.padaf.preflight.helpers.MetadataValidationHelper.java
/** * Return the xpacket from the dictionary's stream *//*from w ww . java 2 s. c om*/ public static byte[] getXpacket(COSDocument cdocument) throws IOException, XpacketParsingException { COSObject catalog = cdocument.getCatalog(); COSBase cb = catalog.getDictionaryObject(COSName.METADATA); if (cb == null) { // missing Metadata Key in catalog ValidationError error = new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT, "Missing Metadata Key in catalog"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } // no filter key COSDictionary metadataDictionnary = COSUtils.getAsDictionary(cb, cdocument); if (metadataDictionnary.getItem(COSName.FILTER) != null) { // should not be defined ValidationError error = new ValidationError(ValidationConstants.ERROR_SYNTAX_STREAM_INVALID_FILTER, "Filter specified in metadata dictionnary"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } PDStream stream = PDStream.createFromCOS(metadataDictionnary); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = stream.createInputStream(); IOUtils.copy(is, bos); is.close(); bos.close(); return bos.toByteArray(); }
From source file:Main.java
public static void copyfile(File source, File dest) { try {//w w w . ja va2s . c om InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(dest); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException e) { Log.e(TAG, e.getLocalizedMessage()); } catch (IOException e) { Log.e(TAG, e.getLocalizedMessage()); } }
From source file:Main.java
public static String read(InputStream in) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000); for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line);/*from w ww.j a va 2s . com*/ } in.close(); return sb.toString(); }
From source file:Main.java
public static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) { InputStream in = null; OutputStream out = null;//from w w w . j a v a 2 s .co m try { in = assetManager.open(fromAssetPath); new File(toPath).createNewFile(); out = new FileOutputStream(toPath); copyFile(in, out); in.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static byte[] readInputStreamFully(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[32768]; int count;/* ww w. j a v a 2 s . c o m*/ try { while ((count = is.read(buffer)) != -1) { os.write(buffer, 0, count); } is.close(); } catch (IOException e) { throw new RuntimeException(e); } return os.toByteArray(); }
From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java
public synchronized static X509Certificate getClass1CaCert() throws IOException { if (class1CaCert == null) { ClassLoader classLoader = ClassLoaderUtil.getClassLoader(); InputStream is = classLoader.getResourceAsStream(CLASS1_CA_Cert_FILE); class1CaCert = PKCSReader.readCert(is); is.close(); }/*ww w . ja v a 2 s. c o m*/ return class1CaCert; }
From source file:com.aqnote.shared.cryptology.cert.loader.CaCertLoader.java
public synchronized static X509Certificate getClass2CaCert() throws IOException { if (class2CaCert == null) { ClassLoader classLoader = ClassLoaderUtil.getClassLoader(); InputStream is = classLoader.getResourceAsStream(CLASS2_CA_Cert_FILE); class2CaCert = PKCSReader.readCert(is); is.close(); }/*from w w w. j a v a 2 s.c om*/ return class2CaCert; }
From source file:Main.java
public static File getFileFromInputStream(File file, InputStream ins) throws IOException { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead);//w w w. ja va 2s. c o m } os.close(); ins.close(); return file; }