List of usage examples for java.io BufferedInputStream close
public void close() throws IOException
From source file:gemlite.core.internal.support.hotdeploy.scanner.ScannerIteratorItem.java
private byte[] readContent(ClassLoader loader, JarEntry entry) throws IOException { URL url = loader.getResource(entry.getName()); URLConnection ulc = url.openConnection(); InputStream in3 = ulc.getInputStream(); InputStream in2 = url.openStream(); InputStream in = loader.getResourceAsStream(entry.getName()); if (in == null) { LogUtil.getCoreLog().trace("ReadContent inputStream is null entry.name={} , loader={}", entry.getName(), loader);/*from w w w . java 2s .c o m*/ } BufferedInputStream bi = new BufferedInputStream(in); byte[] bt = new byte[in.available()]; bi.read(bt); bi.close(); in.close(); return bt; }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * // w w w . j ava2s . co m * Extract a GZip file to a tar * @param in_file * the input bz2 file to extract * @param out_file * the output tar file to extract to */ public static void extractGzip(File in_file, File out_file) throws CompressorException { FileOutputStream out = null; GZIPInputStream zIn = null; FileInputStream fis = null; BufferedInputStream bis = null; try { out = new FileOutputStream(out_file); fis = new FileInputStream(in_file); bis = new BufferedInputStream(fis, Conf.getBufferSize()); zIn = new GZIPInputStream(bis); byte[] buffer = new byte[Conf.getBufferSize()]; int count = 0; while ((count = zIn.read(buffer, 0, Conf.getBufferSize())) != -1) { out.write(buffer, 0, count); } } catch (IOException ioe) { String msg = "Problem uncompressing Gzip " + ioe.getMessage() + " "; throw new CompressorException(msg + in_file.getAbsolutePath()); } finally { try { if (bis != null) bis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (fis != null) fis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (out != null) out.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (zIn != null) zIn.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } } }
From source file:com.wabacus.system.dataimport.DataImportItem.java
public static void backupOrDeleteDataFile(File fileObj) { try {//from w ww . j a va 2 s .c o m if (fileObj == null || !fileObj.exists()) { return; } String backuppath = Config.getInstance().getSystemConfigValue("dataimport-backuppath", ""); if (backuppath.equals("")) { log.debug("?" + fileObj.getAbsolutePath()); } else {//? String filename = fileObj.getName(); int idxdot = filename.lastIndexOf("."); if (idxdot > 0) { filename = filename.substring(0, idxdot) + "_" + Tools.getStrDatetime("yyyyMMddHHmmss", new Date()) + filename.substring(idxdot); } else { filename = filename + "_" + Tools.getStrDatetime("yyyyMMddHHmmss", new Date()); } File destFile = new File( FilePathAssistant.getInstance().standardFilePath(backuppath + File.separator + filename)); log.debug("?" + fileObj.getAbsolutePath() + "" + backuppath + ""); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(fileObj)); bos = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] btmp = new byte[1024]; while (bis.read(btmp) != -1) { bos.write(btmp); } } catch (Exception e) { log.error("?" + fileObj.getAbsolutePath() + "", e); } finally { try { if (bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } try { if (bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } } } fileObj.delete(); } catch (Exception e) { log.error("?" + fileObj.getAbsolutePath() + "", e); } }
From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.TestListWrapper.java
protected Properties loadJdbcProps() throws IOException, FileNotFoundException { jdbcProps = new Properties(); String jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties"); //BufferedInputStream is = new BufferedInputStream(new FileInputStream("C:/Docume~1/tony/.m2/jdbc.properties")); BufferedInputStream is = new BufferedInputStream(new FileInputStream(jdbcPropFile)); jdbcProps.load(is);//from w w w . j a va 2 s.c o m is.close(); return jdbcProps; }
From source file:org.ametro.util.WebUtil.java
public static void downloadFileUnchecked(Object context, URI uri, File file, IDownloadListener listener) throws Exception { BufferedInputStream strm = null; if (listener != null) { listener.onBegin(context, file); }//from www . j a v a 2 s . c om try { HttpClient client = ApplicationEx.getInstance().getHttpClient(); HttpGet request = new HttpGet(); request.setURI(uri); HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); if (status.getStatusCode() == 200) { HttpEntity entity = response.getEntity(); long total = (int) entity.getContentLength(); long position = 0; if (file.exists()) { file.delete(); } BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(entity.getContent()); out = new BufferedOutputStream(new FileOutputStream(file)); byte[] bytes = new byte[2048]; for (int c = in.read(bytes); c != -1; c = in.read(bytes)) { out.write(bytes, 0, c); position += c; if (listener != null) { if (!listener.onUpdate(context, position, total)) { throw new DownloadCanceledException(); } } } } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } if (out != null) { try { out.close(); } catch (Exception e) { } } } if (listener != null) { listener.onDone(context, file); } } else { String message = "Failed to download URL " + uri.toString() + " due error " + status.getStatusCode() + " " + status.getReasonPhrase(); throw new Exception(message); } } finally { if (strm != null) { try { strm.close(); } catch (IOException ex) { } } } }
From source file:com.sun.socialsite.util.Utilities.java
public static void copyInputToOutput(InputStream input, OutputStream output) throws IOException { BufferedInputStream in = new BufferedInputStream(input); BufferedOutputStream out = new BufferedOutputStream(output); byte buffer[] = new byte[8192]; for (int count = 0; count != -1;) { count = in.read(buffer, 0, 8192); if (count != -1) out.write(buffer, 0, count); }/*w ww .j a va2 s. c o m*/ try { in.close(); out.close(); } catch (IOException ex) { throw new IOException("Closing file streams, " + ex.getMessage()); } }
From source file:com.yunmel.syncretic.utils.io.IOUtils.java
private static void compressFile(File file, String fileName, ZipOutputStream zipout, String baseDir) { try {/*w w w . ja v a 2s .co m*/ ZipEntry entry = null; if (baseDir.equals("/")) { baseDir = ""; } String filename = new String((baseDir + fileName).getBytes(), "GBK"); entry = new ZipEntry(filename); entry.setSize(file.length()); zipout.putNextEntry(entry); BufferedInputStream fr = new BufferedInputStream(new FileInputStream(file)); int len; byte[] buffer = new byte[1024]; while ((len = fr.read(buffer)) != -1) zipout.write(buffer, 0, len); fr.close(); } catch (IOException e) { } }
From source file:com.clouddrive.parth.AmazonOperations.java
public byte[] downloadFile(String fileName, String userName) { S3Object object = s3.getObject(new GetObjectRequest(userName, fileName)); byte[] fileBytes = null; try {/*from w w w . j a va 2 s . c o m*/ IOUtils.copy(object.getObjectContent(), new FileOutputStream(userName + "" + fileName)); File file = new File(userName + "" + fileName); FileInputStream fis = new FileInputStream(file); BufferedInputStream inputStream = new BufferedInputStream(fis); fileBytes = new byte[(int) file.length()]; inputStream.read(fileBytes); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return fileBytes; }
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/*w w w .j a va2 s. c o m*/ */ 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(); } }
From source file:com.aurel.track.lucene.util.FileUtil.java
public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException { if (!unzipDestinationDirectory.exists()) { unzipDestinationDirectory.mkdirs(); }//from ww w . java 2s . c om final int BUFFER = 2048; // Open Zip file for reading ZipFile zipFile = new ZipFile(uploadZip, ZipFile.OPEN_READ); // Create an enumeration of the entries in the zip file Enumeration zipFileEntries = zipFile.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(unzipDestinationDirectory, currentEntry); // grab file's parent directory structure File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); // extract file if not a directory if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } zipFile.close(); }